// //------------------ DOK FILE -------------------------------------------------- +Header core.util.json =pkg core.util.json =title core.util.json =short json format =desc This library contains functions to manipulate json representations. fun testJson ()= let load ("test.json") -> src in let jsonParse (src, 0) -> [json next] in ( consoleTime next; echoLn jsonEncodePretty (json) );; // // //------------------ PROTO ----------------------------------------------------- jsonEncodePretty +Proto jsonEncodePretty =type fun Json -> Str =mode function =pkg core.util.json =impl mcy =link =desc This function returns a string with the json representation of the supplied value. For readability it adds spaces and carriage returns. // +arg json =type Json =desc Any json value // +arg result =type Str =desc A string // // //------------------ PROTO ----------------------------------------------------- jsonEncode +Proto jsonEncode =type fun Json -> Str =mode function =pkg core.util.json =impl mcy =link =desc This function returns a string with the json representation of the supplied value. The result is 'compact', without any space or carriage return. // +arg json =type Json =desc Any json value // +arg result =type Str =desc A string // // //------------------ PROTO ----------------------------------------------------- jsonParse +Proto jsonParse =type fun Str Int -> [Json Int] =mode function =pkg core.util.json =impl mcy =link =desc This function assumes that src contains a json representation starting at position index. It throws an exception when it is not the case. When it works, it returns a tuple with the [[Json]] value and the index after the last char read from src. // +arg src =type Str =desc A string with a json representation. // +arg index =type Int =desc An index // +arg result =type [Json Int] =desc A tuple [json_value next_index] // // //------------------ PROTO ----------------------------------------------------- jsonFieldGet +Proto jsonFieldGet =type fun Json Str -> Json =mode function =pkg core.util.json =impl mcy =link =desc This function is a field extractor. It returns nil if the json value wasn't a json object or if this object has no field liked fieldName. // +arg json =type Json =desc Any json value // +arg fieldName =type Str =desc A string // +arg result =type Json =desc A json value or nil // // //------------------ PROTO ----------------------------------------------------- jsonArrayGet +Proto jsonArrayGet =type fun Json -> array Json =mode function =pkg core.util.json =impl mcy =link =desc This function is an array extractor. It returns nil if the json value wasn't a json array. // +arg json =type Json =desc Any json value // +arg result =type array Json =desc An array of json values or nil // // //------------------ PROTO ----------------------------------------------------- jsonObjectGet +Proto jsonObjectGet =type fun Json -> list [Str Json] =mode function =pkg core.util.json =impl mcy =link =desc This function is an object extractor. It returns nil if the json value wasn't a json object. // +arg json =type Json =desc Any json value // +arg result =type list [Str Json] =desc A list of tuples [key value], or nil // // //------------------ PROTO ----------------------------------------------------- jsonStringGet +Proto jsonStringGet =type fun Json -> Str =mode function =pkg core.util.json =impl mcy =link =desc This function is a string extractor. It returns nil if the json value wasn't a json string. // +arg json =type Json =desc Any json value // +arg result =type Str =desc A string or nil // // //------------------ PROTO ----------------------------------------------------- jsonBoolGet +Proto jsonBoolGet =type fun Json -> Bool =mode function =pkg core.util.json =impl mcy =link =desc This function is a boolean extractor. It returns nil if the json value wasn't a json boolean. // +arg json =type Json =desc Any json value // +arg result =type Bool =desc A boolean or nil // // //------------------ PROTO ----------------------------------------------------- jsonIntGet +Proto jsonIntGet =type fun Json -> Int =mode function =pkg core.util.json =impl mcy =link =desc This function is a int extractor. It returns nil if the json value wasn't a json number. As json numbers are floats, the function converts the float into an integer. // +arg json =type Json =desc Any json value // +arg result =type Int =desc An integer or nil // // //------------------ PROTO ----------------------------------------------------- jsonNumberGet +Proto jsonNumberGet =type fun Json -> Float =mode function =pkg core.util.json =impl mcy =link =desc This function is a number extractor. It returns nil if the json value wasn't a json number. // +arg json =type Json =desc Any json value // +arg result =type Float =desc A float or nil // // //------------------ PROTO ----------------------------------------------------- jsonObject +Proto jsonObject =type fun list [Str Json] -> Json =mode constructor =pkg core.util.json =impl mcy =link =desc This is the constructor for json objects. The object content is a list of tuples [key value] where key is a string and value has [[Json]] type. // // //------------------ PROTO ----------------------------------------------------- jsonArray +Proto jsonArray =type fun array Json -> Json =mode constructor =pkg core.util.json =impl mcy =link =desc This is the constructor for json arrays. // // //------------------ PROTO ----------------------------------------------------- jsonFalse +Proto jsonFalse =type Json =mode empty constructor =pkg core.util.json =impl mcy =link =desc This is the 'false' value of json format. // // //------------------ PROTO ----------------------------------------------------- jsonTrue +Proto jsonTrue =type Json =mode empty constructor =pkg core.util.json =impl mcy =link =desc This is the 'true' value of json format. // // //------------------ PROTO ----------------------------------------------------- jsonNull +Proto jsonNull =type Json =mode empty constructor =pkg core.util.json =impl mcy =link =desc This is the 'null' value of json format. // // //------------------ PROTO ----------------------------------------------------- jsonString +Proto jsonString =type fun Str -> Json =mode constructor =pkg core.util.json =impl mcy =link =desc This is the constructor for json strings. // // //------------------ PROTO ----------------------------------------------------- jsonNumber +Proto jsonNumber =type fun Float -> Json =mode constructor =pkg core.util.json =impl mcy =link =desc This is the constructor for json numbers. Note that json numbers are floats. // // //------------------ PROTO ----------------------------------------------------- Json +Proto Json =type Json =mode sum =pkg core.util.json =impl mcy =link =desc The Json type is a sum type. // // //------------------ PROTO ----------------------------------------------------- jsonBool +Proto jsonBool =type fun Bool -> Json =mode function =pkg core.util.json =impl mcy =link =desc This function returns [[jsonTrue]] or [[jsonFalse]]. // +arg value =type Bool =desc A boolean // +arg result =type Json =desc A json value // // //------------------ PROTO ----------------------------------------------------- jsonInt +Proto jsonInt =type fun Int -> Json =mode function =pkg core.util.json =impl mcy =link =desc This function returns a [[jsonNumber]] value. In json, numbers are floats. This explains why the constructor [[jsonNumber]] needs a float. This [[jsonInt]] function converts the integer value into a float and use it to build a [[jsonNumber]]. // +arg value =type Int =desc An integer // +arg result =type Json =desc A json value // // //------------------ PROTO ----------------------------------------------------- jsonListGet +Proto jsonListGet =type fun Json -> list Json =mode Function =pkg core.util.json =impl mcy =link =desc Json implements objects and arrays. However Minimacy prefers lists. This function returns a list instead of an array when json is an array. // +arg json =type Json =desc Any Json value // +arg result =type list Json =desc // // //------------------ PROTO ----------------------------------------------------- jsonList +Proto jsonList =type fun list Json -> Json =mode Function =pkg core.util.json =impl mcy =link =desc This is the constructor for json arrays from a list of Json elements. // +arg input =type list Json =desc a list of Json elements // +arg result =type Json =desc a Json array