Misty Programming Language:

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 objects as colon and comma-separated name:value pairs within braces.

The Misty Programming Language contains a superset of this notation.

The object literals and array literals in Misty source programs can have these additional extensions:

The methods text.object(), text.array(), object.text(), and array.text() support these extensions:

So, for example,

var a : []
a[0] : a
value : a.text()    # value is "[^0]"

JSON Methods

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

json.parse(text, reviver)

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

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

myData : json.parse(text, method (key, value) 
    key.find('date') then time.number(value) else value)

json.path(object, path)

The object is queried by the JSON path text. See http://goessner.net/articles/JsonPath/ for a description of JSONPath.

It returns the result of the path query.

var my_object = { "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      }
}};
	  
var titles = json.path(my_object, "$.store.book..title");	 

# titles is ["Sayings of the Century", "Sword of Honour", "Moby Dick"]

json.text(value, replacer, space)

Produce a JSON text from a Misty value. White space will be included to make the text more human readable. If an object value, at any level, contains a json() method, it will be called, and the value it returns (usually a simpler object) will be JSONified.

If the object does not have a json() method, and if replacer is an array, then only the keys that are listed in the array will be included.

If there is an option space parameter, and if it is a text, then line breaks and extra whitespace will be included in the text, using the space text for indentation. Typically, this is '    ' or '\t'. If the optional space parameter is a number, then it specifies the number of space characters to use for each level of indentation. If space is null, then the text will packed with no whitespace. If space is not a text, number, or null, then a 'type' exception is raised.

myText : json.text(myData, method (key, value) 
    key.find('date') then time.text(value) else value