Skip to content

Commit

Permalink
Merge pull request #4 from ellemenno/v1.0.0
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
pixeldroid authored Sep 19, 2017
2 parents f3476f1 + a4441b9 commit 3a153c4
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 68 deletions.
145 changes: 135 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,47 @@ 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:
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

As you would find from jsonlint:
Similar to [jsonlint][jsonlint]:

```json
{
"array": [
1,
23
[
23,
45
],
[
67,
[
666
],
[
777
],
89
]
],
"bool": true,
"string": "one two three"
"dictionary": {
"a": [
65,
97
],
"z": {
"A": 65,
"a": 97
}
},
"nulls": "loom dictionaries delete null values",
"number": 987.6543,
"string": "aA bB cC"
}
```

Expand All @@ -78,9 +105,19 @@ A tighter formatting that retains readability:

```json
{
"array": [ 1, 23 ],
"array": [
1,
[ 23, 45 ],
[ 67, [ 666 ], [ 777 ], 89 ]
],
"bool": true,
"string": "one two three"
"dictionary": {
"a": [ 65, 97 ],
"z": { "A": 65, "a": 97 }
},
"nulls": "loom dictionaries delete null values",
"number": 987.6543,
"string": "aA bB cC"
}
```

Expand All @@ -89,7 +126,92 @@ A tighter formatting that retains readability:
No extra whitespace:

```json
{"array":[1,23],"bool":true,"string":"one two three"}
{"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][yamllint]:

```yaml
---
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][yaml.org]:
```yaml
---
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:

```yaml
---
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
Expand All @@ -109,7 +231,7 @@ you can compile and run the demo from the command line:
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/v0.0.3/Json-sprint34.loomlib
https://github.com/pixeldroid/json-ls/releases/download/v1.0.0/Json-sprint34.loomlib

To uninstall, simply delete the file:

Expand Down Expand Up @@ -166,7 +288,10 @@ this will build the Json library, install it in the currently configured sdk, bu
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"
[JsonDemoCLI.build]: ./cli/src/JsonDemoCLI.build "build file for the CLI demo"
[JsonDemoCLI.ls]: ./cli/src/JsonDemoCLI.ls "source file for the CLI demo"
[jsonlint]: https://jsonlint.com/ "jsonlint"
[loom-json]: http://docs.theengine.co/loom/1.1.4813/api/system/JSON.html "Loom JSON class"
[loomtasks]: https://github.com/pixeldroid/loomtasks "loomtasks"
[yaml.org]: http://www.yaml.org/start.html "yaml.org"
[yamllint]: http://www.yamllint.com/ "yamllint"
76 changes: 67 additions & 9 deletions cli/src/JsonDemoCLI.ls
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,89 @@ package
import pixeldroid.json.Json;
import pixeldroid.json.JsonPrinter;
import pixeldroid.json.JsonPrinterOptions;
import pixeldroid.json.YamlPrinter;
import pixeldroid.json.YamlPrinterOptions;


public class JsonDemoCLI extends ConsoleApplication
{
override public function run():void
{
var jsonObject:Dictionary.<String,Object> = { "bool": true, "array": [1,23], "string": "aA bB cC" };
trace('source:\n' +dictionaryToString(jsonObject));
var jsonObject:Dictionary.<String,Object> = {
'nulls': "loom dictionaries delete null values",
'bool': true,
'number': 987.6543,
'string-simple': "aA bB cC",
'string-escapes': "\'single quote,\'\n\"double quote\non two lines\"",
'array': [1, [23, 45], [67, [666], [777], 89]],
'dictionary': { 'a': [65, 97], 'z': { 'A': 65, 'a': 97 } }
};
trace('\nsource:\n' +objectToString(jsonObject));

var j:Json = Json.fromObject(jsonObject);
trace('standard:\n' +JsonPrinter.print(j, JsonPrinterOptions.standard));
trace('compact:\n' +JsonPrinter.print(j, JsonPrinterOptions.compact));
trace('minified:\n' +JsonPrinter.print(j, JsonPrinterOptions.minified));
printJson(j);
printYaml(j);
}

private function dictionaryToString(d:Dictionary.<String,Object>):String

private function printJson(j:Json):void
{
var s:String = '';
trace('\njson');
trace('\nstandard:\n' +JsonPrinter.print(j, JsonPrinterOptions.standard));
trace('\ncompact:\n' +JsonPrinter.print(j, JsonPrinterOptions.compact));
trace('\nminified:\n' +JsonPrinter.print(j, JsonPrinterOptions.minified));
}

for (var k:String in d)
private function printYaml(j:Json):void
{
trace('\nyaml');
trace('\nstandard:\n' +YamlPrinter.print(j, YamlPrinterOptions.standard));
trace('\ncompact:\n' +YamlPrinter.print(j, YamlPrinterOptions.compact));
var opts:YamlPrinterOptions = new YamlPrinterOptions();
opts.printDocumentEnd = true;
opts.tabSize = 4;
opts.tightLists = true;
trace('\ncustom:\n' +YamlPrinter.print(j, opts));
}


private function objectToString(o:Object):String
{
// this simplified implementation assumes dictionaries are <String,Object> tuples
var s:String;

switch (o.getFullTypeName())
{
s += '"' +k +'": ' +d[k].toString() +'\n';
case 'system.Null' : s = 'null'; break;
case 'system.Boolean' : s = o.toString(); break;
case 'system.Number' : s = o.toString(); break;
case 'system.String' : s = '"' + o.toString() + '"'; break;
case 'system.Vector' : s = vectorToString(o as Vector.<Object>); break;
case 'system.Dictionary' : s = dictionaryToString(o as Dictionary.<String,Object>); break;
default: s = o.toString();
}

return s;
}

private function dictionaryToString(d:Dictionary.<String,Object>):String
{
var l:Vector.<String> = [];

for (var k:String in d)
l.push('"' +k +'": ' +objectToString(d[k]));

return '{ ' +l.join(', ') +' }';
}

private function vectorToString(v:Vector.<Object>):String
{
var l:Vector.<String> = [];

for each(var o:Object in v)
l.push(objectToString(o));

return '[ ' +l.join(', ') +' ]';
}
}
}
4 changes: 2 additions & 2 deletions lib/src/Json.build
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "Json",
"version": "0.0.3",
"version": "1.0.0",
"outputDir": "./build",
"references": [
"System"
],
"modules": [
{
"name": "Json",
"version": "0.0.3",
"version": "1.0.0",
"sourcePath": [
"."
]
Expand Down
28 changes: 27 additions & 1 deletion lib/src/pixeldroid/json/Json.ls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ package pixeldroid.json

public class Json
{
public static const version:String = '0.0.3';
public static const version:String = '1.0.0';

/**
Merges key:value pairs of second parameter into first, overriding any duplicates.
*Note* this method does not clone non-primitive values.
During the merge, new keys in the first Json object will pointed to values in the second.
After the merge, modifying values in the second Json object would be the same as modifying them in the first.
@param j1 Target Json object; will have j2 merged into it
@param j2 Json object to merge into j1
*/
static public function merge(j1:Json, j2:Json):void
{
Debug.assert(j1.type == Dictionary.getType(), "merge operand root data type must be Dictionary");
Debug.assert(j2.type == Dictionary.getType(), "merge operand root data type must be Dictionary");

for (var k:String in j2.keys)
j1.keys[k] = j2.keys[k];
}

static public function fromString(value:String):Json
{
Expand All @@ -22,6 +41,13 @@ package pixeldroid.json
return itemToJson(value);
}

static public function fromJSON(value:JSON):Json
{
Debug.assert(value.getJSONType() == JSONType.JSON_OBJECT, "JSON root data type must be Object");

return JSONObjectToJson(value);
}

public var keys:Dictionary.<String, Json> = {};
public var items:Vector.<Json> = [];
public var value:Object = null;
Expand Down
21 changes: 19 additions & 2 deletions lib/src/pixeldroid/json/JsonPrinter.ls
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ package pixeldroid.json

switch (json.type.getFullName())
{
case 'system.Null' : s = 'null'; break;
// 'system.Null' : loom dictionaries delete any keys with values set to null
case 'system.Boolean' : s = json.value.toString(); break;
case 'system.Number' : s = json.value.toString(); break;
case 'system.String' : s = '"' + json.value + '"'; break;
case 'system.String' : s = stringToJsonString(json.value.toString()); break;
case 'system.Vector' : s = vectorToJsonString(options, json.items, indentLevel); break;
case 'system.Dictionary' : s = dictionaryToJsonString(options, json.keys, indentLevel); break;
}
Expand All @@ -39,6 +39,23 @@ package pixeldroid.json
return s;
}

static private function stringToJsonString(s:String):String
{
var result:String = s;

result = result.split('\\').join('\\\\'); // expand backslash before others

result = result.split('"').join('\\"');
result = result.split('\b').join('\\b');
result = result.split('\f').join('\\f');
result = result.split('\n').join('\\n');
result = result.split('\r').join('\\r');
result = result.split('\t').join('\\t');
// result = result.split('\u').join('\\u'); // FIXME: need to match \uXXXX (u+4)

return '"' +result +'"';
}

static private function containerToJsonString(options:JsonPrinterOptions, items:Vector.<Object>, indentLevel:Number, openBrace:String, closeBrace:String):String
{
var s:String;
Expand Down
Loading

0 comments on commit 3a153c4

Please sign in to comment.