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: ezyang@22: type phoneType = variant [Mobile = string, ezyang@22: LandLine = unit, ezyang@22: Secret = unit] ezyang@22: ezyang@22: type phoneNumber = {Type_ : phoneType, 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"}, ezyang@22: PhoneNumber = {Type_ = make [#Mobile] "Verizon", Number = "1234"} ezyang@22: :: {Type_ = make [#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: ezyang@22: val json_phoneType : json phoneType = json_variant {Mobile = "mobile", ezyang@22: LandLine = "landline", ezyang@22: Secret = "secret"} ezyang@22: 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]}
ezyang@22: Phone numbers: {List.mapX (fn pn => {[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:
adam@3: adam@3: adam@3: