Mercurial > meta
changeset 3:189245a3c075
Wikipedia JSON example working
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Thu, 02 Dec 2010 12:17:51 -0500 |
parents | 478524b9d23a |
children | 8f7396495045 |
files | json.ur tests/testJson.ur tests/testJson.urs |
diffstat | 3 files changed, 72 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/json.ur Thu Dec 02 11:59:55 2010 -0500 +++ b/json.ur Thu Dec 02 12:17:51 2010 -0500 @@ -243,7 +243,7 @@ error <xml>JSON record doesn't begin with brace</xml> else let - val (r, s') = fromJ (String.substring s {Start = 1, Len = String.length s - 1}) + val (r, s') = fromJ (skipSpaces (String.substring s {Start = 1, Len = String.length s - 1})) (@map0 [option] (fn [t ::_] => None) fl) in (@map2 [option] [fn _ => string] [id] (fn [t] (v : option t) name =>
--- a/tests/testJson.ur Thu Dec 02 11:59:55 2010 -0500 +++ b/tests/testJson.ur Thu Dec 02 12:17:51 2010 -0500 @@ -1,23 +1,73 @@ open Json -val json_abcd : json {A : int, B : float, C : string, D : bool} = - json_record {A = "a", B = "b", C = "c", D = "d"} +(* Example from http://en.wikipedia.org/wiki/JSON *) -fun main () : transaction page = - d <- return (fromJson "{\"a\": 1, \"b\": 2.3, \"c\": \"Hi\", \"d\": true}" : {A : int, B : float, C : string, D : bool}); - return <xml><body> - {[toJson (1 :: 2 :: 8 :: [])]}<br/> - {[fromJson "[1,2, 8]" : list int]} - <hr/> - {[toJson (1.2 :: 2.4 :: (-8.8) :: [])]}<br/> - {[fromJson "[1.4,-2.7, 8.215506]" : list float]} - <hr/> - {[toJson ("hi" :: "bye" :: "tricky\\\" one!" :: [])]}<br/> - {[fromJson "[\"abc\", \"\\\\whoa\"]" : list string]} - <hr/> - {[toJson (True :: False :: True :: [])]}<br/> - {[fromJson "[true,false, true]" : list bool]} - <hr/> - {[toJson {A = 1, B = 2.3, C = "Hi", D = True}]}<br/> - A: {[d.A]}, B: {[d.B]}, C: {[d.C]}, D: {[d.D]} - </body></xml> +type address = {StreetAddress : string, + City : string, + State : string, + PostalCode : string} + +type phoneNumber = {Type_ : string, + Number : string} + +type person = {FirstName : string, + LastName : string, + Age : int, + Address : address, + PhoneNumber : list phoneNumber} + +val sample : person = {FirstName = "Larry", + LastName = "Lambda", + Age = 42, + Address = {StreetAddress = "99 Confluence Circle", + City = "Hoserville", + State = "QQ", + PostalCode = "66666"}, + PhoneNumber = {Type_ = "mobile", Number = "1234"} + :: {Type_ = "secret", Number = "ssssh"} + :: []} + +val json_address : json address = json_record {StreetAddress = "streetAddress", + City = "city", + State = "state", + PostalCode = "postalCode"} + +val json_phoneNumber : json phoneNumber = json_record {Type_ = "type", + Number = "number"} + +val json_person : json person = json_record {FirstName = "firstName", + LastName = "lastName", + Age = "age", + Address = "address", + PhoneNumber = "phoneNumber"} + +fun renderPerson (p : person) = <xml> + <b>First name:</b> {[p.FirstName]}<br/> + <b>Last name:</b> {[p.LastName]}<br/> + <b>Age:</b> {[p.Age]}<br/> + <b>Street address:</b> {[p.Address.StreetAddress]}<br/> + <b>City:</b> {[p.Address.City]}<br/> + <b>State:</b> {[p.Address.State]}<br/> + <b>Postal code:</b> {[p.Address.PostalCode]}<br/> + <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Type_]} => {[pn.Number]}; </xml>) p.PhoneNumber}<br/> +</xml> + +fun parse r = return <xml><body> + {renderPerson (fromJson r.Text)} +</body></xml> + +fun main () = return <xml><body> + <h1>Json parsing adventure!</h1> + + <hr/> + <h2>Free sample</h2> + + {[toJson sample]} + <hr/> + + <h2>Parse your own</h2> + <form> + <textarea{#Text} rows={10} cols={80}/><br/> + <submit value="Parse" action={parse}/> + </form> +</body></xml>