annotate tests/testJson.ur @ 12:a6730c3cfea7

Record.equal
author Adam Chlipala <adam@chlipala.net>
date Tue, 08 Feb 2011 16:52:41 -0500
parents 189245a3c075
children 8de201d70b91
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
adam@3 10 type phoneNumber = {Type_ : string,
adam@3 11 Number : string}
adam@3 12
adam@3 13 type person = {FirstName : string,
adam@3 14 LastName : string,
adam@3 15 Age : int,
adam@3 16 Address : address,
adam@3 17 PhoneNumber : list phoneNumber}
adam@3 18
adam@3 19 val sample : person = {FirstName = "Larry",
adam@3 20 LastName = "Lambda",
adam@3 21 Age = 42,
adam@3 22 Address = {StreetAddress = "99 Confluence Circle",
adam@3 23 City = "Hoserville",
adam@3 24 State = "QQ",
adam@3 25 PostalCode = "66666"},
adam@3 26 PhoneNumber = {Type_ = "mobile", Number = "1234"}
adam@3 27 :: {Type_ = "secret", Number = "ssssh"}
adam@3 28 :: []}
adam@3 29
adam@3 30 val json_address : json address = json_record {StreetAddress = "streetAddress",
adam@3 31 City = "city",
adam@3 32 State = "state",
adam@3 33 PostalCode = "postalCode"}
adam@3 34
adam@3 35 val json_phoneNumber : json phoneNumber = json_record {Type_ = "type",
adam@3 36 Number = "number"}
adam@3 37
adam@3 38 val json_person : json person = json_record {FirstName = "firstName",
adam@3 39 LastName = "lastName",
adam@3 40 Age = "age",
adam@3 41 Address = "address",
adam@3 42 PhoneNumber = "phoneNumber"}
adam@3 43
adam@3 44 fun renderPerson (p : person) = <xml>
adam@3 45 <b>First name:</b> {[p.FirstName]}<br/>
adam@3 46 <b>Last name:</b> {[p.LastName]}<br/>
adam@3 47 <b>Age:</b> {[p.Age]}<br/>
adam@3 48 <b>Street address:</b> {[p.Address.StreetAddress]}<br/>
adam@3 49 <b>City:</b> {[p.Address.City]}<br/>
adam@3 50 <b>State:</b> {[p.Address.State]}<br/>
adam@3 51 <b>Postal code:</b> {[p.Address.PostalCode]}<br/>
adam@3 52 <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Type_]} => {[pn.Number]}; </xml>) p.PhoneNumber}<br/>
adam@3 53 </xml>
adam@3 54
adam@3 55 fun parse r = return <xml><body>
adam@3 56 {renderPerson (fromJson r.Text)}
adam@3 57 </body></xml>
adam@3 58
adam@3 59 fun main () = return <xml><body>
adam@3 60 <h1>Json parsing adventure!</h1>
adam@3 61
adam@3 62 <hr/>
adam@3 63 <h2>Free sample</h2>
adam@3 64
adam@3 65 {[toJson sample]}
adam@3 66 <hr/>
adam@3 67
adam@3 68 <h2>Parse your own</h2>
adam@3 69 <form>
adam@3 70 <textarea{#Text} rows={10} cols={80}/><br/>
adam@3 71 <submit value="Parse" action={parse}/>
adam@3 72 </form>
adam@3 73 </body></xml>