Skip to content

pixeldroid/json-ls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-ls

JSON helpers for Loom

rationale

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 Strings
  • items - an Array of Json instances
  • value - the actual data for the instance
  • type - any basic System type, or Json
    • Null, 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

Using system.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));

Using pixeldroid.json.Json

var jsonString:String = File.loadTextFile('assets/json.json');
var j:Json = Json.fromString(jsonString);
trace(j.keys['key'].items[2].keys['r'].items[1]);

JsonPrinter

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.

Standard

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"
}

Compact

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"
}

Minified

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"}

YamlPrinter

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.

Standard

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"

Compact

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

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"
...

JsonDemo

see an example of using Json here:

you can compile and run the demo from the command line:

$ rake cli

installation

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

usage

  1. declare a reference to the Json loomlib in your .build file: *
    "references": [
        "System",
        "Json"
    ],
  2. import pixeldroid.json.Json
  3. instantiate a new pixeldroid.json.Json and call the fromString() or fromObject() method on it
  4. retrieve values for key / item chains as desired
  5. 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));

building

first install loomtasks

compiling

$ rake lib:install

this will build the Json library and install it in the currently configured sdk

running tests

$ rake test

this will build the Json library, install it in the currently configured sdk, build the test app, and run the test app.

contributing

Pull requests are welcome!