JSON helpers for Loom
Loom provides the JSON class, which provides strongly typed access to values, requiring separate accessors for every data type, and two families of these accessors for retrieving from Objects or Arrays. There are 18 basic accessors, and 2 methods for determining type.
The Json
class in this library aims to simplify access to json data, using the Json
class itself as the single container type, which exposes only 3 basic accessors, and 1 more for type retrieval:
keys
- a Dictionary of Json instances, indexed by Stringsitems
- an Array of Json instancesvalue
- the actual data for the instancetype
- any basic System type, or JsonNull
,Boolean
,Number
,String
,Vector
,Dictionary
,Json
For comparison, the code snippets below present two ways to retrieve the second value of the nested array indexed by r
in the following json data:
{
"key": [
{"a":1.23, "b":45.67},
{"x":8, "y":9},
{"q":[1,2], "r":[3,4], "n":null}
],
}
json.json
var jsonString:String = File.loadTextFile('assets/json.json');
var j:JSON = JSON.parse(jsonString);
trace(j.getArray('key').getArrayObject(2).getArray('r').getArrayNumber(1));
var jsonString:String = File.loadTextFile('assets/json.json');
var j:Json = Json.fromString(jsonString);
trace(j.keys['key'].items[2].keys['r'].items[1]);
This library includes a configurable JSON pretty-printer, with three pre-defined configurations for convenience.
Other custom configurations are possible by adjusting the values of JsonPrinterOptions
.
Similar to jsonlint:
{
"array": [
1,
[
23,
45
],
[
67,
[
666
],
[
777
],
89
]
],
"bool": true,
"dictionary": {
"a": [
65,
97
],
"z": {
"A": 65,
"a": 97
}
},
"nulls": "loom dictionaries delete null values",
"number": 987.6543,
"string": "aA bB cC"
}
A tighter formatting that retains readability:
{
"array": [
1,
[ 23, 45 ],
[ 67, [ 666 ], [ 777 ], 89 ]
],
"bool": true,
"dictionary": {
"a": [ 65, 97 ],
"z": { "A": 65, "a": 97 }
},
"nulls": "loom dictionaries delete null values",
"number": 987.6543,
"string": "aA bB cC"
}
No extra whitespace:
{"array":[1,[23,45],[67,[666],[777],89]],"bool":true,"dictionary":{"a":[65,97],"z":{"A":65,"a":97}},"nulls":"loom dictionaries delete null values","number":987.6543,"string":"aA bB cC"}
This library includes a configurable YAML pretty-printer, with two pre-defined configurations for convenience:
Other custom configurations are possible by adjusting the values of YamlPrinterOptions
.
As you would find from yamllint:
---
array:
- 1
-
- 23
- 45
-
- 67
-
- 666
-
- 777
- 89
bool: true
dictionary:
a:
- 65
- 97
z:
A: 65
a: 97
nulls: "loom dictionaries delete null values"
number: 987.6543
string: "aA bB cC"
A tighter formatting similar to yaml.org:
---
array:
- 1
- [ 23, 45 ]
- [ 67, [ 666 ], [ 777 ], 89 ]
bool: true
dictionary:
a: [ 65, 97 ]
z: { A: 65, a: 97 }
nulls: "loom dictionaries delete null values"
number: 987.6543
string: "aA bB cC"
Custom formatting can be achieved by configuring the YamlPrinterOptions
parameter:
---
array:
- 1
- - 23
- 45
- - 67
- - 666
- - 777
- 89
bool: true
dictionary:
a:
- 65
- 97
z:
A: 65
a: 97
nulls: "loom dictionaries delete null values"
number: 987.6543
string: "aA bB cC"
...
see an example of using Json here:
you can compile and run the demo from the command line:
$ rake cli
Download the library into its matching sdk folder:
$ curl -L -o ~/.loom/sdks/sprint34/libs/Json.loomlib \
https://github.com/pixeldroid/json-ls/releases/download/v1.0.0/Json-sprint34.loomlib
To uninstall, simply delete the file:
$ rm ~/.loom/sdks/sprint34/libs/Json.loomlib
- declare a reference to the Json loomlib in your
.build
file: *"references": [ "System", "Json" ],
- import
pixeldroid.json.Json
- instantiate a new
pixeldroid.json.Json
and call thefromString()
orfromObject()
method on it - retrieve values for key / item chains as desired
- print to formatted JSON string with JsonPrinter
var jsonString:String = File.loadTextFile('assets/json.json');
var j:Json = Json.fromString(jsonString);
trace(j.keys['key_name'].items[2].value);
var jsonObject:Dictionary.<String, Object> = { "bool": true, "array": [1,23], "string": "one two three" };
var j:Json = Json.fromObject(jsonObject);
trace(JsonPrinter.print(j, JsonPrinterOptions.compact));
first install loomtasks
$ rake lib:install
this will build the Json library and install it in the currently configured sdk
$ rake test
this will build the Json library, install it in the currently configured sdk, build the test app, and run the test app.
Pull requests are welcome!