# HG changeset patch # User Adam Chlipala # Date 1291310271 18000 # Node ID 189245a3c075de054976e958e100c1b2cead17ab # Parent 478524b9d23afcd550a8d2a9b63679c86b6c6b81 Wikipedia JSON example working diff -r 478524b9d23a -r 189245a3c075 json.ur --- 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 JSON record doesn't begin with brace 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 => diff -r 478524b9d23a -r 189245a3c075 tests/testJson.ur --- 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 - {[toJson (1 :: 2 :: 8 :: [])]}
- {[fromJson "[1,2, 8]" : list int]} -
- {[toJson (1.2 :: 2.4 :: (-8.8) :: [])]}
- {[fromJson "[1.4,-2.7, 8.215506]" : list float]} -
- {[toJson ("hi" :: "bye" :: "tricky\\\" one!" :: [])]}
- {[fromJson "[\"abc\", \"\\\\whoa\"]" : list string]} -
- {[toJson (True :: False :: True :: [])]}
- {[fromJson "[true,false, true]" : list bool]} -
- {[toJson {A = 1, B = 2.3, C = "Hi", D = True}]}
- A: {[d.A]}, B: {[d.B]}, C: {[d.C]}, D: {[d.D]} -
+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) = + First name: {[p.FirstName]}
+ Last name: {[p.LastName]}
+ Age: {[p.Age]}
+ Street address: {[p.Address.StreetAddress]}
+ City: {[p.Address.City]}
+ State: {[p.Address.State]}
+ Postal code: {[p.Address.PostalCode]}
+ Phone numbers: {List.mapX (fn pn => {[pn.Type_]} => {[pn.Number]}; ) p.PhoneNumber}
+
+ +fun parse r = return + {renderPerson (fromJson r.Text)} + + +fun main () = return +

Json parsing adventure!

+ +
+

Free sample

+ + {[toJson sample]} +
+ +

Parse your own

+
+
+ + +
diff -r 478524b9d23a -r 189245a3c075 tests/testJson.urs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/testJson.urs Thu Dec 02 12:17:51 2010 -0500 @@ -0,0 +1,1 @@ +val main : {} -> transaction page