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@23: 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:
ezyang@23: ezyang@23: structure God = Json.Recursive(struct ezyang@23: con t a = variant [Fun = string * list a, ezyang@23: Var = string] ezyang@23: ezyang@23: fun json_t [a] (_ : json a) : json (t a) = ezyang@23: let ezyang@23: val json_fun : json (string * list a) = json_record ("1", "2") ezyang@23: in ezyang@23: json_variant {Fun = "Fun", Var = "Var"} ezyang@23: end ezyang@23: end) ezyang@23: ezyang@23: fun renderGod (God.Rec g) = ezyang@23: match g ezyang@23: {Fun = fn (s, gs) => ezyang@23: Main god: {[s]}
ezyang@23: Subgods: ezyang@23:
, ezyang@23: Var = fn s => ezyang@23: Var: {[s]} ezyang@23: } ezyang@23: ezyang@23: fun parseGod r = return ezyang@23:

Beautified

ezyang@23: {renderGod (fromJson r.Text)} ezyang@23: ezyang@23:

Round-tripped

ezyang@23: {[toJson (fromJson r.Text : God.r)]} ezyang@23:
ezyang@23: ezyang@23: fun godMain () = return ezyang@23:

Parse ye gods

ezyang@23: ezyang@23:
ezyang@23:
ezyang@23: ezyang@23: ezyang@23: