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.
, (comma) before the closing ]
(right bracket) or } (right brace)
is ignored.The object literals and array literals in Misty source programs can have these additional extensions:
The methods string.object(), string.array(),
object.string(), and array.string()
support these extensions:
true or false (with ^
(circumflex) prefix).^number
notation which works as follows:
Each array[(left bracket)and object{(left brace) is given a number, starting with zero. When an array or object recurs, it is represented as a^(circumflex) followed by the number of the first occurrence.
So, for example,
var a := [] a[0] := a value := a.string() # value is "[^0]"
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 string 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 string. 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.string(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 string,
then line breaks and extra whitespace will be included in the text, using
the space string 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 string, number, or null,
then a 'type' exception is raised.
myText := json.string(myData, method (key, value)
key.find('date') then time.string(value) else value