Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/releases
bin/
lib/build/
logs/
lib/build
test/bin
releases/
test/bin/
TEST-*.xml
25 changes: 12 additions & 13 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
The MIT License (MIT)

Copyright (c) pixeldroid
https://github.com/pixeldroid/json-ls

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ json-ls

JSON helpers for Loom

- `Json` - a utility to simplify json data access
- `JsonPrinter` - a pretty printer for json data


## installation

Download the library into its matching sdk folder:

$ curl -L -o ~/.loom/sdks/sprint33/libs/Json.loomlib \
https://github.com/pixeldroid/json-ls/releases/download/v0.0.1/Json-sprint33.loomlib
$ curl -L -o ~/.loom/sdks/sprint34/libs/Json.loomlib \
https://github.com/pixeldroid/json-ls/releases/download/v0.0.2/Json-sprint34.loomlib

To uninstall, simply delete the file:

$ rm ~/.loom/sdks/sprint33/libs/Json.loomlib
$ rm ~/.loom/sdks/sprint34/libs/Json.loomlib


## usage
Expand Down Expand Up @@ -43,6 +46,49 @@ var j:Json = Json.fromObject(jsonObject);
trace(JsonPrinter.print(j, JsonPrinterOptions.compact));
```

### Json

Loom provides the [JSON][loom-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:

```json
{
"key": [
{"a":1.23, "b":45.67},
{"x":8, "y":9},
{"q":[1,2], "r":[3,4], "n":null}
],
}
```

> `json.json`

#### Using `system.JSON`

```ls
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`

```ls
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:
Expand Down Expand Up @@ -116,5 +162,6 @@ Pull requests are welcome!


[loomtasks]: https://github.com/pixeldroid/loomtasks "loomtasks"
[loom-json]: http://docs.theengine.co/loom/1.1.4813/api/system/JSON.html "Loom JSON class"
[JsonDemo.build]: ./test/src/JsonDemo.build "build file for the demo"
[JsonDemo.ls]: ./test/src/JsonDemo.ls "source file for the demo"
2 changes: 1 addition & 1 deletion lib/loom.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"sdk_version": "sprint33"
"sdk_version": "sprint34"
}
4 changes: 2 additions & 2 deletions lib/src/pixeldroid/json/Json.ls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package pixeldroid.json

public class Json
{
public static const version:String = '0.0.1';
public static const version:String = '0.0.2';

static public function fromString(value:String):Json
{
Expand Down Expand Up @@ -66,7 +66,7 @@ package pixeldroid.json
default :
if (item.hasOwnProperty('toJson'))
{
var method:MethodInfo = item.getType().getMethodInfo('toJson');
var method:MethodInfo = item.getType().getMethodInfoByName('toJson');
if (method) json = method.invokeSingle(item, null) as Json;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions test/loom.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk_version": "sprint33",
"sdk_version": "sprint34",
"executable": "Main.loom",
"display": {
"width": 480,
Expand All @@ -11,4 +11,4 @@
"app_id": "pixeldroid.JsonTest",
"app_name": "JsonTest",
"app_version": "0.0.1"
}
}
14 changes: 10 additions & 4 deletions test/src/app/JsonTest.ls
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package
{

import system.Process;
import system.application.ConsoleApplication;

import pixeldroid.bdd.Spec;
Expand All @@ -16,23 +17,28 @@ package

public class JsonTest extends ConsoleApplication
{
private var seed:Number = -1;
private const SUCCESS:Number = 0;
private const FAILURE:Number = 1;

override public function run():void
{
JsonSpec.describe();
JsonPrinterSpec.describe();

addReporters();
parseArgs();

Spec.execute();
Process.exit(Spec.execute(seed) ? SUCCESS : FAILURE);
}

private function addReporters():void
private function parseArgs():void
{
var arg:String;
for (var i = 2; i < CommandLine.getArgCount(); i++)
for (var i = 0; i < CommandLine.getArgCount(); i++)
{
arg = CommandLine.getArg(i);
if (arg == '--format') Spec.addReporter(reporterByName(CommandLine.getArg(++i)));
if (arg == '--seed') seed = Number.fromString(CommandLine.getArg(++i));
}

if (Spec.numReporters == 0) Spec.addReporter(new ConsoleReporter());
Expand Down