Permalink
Browse files

Add docs and tests

  • Loading branch information...
KevinEady committed Jan 7, 2018
1 parent 92f234e commit 766303e95ad7389fff06483ef9f9d6d197fbb42f
@@ -310,4 +310,33 @@ const TRIM_BOTH := 0x3; // Trim whitespace from Left and Right of string.</code
<return>An Integer</return>
</function>
<function name="PackJSON">
<prototype>PackJSON( expr )</prototype>
<parameter name="expr" value="An object to pack" />
<explain>Returns a JSON-serialization of the object to pack.</explain>
<explain>The following conversions are done:</explain>
<explain>eScript -&gt; JSON</explain>
<explain>double -&gt; number</explain>
<explain>int -&gt; number</explain>
<explain>string -&gt; string</explain>
<explain>array -&gt; array</explain>
<explain>struct -&gt; object</explain>
<explain>dict -&gt; object</explain>
<explain>everything else -&gt; null</explain>
<return>string</return>
</function>
<function name="UnpackJSON">
<prototype>UnpackJSON( str )</prototype>
<parameter name="str" value="A JSON serialized string" />
<explain>Returns an eScript object representing the serialized JSON string.</explain>
<explain>The following conversions are done:</explain>
<explain>JSON -&gt; eScript</explain>
<explain>number -&gt; double</explain>
<explain>string -&gt; string</explain>
<explain>array -&gt; array</explain>
<explain>object-&gt; struct</explain>
<explain>null -&gt; uninitialized object</explain>
<return>Any one of: double, string, array, struct, or uninit.</return>
</function>
</ESCRIPT>
@@ -2,9 +2,17 @@
<ESCRIPT>
<header>
<topic>Latest Core Changes</topic>
<datemodified>01-05-2018</datemodified>
<datemodified>01-07-2018</datemodified>
</header>
<version name="POL099">
<entry>
<date>01-07-2018</date>
<author>Kevin:</author>
<change type="Added">basic::UnpackJSON(string)<br/>
Given a string in JSON format, returns an eScript object representing that JSON value.</change>
<change type="Added">basic::PackJSON(expr)<br/>
Given an eScript expression (Int, Dbl, String, Array, Struct, Dict), returns a serialization of the object in JSON format.</change>
</entry>
<entry>
<date>01-05-2018</date>
<author>Turley:</author>
@@ -1,4 +1,9 @@
-- POL099 --
01-07-2018 Kevin:
Added: basic::UnpackJSON(string)
Given a string in JSON format, returns an eScript object representing that JSON value.
Added: basic::PackJSON(expr)
Given an eScript expression (Int, Dbl, String, Array, Struct, Dict), returns a serialization of the object in JSON format.
01-05-2018 Turley:
Added: fileaccess.cfg Directory entry
Can be definied multiple times in package syntax.
@@ -0,0 +1,119 @@
H:\vs_workspace\Distro\scripts\json.src, Line 2
program main()
var expr := struct{
0: decl local #0
1: struct
2: "hello there"
3: addmember-assign(a)
4: 1L
5: Func(0,0): Boolean
6: addmember-assign(b)
7: array
8: 0L
9: Func(0,0): Boolean
10: init{}
11: "u"
12: Func(0,1): Unpack
13: init{}
14: addmember-assign(c)
15: struct
16: "value"
17: addmember-assign(key)
18: addmember-assign(d)
19: :=
20: #
var json := PackJSON(expr);
21: decl local #1
22: local #0
23: Func(0,2): PackJSON
24: :=
25: #
var obj := UnpackJSON(json);
26: decl local #2
27: local #1
28: Func(0,3): UnpackJSON
29: :=
30: #
print(TypeOfInt(obj) == OT_STRUCT);
31: local #2
32: Func(0,4): TypeOfInt
33: 10L
34: ==
35: Func(1,0): Print
36: #
print(obj.size() == 4);
37: local #2
38: Call Method id size (#48, 0 params)
39: 4L
40: ==
41: Func(1,0): Print
42: #
print(obj.a == "hello there");
43: local #2
44: get member 'a'
45: "hello there"
46: ==
47: Func(1,0): Print
48: #
print(obj.b == Boolean(1));
49: local #2
50: get member 'b'
51: 1L
52: Func(0,0): Boolean
53: ==
54: Func(1,0): Print
55: #
print(obj.c[1] == Boolean(0));
56: local #2
57: get member 'c'
58: 1L
59: [] 1
60: 0L
61: Func(0,0): Boolean
62: ==
63: Func(1,0): Print
64: #
print(obj.c[2] == Unpack("u"));
65: local #2
66: get member 'c'
67: 2L
68: [] 1
69: "u"
70: Func(0,1): Unpack
71: ==
72: Func(1,0): Print
73: #
print(obj.c.size() == 2);
74: local #2
75: get member 'c'
76: Call Method id size (#48, 0 params)
77: 2L
78: ==
79: Func(1,0): Print
80: #
print(TypeOfInt(obj.d) == OT_STRUCT);
81: local #2
82: get member 'd'
83: Func(0,4): TypeOfInt
84: 10L
85: ==
86: Func(1,0): Print
87: #
print(obj.d.size() == 1);
88: local #2
89: get member 'd'
90: Call Method id size (#48, 0 params)
91: 1L
92: ==
93: Func(1,0): Print
94: #
print(obj.d.key == "value");
95: local #2
96: get member 'd'
97: get member 'key'
98: "value"
99: ==
100: Func(1,0): Print
101: #
102: leave block(3)
103: progend
@@ -0,0 +1,10 @@
1
1
1
1
1
1
1
1
1
1
@@ -0,0 +1,20 @@
program main()
var expr := struct{
"a" := "hello there",
"b" := Boolean(1),
"c" := array{ Boolean(0), Unpack("u") },
"d" := struct{ "key" := "value" }
};
var json := PackJSON(expr);
var obj := UnpackJSON(json);
print(TypeOfInt(obj) == OT_STRUCT);
print(obj.size() == 4);
print(obj.a == "hello there");
print(obj.b == Boolean(1));
print(obj.c[1] == Boolean(0));
print(obj.c[2] == Unpack("u"));
print(obj.c.size() == 2);
print(TypeOfInt(obj.d) == OT_STRUCT);
print(obj.d.size() == 1);
print(obj.d.key == "value");
endprogram

0 comments on commit 766303e

Please sign in to comment.