diff --git a/.gitignore b/.gitignore index 768cae4..df723dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -/releases +bin/ +lib/build/ logs/ -lib/build -test/bin +releases/ +test/bin/ TEST-*.xml diff --git a/LICENSE b/LICENSE index 183d7ca..8105a86 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md index 7f8ab52..bb9efd7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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" diff --git a/lib/loom.config b/lib/loom.config index 12977d9..345d99b 100644 --- a/lib/loom.config +++ b/lib/loom.config @@ -1,3 +1,3 @@ { - "sdk_version": "sprint33" + "sdk_version": "sprint34" } \ No newline at end of file diff --git a/lib/src/pixeldroid/json/Json.ls b/lib/src/pixeldroid/json/Json.ls index b18aed7..b063ddf 100644 --- a/lib/src/pixeldroid/json/Json.ls +++ b/lib/src/pixeldroid/json/Json.ls @@ -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 { @@ -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; diff --git a/test/loom.config b/test/loom.config index 89957d1..1d34867 100644 --- a/test/loom.config +++ b/test/loom.config @@ -1,5 +1,5 @@ { - "sdk_version": "sprint33", + "sdk_version": "sprint34", "executable": "Main.loom", "display": { "width": 480, @@ -11,4 +11,4 @@ "app_id": "pixeldroid.JsonTest", "app_name": "JsonTest", "app_version": "0.0.1" -} +} \ No newline at end of file diff --git a/test/src/app/JsonTest.ls b/test/src/app/JsonTest.ls index e8fb42b..b88889a 100644 --- a/test/src/app/JsonTest.ls +++ b/test/src/app/JsonTest.ls @@ -2,6 +2,7 @@ package { + import system.Process; import system.application.ConsoleApplication; import pixeldroid.bdd.Spec; @@ -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());