Misty Programming Language:JSON

JSON (JavaScript Object Notation) is a compact, portable, standard data representation language. See JSON.org. It represents arrays as comma-separated values within brackets, and records as comma-separated name:value pairs within braces.

def json: module json()

JSON Methods

Misty also provides a json record that contains functions that process JSON text strictly. Strict JSON has better security and interoperability properties.

json.decode(text reviver)

The text text is parsed, and the resulting value (usually a record or an array) is returned.

The optional reviver input is a function that is called for every key and value at every level of the result. Each value is replaced by the result of the reviver function. This can be used to reform data-only records into method-bearing records, or to transform date strings into seconds.

assign my_data: json.decode(
    text
    function (key value) {
        return (
            search(key "date") <> null 
            then time.number(value)
            else value
        )
    }
)

json.encode(value replacer whitelist)

Produce a JSON text from a Misty value. For each record in the value, at any level, if it contains a json() function, the function is called, and the return value it returns (usually a simpler record) is JSONified instead of the original record.

If the record does not have a json() method, and if whitelist is a record, then only the keys that are associated with true in the whitelist are included.

var my_text: json.encode(
    my_record
    function (key value) {
        return (
            search(key "date") <> null 
            then time.text(value)
            else value
        )
    }
)

json.pretty(value replacer whitelist)

This is like encode except that line breaks and whitespace are inserted into the result to make it more readable.