comparison tests/testJson.ur @ 22:8de201d70b91

Implement JSON typeclass for polymorphic variants.
author Edward Z. Yang <ezyang@mit.edu>
date Wed, 02 May 2012 11:47:37 -0400
parents 189245a3c075
children 9d6b931fbd13
comparison
equal deleted inserted replaced
21:e7d64ea0f922 22:8de201d70b91
5 type address = {StreetAddress : string, 5 type address = {StreetAddress : string,
6 City : string, 6 City : string,
7 State : string, 7 State : string,
8 PostalCode : string} 8 PostalCode : string}
9 9
10 type phoneNumber = {Type_ : string, 10 type phoneType = variant [Mobile = string,
11 LandLine = unit,
12 Secret = unit]
13
14 type phoneNumber = {Type_ : phoneType,
11 Number : string} 15 Number : string}
12 16
13 type person = {FirstName : string, 17 type person = {FirstName : string,
14 LastName : string, 18 LastName : string,
15 Age : int, 19 Age : int,
21 Age = 42, 25 Age = 42,
22 Address = {StreetAddress = "99 Confluence Circle", 26 Address = {StreetAddress = "99 Confluence Circle",
23 City = "Hoserville", 27 City = "Hoserville",
24 State = "QQ", 28 State = "QQ",
25 PostalCode = "66666"}, 29 PostalCode = "66666"},
26 PhoneNumber = {Type_ = "mobile", Number = "1234"} 30 PhoneNumber = {Type_ = make [#Mobile] "Verizon", Number = "1234"}
27 :: {Type_ = "secret", Number = "ssssh"} 31 :: {Type_ = make [#Secret] (), Number = "ssssh"}
28 :: []} 32 :: []}
29 33
30 val json_address : json address = json_record {StreetAddress = "streetAddress", 34 val json_address : json address = json_record {StreetAddress = "streetAddress",
31 City = "city", 35 City = "city",
32 State = "state", 36 State = "state",
33 PostalCode = "postalCode"} 37 PostalCode = "postalCode"}
38
39 val json_phoneType : json phoneType = json_variant {Mobile = "mobile",
40 LandLine = "landline",
41 Secret = "secret"}
34 42
35 val json_phoneNumber : json phoneNumber = json_record {Type_ = "type", 43 val json_phoneNumber : json phoneNumber = json_record {Type_ = "type",
36 Number = "number"} 44 Number = "number"}
37 45
38 val json_person : json person = json_record {FirstName = "firstName", 46 val json_person : json person = json_record {FirstName = "firstName",
47 <b>Age:</b> {[p.Age]}<br/> 55 <b>Age:</b> {[p.Age]}<br/>
48 <b>Street address:</b> {[p.Address.StreetAddress]}<br/> 56 <b>Street address:</b> {[p.Address.StreetAddress]}<br/>
49 <b>City:</b> {[p.Address.City]}<br/> 57 <b>City:</b> {[p.Address.City]}<br/>
50 <b>State:</b> {[p.Address.State]}<br/> 58 <b>State:</b> {[p.Address.State]}<br/>
51 <b>Postal code:</b> {[p.Address.PostalCode]}<br/> 59 <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/> 60 <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Number]}; </xml>) p.PhoneNumber}<br/>
53 </xml> 61 </xml>
54 62
55 fun parse r = return <xml><body> 63 fun parse r = return <xml><body>
56 {renderPerson (fromJson r.Text)} 64 {renderPerson (fromJson r.Text)}
57 </body></xml> 65 </body></xml>