annotate 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
rev   line source
adam@1 1 open Json
adam@1 2
adam@3 3 (* Example from http://en.wikipedia.org/wiki/JSON *)
adam@1 4
adam@3 5 type address = {StreetAddress : string,
adam@3 6 City : string,
adam@3 7 State : string,
adam@3 8 PostalCode : string}
adam@3 9
ezyang@22 10 type phoneType = variant [Mobile = string,
ezyang@22 11 LandLine = unit,
ezyang@22 12 Secret = unit]
ezyang@22 13
ezyang@22 14 type phoneNumber = {Type_ : phoneType,
adam@3 15 Number : string}
adam@3 16
adam@3 17 type person = {FirstName : string,
adam@3 18 LastName : string,
adam@3 19 Age : int,
adam@3 20 Address : address,
adam@3 21 PhoneNumber : list phoneNumber}
adam@3 22
adam@3 23 val sample : person = {FirstName = "Larry",
adam@3 24 LastName = "Lambda",
adam@3 25 Age = 42,
adam@3 26 Address = {StreetAddress = "99 Confluence Circle",
adam@3 27 City = "Hoserville",
adam@3 28 State = "QQ",
adam@3 29 PostalCode = "66666"},
ezyang@22 30 PhoneNumber = {Type_ = make [#Mobile] "Verizon", Number = "1234"}
ezyang@22 31 :: {Type_ = make [#Secret] (), Number = "ssssh"}
adam@3 32 :: []}
adam@3 33
adam@3 34 val json_address : json address = json_record {StreetAddress = "streetAddress",
adam@3 35 City = "city",
adam@3 36 State = "state",
adam@3 37 PostalCode = "postalCode"}
adam@3 38
ezyang@22 39 val json_phoneType : json phoneType = json_variant {Mobile = "mobile",
ezyang@22 40 LandLine = "landline",
ezyang@22 41 Secret = "secret"}
ezyang@22 42
adam@3 43 val json_phoneNumber : json phoneNumber = json_record {Type_ = "type",
adam@3 44 Number = "number"}
adam@3 45
adam@3 46 val json_person : json person = json_record {FirstName = "firstName",
adam@3 47 LastName = "lastName",
adam@3 48 Age = "age",
adam@3 49 Address = "address",
adam@3 50 PhoneNumber = "phoneNumber"}
adam@3 51
adam@3 52 fun renderPerson (p : person) = <xml>
adam@3 53 <b>First name:</b> {[p.FirstName]}<br/>
adam@3 54 <b>Last name:</b> {[p.LastName]}<br/>
adam@3 55 <b>Age:</b> {[p.Age]}<br/>
adam@3 56 <b>Street address:</b> {[p.Address.StreetAddress]}<br/>
adam@3 57 <b>City:</b> {[p.Address.City]}<br/>
adam@3 58 <b>State:</b> {[p.Address.State]}<br/>
adam@3 59 <b>Postal code:</b> {[p.Address.PostalCode]}<br/>
ezyang@22 60 <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Number]}; </xml>) p.PhoneNumber}<br/>
adam@3 61 </xml>
adam@3 62
adam@3 63 fun parse r = return <xml><body>
adam@3 64 {renderPerson (fromJson r.Text)}
adam@3 65 </body></xml>
adam@3 66
adam@3 67 fun main () = return <xml><body>
adam@3 68 <h1>Json parsing adventure!</h1>
adam@3 69
adam@3 70 <hr/>
adam@3 71 <h2>Free sample</h2>
adam@3 72
adam@3 73 {[toJson sample]}
adam@3 74 <hr/>
adam@3 75
adam@3 76 <h2>Parse your own</h2>
adam@3 77 <form>
adam@3 78 <textarea{#Text} rows={10} cols={80}/><br/>
adam@3 79 <submit value="Parse" action={parse}/>
adam@3 80 </form>
adam@3 81 </body></xml>