Skip to content

Commit 3a153c4

Browse files
authored
Merge pull request #4 from ellemenno/v1.0.0
v1.0.0
2 parents f3476f1 + a4441b9 commit 3a153c4

File tree

12 files changed

+618
-68
lines changed

12 files changed

+618
-68
lines changed

README.md

Lines changed: 135 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,47 @@ trace(j.keys['key'].items[2].keys['r'].items[1]);
5555

5656
### JsonPrinter
5757

58-
This library includes a configurable JSON pretty-printer, with three pre-defined configurations for convenience:
58+
This library includes a configurable JSON pretty-printer, with three pre-defined configurations for convenience.
59+
60+
Other custom configurations are possible by adjusting the values of `JsonPrinterOptions`.
5961

6062
#### Standard
6163

62-
As you would find from jsonlint:
64+
Similar to [jsonlint][jsonlint]:
6365

6466
```json
6567
{
6668
"array": [
6769
1,
68-
23
70+
[
71+
23,
72+
45
73+
],
74+
[
75+
67,
76+
[
77+
666
78+
],
79+
[
80+
777
81+
],
82+
89
83+
]
6984
],
7085
"bool": true,
71-
"string": "one two three"
86+
"dictionary": {
87+
"a": [
88+
65,
89+
97
90+
],
91+
"z": {
92+
"A": 65,
93+
"a": 97
94+
}
95+
},
96+
"nulls": "loom dictionaries delete null values",
97+
"number": 987.6543,
98+
"string": "aA bB cC"
7299
}
73100
```
74101

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

79106
```json
80107
{
81-
"array": [ 1, 23 ],
108+
"array": [
109+
1,
110+
[ 23, 45 ],
111+
[ 67, [ 666 ], [ 777 ], 89 ]
112+
],
82113
"bool": true,
83-
"string": "one two three"
114+
"dictionary": {
115+
"a": [ 65, 97 ],
116+
"z": { "A": 65, "a": 97 }
117+
},
118+
"nulls": "loom dictionaries delete null values",
119+
"number": 987.6543,
120+
"string": "aA bB cC"
84121
}
85122
```
86123

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

91128
```json
92-
{"array":[1,23],"bool":true,"string":"one two three"}
129+
{"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"}
130+
```
131+
132+
133+
### YamlPrinter
134+
135+
This library includes a configurable YAML pretty-printer, with two pre-defined configurations for convenience:
136+
137+
Other custom configurations are possible by adjusting the values of `YamlPrinterOptions`.
138+
139+
#### Standard
140+
141+
As you would find from [yamllint][yamllint]:
142+
143+
```yaml
144+
---
145+
array:
146+
- 1
147+
-
148+
- 23
149+
- 45
150+
-
151+
- 67
152+
-
153+
- 666
154+
-
155+
- 777
156+
- 89
157+
bool: true
158+
dictionary:
159+
a:
160+
- 65
161+
- 97
162+
z:
163+
A: 65
164+
a: 97
165+
nulls: "loom dictionaries delete null values"
166+
number: 987.6543
167+
string: "aA bB cC"
168+
```
169+
170+
#### Compact
171+
172+
A tighter formatting similar to [yaml.org][yaml.org]:
173+
174+
```yaml
175+
---
176+
array:
177+
- 1
178+
- [ 23, 45 ]
179+
- [ 67, [ 666 ], [ 777 ], 89 ]
180+
bool: true
181+
dictionary:
182+
a: [ 65, 97 ]
183+
z: { A: 65, a: 97 }
184+
nulls: "loom dictionaries delete null values"
185+
number: 987.6543
186+
string: "aA bB cC"
187+
```
188+
189+
#### Custom
190+
191+
Custom formatting can be achieved by configuring the `YamlPrinterOptions` parameter:
192+
193+
```yaml
194+
---
195+
array:
196+
- 1
197+
- - 23
198+
- 45
199+
- - 67
200+
- - 666
201+
- - 777
202+
- 89
203+
bool: true
204+
dictionary:
205+
a:
206+
- 65
207+
- 97
208+
z:
209+
A: 65
210+
a: 97
211+
nulls: "loom dictionaries delete null values"
212+
number: 987.6543
213+
string: "aA bB cC"
214+
...
93215
```
94216

95217
### JsonDemo
@@ -109,7 +231,7 @@ you can compile and run the demo from the command line:
109231
Download the library into its matching sdk folder:
110232

111233
$ curl -L -o ~/.loom/sdks/sprint34/libs/Json.loomlib \
112-
https://github.com/pixeldroid/json-ls/releases/download/v0.0.3/Json-sprint34.loomlib
234+
https://github.com/pixeldroid/json-ls/releases/download/v1.0.0/Json-sprint34.loomlib
113235

114236
To uninstall, simply delete the file:
115237

@@ -166,7 +288,10 @@ this will build the Json library, install it in the currently configured sdk, bu
166288
Pull requests are welcome!
167289

168290

169-
[loomtasks]: https://github.com/pixeldroid/loomtasks "loomtasks"
170-
[loom-json]: http://docs.theengine.co/loom/1.1.4813/api/system/JSON.html "Loom JSON class"
171291
[JsonDemoCLI.build]: ./cli/src/JsonDemoCLI.build "build file for the CLI demo"
172292
[JsonDemoCLI.ls]: ./cli/src/JsonDemoCLI.ls "source file for the CLI demo"
293+
[jsonlint]: https://jsonlint.com/ "jsonlint"
294+
[loom-json]: http://docs.theengine.co/loom/1.1.4813/api/system/JSON.html "Loom JSON class"
295+
[loomtasks]: https://github.com/pixeldroid/loomtasks "loomtasks"
296+
[yaml.org]: http://www.yaml.org/start.html "yaml.org"
297+
[yamllint]: http://www.yamllint.com/ "yamllint"

cli/src/JsonDemoCLI.ls

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,89 @@ package
55
import pixeldroid.json.Json;
66
import pixeldroid.json.JsonPrinter;
77
import pixeldroid.json.JsonPrinterOptions;
8+
import pixeldroid.json.YamlPrinter;
9+
import pixeldroid.json.YamlPrinterOptions;
810

911

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

1727
var j:Json = Json.fromObject(jsonObject);
18-
trace('standard:\n' +JsonPrinter.print(j, JsonPrinterOptions.standard));
19-
trace('compact:\n' +JsonPrinter.print(j, JsonPrinterOptions.compact));
20-
trace('minified:\n' +JsonPrinter.print(j, JsonPrinterOptions.minified));
28+
printJson(j);
29+
printYaml(j);
2130
}
2231

23-
private function dictionaryToString(d:Dictionary.<String,Object>):String
32+
33+
private function printJson(j:Json):void
2434
{
25-
var s:String = '';
35+
trace('\njson');
36+
trace('\nstandard:\n' +JsonPrinter.print(j, JsonPrinterOptions.standard));
37+
trace('\ncompact:\n' +JsonPrinter.print(j, JsonPrinterOptions.compact));
38+
trace('\nminified:\n' +JsonPrinter.print(j, JsonPrinterOptions.minified));
39+
}
2640

27-
for (var k:String in d)
41+
private function printYaml(j:Json):void
42+
{
43+
trace('\nyaml');
44+
trace('\nstandard:\n' +YamlPrinter.print(j, YamlPrinterOptions.standard));
45+
trace('\ncompact:\n' +YamlPrinter.print(j, YamlPrinterOptions.compact));
46+
var opts:YamlPrinterOptions = new YamlPrinterOptions();
47+
opts.printDocumentEnd = true;
48+
opts.tabSize = 4;
49+
opts.tightLists = true;
50+
trace('\ncustom:\n' +YamlPrinter.print(j, opts));
51+
}
52+
53+
54+
private function objectToString(o:Object):String
55+
{
56+
// this simplified implementation assumes dictionaries are <String,Object> tuples
57+
var s:String;
58+
59+
switch (o.getFullTypeName())
2860
{
29-
s += '"' +k +'": ' +d[k].toString() +'\n';
61+
case 'system.Null' : s = 'null'; break;
62+
case 'system.Boolean' : s = o.toString(); break;
63+
case 'system.Number' : s = o.toString(); break;
64+
case 'system.String' : s = '"' + o.toString() + '"'; break;
65+
case 'system.Vector' : s = vectorToString(o as Vector.<Object>); break;
66+
case 'system.Dictionary' : s = dictionaryToString(o as Dictionary.<String,Object>); break;
67+
default: s = o.toString();
3068
}
3169

3270
return s;
3371
}
72+
73+
private function dictionaryToString(d:Dictionary.<String,Object>):String
74+
{
75+
var l:Vector.<String> = [];
76+
77+
for (var k:String in d)
78+
l.push('"' +k +'": ' +objectToString(d[k]));
79+
80+
return '{ ' +l.join(', ') +' }';
81+
}
82+
83+
private function vectorToString(v:Vector.<Object>):String
84+
{
85+
var l:Vector.<String> = [];
86+
87+
for each(var o:Object in v)
88+
l.push(objectToString(o));
89+
90+
return '[ ' +l.join(', ') +' ]';
91+
}
3492
}
3593
}

lib/src/Json.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "Json",
3-
"version": "0.0.3",
3+
"version": "1.0.0",
44
"outputDir": "./build",
55
"references": [
66
"System"
77
],
88
"modules": [
99
{
1010
"name": "Json",
11-
"version": "0.0.3",
11+
"version": "1.0.0",
1212
"sourcePath": [
1313
"."
1414
]

lib/src/pixeldroid/json/Json.ls

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,26 @@ package pixeldroid.json
66

77
public class Json
88
{
9-
public static const version:String = '0.0.3';
9+
public static const version:String = '1.0.0';
10+
11+
/**
12+
Merges key:value pairs of second parameter into first, overriding any duplicates.
13+
14+
*Note* this method does not clone non-primitive values.
15+
During the merge, new keys in the first Json object will pointed to values in the second.
16+
After the merge, modifying values in the second Json object would be the same as modifying them in the first.
17+
18+
@param j1 Target Json object; will have j2 merged into it
19+
@param j2 Json object to merge into j1
20+
*/
21+
static public function merge(j1:Json, j2:Json):void
22+
{
23+
Debug.assert(j1.type == Dictionary.getType(), "merge operand root data type must be Dictionary");
24+
Debug.assert(j2.type == Dictionary.getType(), "merge operand root data type must be Dictionary");
25+
26+
for (var k:String in j2.keys)
27+
j1.keys[k] = j2.keys[k];
28+
}
1029

1130
static public function fromString(value:String):Json
1231
{
@@ -22,6 +41,13 @@ package pixeldroid.json
2241
return itemToJson(value);
2342
}
2443

44+
static public function fromJSON(value:JSON):Json
45+
{
46+
Debug.assert(value.getJSONType() == JSONType.JSON_OBJECT, "JSON root data type must be Object");
47+
48+
return JSONObjectToJson(value);
49+
}
50+
2551
public var keys:Dictionary.<String, Json> = {};
2652
public var items:Vector.<Json> = [];
2753
public var value:Object = null;

lib/src/pixeldroid/json/JsonPrinter.ls

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ package pixeldroid.json
1414

1515
switch (json.type.getFullName())
1616
{
17-
case 'system.Null' : s = 'null'; break;
17+
// 'system.Null' : loom dictionaries delete any keys with values set to null
1818
case 'system.Boolean' : s = json.value.toString(); break;
1919
case 'system.Number' : s = json.value.toString(); break;
20-
case 'system.String' : s = '"' + json.value + '"'; break;
20+
case 'system.String' : s = stringToJsonString(json.value.toString()); break;
2121
case 'system.Vector' : s = vectorToJsonString(options, json.items, indentLevel); break;
2222
case 'system.Dictionary' : s = dictionaryToJsonString(options, json.keys, indentLevel); break;
2323
}
@@ -39,6 +39,23 @@ package pixeldroid.json
3939
return s;
4040
}
4141

42+
static private function stringToJsonString(s:String):String
43+
{
44+
var result:String = s;
45+
46+
result = result.split('\\').join('\\\\'); // expand backslash before others
47+
48+
result = result.split('"').join('\\"');
49+
result = result.split('\b').join('\\b');
50+
result = result.split('\f').join('\\f');
51+
result = result.split('\n').join('\\n');
52+
result = result.split('\r').join('\\r');
53+
result = result.split('\t').join('\\t');
54+
// result = result.split('\u').join('\\u'); // FIXME: need to match \uXXXX (u+4)
55+
56+
return '"' +result +'"';
57+
}
58+
4259
static private function containerToJsonString(options:JsonPrinterOptions, items:Vector.<Object>, indentLevel:Number, openBrace:String, closeBrace:String):String
4360
{
4461
var s:String;

0 commit comments

Comments
 (0)