comparison tests/testJson.ur @ 3:189245a3c075

Wikipedia JSON example working
author Adam Chlipala <adam@chlipala.net>
date Thu, 02 Dec 2010 12:17:51 -0500
parents 478524b9d23a
children 8de201d70b91
comparison
equal deleted inserted replaced
2:478524b9d23a 3:189245a3c075
1 open Json 1 open Json
2 2
3 val json_abcd : json {A : int, B : float, C : string, D : bool} = 3 (* Example from http://en.wikipedia.org/wiki/JSON *)
4 json_record {A = "a", B = "b", C = "c", D = "d"}
5 4
6 fun main () : transaction page = 5 type address = {StreetAddress : string,
7 d <- return (fromJson "{\"a\": 1, \"b\": 2.3, \"c\": \"Hi\", \"d\": true}" : {A : int, B : float, C : string, D : bool}); 6 City : string,
8 return <xml><body> 7 State : string,
9 {[toJson (1 :: 2 :: 8 :: [])]}<br/> 8 PostalCode : string}
10 {[fromJson "[1,2, 8]" : list int]} 9
11 <hr/> 10 type phoneNumber = {Type_ : string,
12 {[toJson (1.2 :: 2.4 :: (-8.8) :: [])]}<br/> 11 Number : string}
13 {[fromJson "[1.4,-2.7, 8.215506]" : list float]} 12
14 <hr/> 13 type person = {FirstName : string,
15 {[toJson ("hi" :: "bye" :: "tricky\\\" one!" :: [])]}<br/> 14 LastName : string,
16 {[fromJson "[\"abc\", \"\\\\whoa\"]" : list string]} 15 Age : int,
17 <hr/> 16 Address : address,
18 {[toJson (True :: False :: True :: [])]}<br/> 17 PhoneNumber : list phoneNumber}
19 {[fromJson "[true,false, true]" : list bool]} 18
20 <hr/> 19 val sample : person = {FirstName = "Larry",
21 {[toJson {A = 1, B = 2.3, C = "Hi", D = True}]}<br/> 20 LastName = "Lambda",
22 A: {[d.A]}, B: {[d.B]}, C: {[d.C]}, D: {[d.D]} 21 Age = 42,
23 </body></xml> 22 Address = {StreetAddress = "99 Confluence Circle",
23 City = "Hoserville",
24 State = "QQ",
25 PostalCode = "66666"},
26 PhoneNumber = {Type_ = "mobile", Number = "1234"}
27 :: {Type_ = "secret", Number = "ssssh"}
28 :: []}
29
30 val json_address : json address = json_record {StreetAddress = "streetAddress",
31 City = "city",
32 State = "state",
33 PostalCode = "postalCode"}
34
35 val json_phoneNumber : json phoneNumber = json_record {Type_ = "type",
36 Number = "number"}
37
38 val json_person : json person = json_record {FirstName = "firstName",
39 LastName = "lastName",
40 Age = "age",
41 Address = "address",
42 PhoneNumber = "phoneNumber"}
43
44 fun renderPerson (p : person) = <xml>
45 <b>First name:</b> {[p.FirstName]}<br/>
46 <b>Last name:</b> {[p.LastName]}<br/>
47 <b>Age:</b> {[p.Age]}<br/>
48 <b>Street address:</b> {[p.Address.StreetAddress]}<br/>
49 <b>City:</b> {[p.Address.City]}<br/>
50 <b>State:</b> {[p.Address.State]}<br/>
51 <b>Postal code:</b> {[p.Address.PostalCode]}<br/>
52 <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Type_]} => {[pn.Number]}; </xml>) p.PhoneNumber}<br/>
53 </xml>
54
55 fun parse r = return <xml><body>
56 {renderPerson (fromJson r.Text)}
57 </body></xml>
58
59 fun main () = return <xml><body>
60 <h1>Json parsing adventure!</h1>
61
62 <hr/>
63 <h2>Free sample</h2>
64
65 {[toJson sample]}
66 <hr/>
67
68 <h2>Parse your own</h2>
69 <form>
70 <textarea{#Text} rows={10} cols={80}/><br/>
71 <submit value="Parse" action={parse}/>
72 </form>
73 </body></xml>