adam@1: open Json
adam@1:
adam@3: (* Example from http://en.wikipedia.org/wiki/JSON *)
adam@1:
adam@3: type address = {StreetAddress : string,
adam@3: City : string,
adam@3: State : string,
adam@3: PostalCode : string}
adam@3:
adam@3: type phoneNumber = {Type_ : string,
adam@3: Number : string}
adam@3:
adam@3: type person = {FirstName : string,
adam@3: LastName : string,
adam@3: Age : int,
adam@3: Address : address,
adam@3: PhoneNumber : list phoneNumber}
adam@3:
adam@3: val sample : person = {FirstName = "Larry",
adam@3: LastName = "Lambda",
adam@3: Age = 42,
adam@3: Address = {StreetAddress = "99 Confluence Circle",
adam@3: City = "Hoserville",
adam@3: State = "QQ",
adam@3: PostalCode = "66666"},
adam@3: PhoneNumber = {Type_ = "mobile", Number = "1234"}
adam@3: :: {Type_ = "secret", Number = "ssssh"}
adam@3: :: []}
adam@3:
adam@3: val json_address : json address = json_record {StreetAddress = "streetAddress",
adam@3: City = "city",
adam@3: State = "state",
adam@3: PostalCode = "postalCode"}
adam@3:
adam@3: val json_phoneNumber : json phoneNumber = json_record {Type_ = "type",
adam@3: Number = "number"}
adam@3:
adam@3: val json_person : json person = json_record {FirstName = "firstName",
adam@3: LastName = "lastName",
adam@3: Age = "age",
adam@3: Address = "address",
adam@3: PhoneNumber = "phoneNumber"}
adam@3:
adam@3: fun renderPerson (p : person) =
adam@3: First name: {[p.FirstName]}
adam@3: Last name: {[p.LastName]}
adam@3: Age: {[p.Age]}
adam@3: Street address: {[p.Address.StreetAddress]}
adam@3: City: {[p.Address.City]}
adam@3: State: {[p.Address.State]}
adam@3: Postal code: {[p.Address.PostalCode]}
adam@3: Phone numbers: {List.mapX (fn pn => {[pn.Type_]} => {[pn.Number]}; ) p.PhoneNumber}
adam@3:
adam@3:
adam@3: fun parse r = return
adam@3: {renderPerson (fromJson r.Text)}
adam@3:
adam@3:
adam@3: fun main () = return
adam@3: Json parsing adventure!
adam@3:
adam@3:
adam@3: Free sample
adam@3:
adam@3: {[toJson sample]}
adam@3:
adam@3:
adam@3: Parse your own
adam@3:
adam@3: