-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadme.html
230 lines (214 loc) · 7.55 KB
/
readme.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<html>
<head>
<style type="text/css">
<!--
pre#excode {
background-color:#FFFFFF;
border:1px dashed #000000;
font-family:'courier new';
font-size:8pt;
padding:8px;
}
span#lit {
color:#0000FF;
}
span#keyw {
color:#000000;
font-weight:bold;
}
span#cmt {
color:#008000;
}
span#dtyp {
color:#2B91AF;
}
--> </style>
</head>
<body bgcolor="#D4D0C8">
<h1>SuperObject</h1>
<h3>What is JSON ?</h3>
<p>
JSON (JavaScript Object Notation) is a lightweight data-interchange format.<br>
It is easy for humans to read and write. <br>
It is easy for machines to parse and generate. <br>
It is based on a subset of the JavaScript Programming Language,
Standard ECMA-262 3rd Edition - December 1999. <br>
JSON is a text format that is
completely language independent but uses conventions that are familiar to programmers.<br>
These properties make JSON an ideal data-interchange language.<br>
You can get more informations on <a href="http://www.json.org">json.org</a>.
<pre id="exCode">
{
"name": <span id="lit">"Henri Gourvest"</span>, <span id="cmt">/* this is a comment */</span>
"vip": true,
"telephones": [<span id="lit">"000000000"</span>, <span id="lit">"111111111111"</span>],
"age": 33,
"size": 1.83,
"adresses": [
{
"adress": <span id="lit">"blabla"</span>,
"city": <span id="lit">"Metz"</span>,
"pc": 57000
},
{
"adress": <span id="lit">"blabla"</span>,
"city": <span id="lit">"Nantes"</span>,
"pc": 44000
}
]
}
</pre>
<h3>Parsing a JSON data structure</h3>
<pre id="exCode">
<span id="keyw">var</span>
obj: ISuperObject;
<span id="keyw">begin</span>
obj := SO(<span id="lit">'{"foo": true}'</span>);
obj := TSuperObject.ParseString(<span id="lit">'{"foo": true}'</span>);
obj := TSuperObject.ParseStream(stream);
obj := TSuperObject.ParseFile(FileName);
<span id="keyw">end;</span>
</pre>
<h3>Accessing data</h3>
<p>
There isn't individual datastructure for each supported data types.<br>
They are all an object: the <b>ISuperObject</b>.
</p>
<pre id="exCode">
val := obj.AsString;
val := obj.AsInteger;
val := obj.AsBoolean;
val := obj.AsDouble;
val := obj.AsArray;
val := obj.AsObject;
val := obj.AsMethod;
</pre>
<h4>How to read a property value of an object ?</h4>
<pre id="exCode">
val := obj.AsObject.S[<span id="lit">'foo'</span>]; <span id="cmt">// get a string</span>
val := obj.AsObject.I[<span id="lit">'foo'</span>]; <span id="cmt">// get an Int64</span>
val := obj.AsObject.B[<span id="lit">'foo'</span>]; <span id="cmt">// get a Boolean</span>
val := obj.AsObject.D[<span id="lit">'foo'</span>]; <span id="cmt">// get a Double</span>
val := obj.AsObject.O[<span id="lit">'foo'</span>]; <span id="cmt">// get an Object (default)</span>
val := obj.AsObject.M[<span id="lit">'foo'</span>]; <span id="cmt">// get a Method</span>
val := obj.AsObject.N[<span id="lit">'foo'</span>]; <span id="cmt">// get a null object</span>
</pre>
<h4>How to read a value from an array ?</h4>
<pre id="exCode">
<span id="cmt">// the advanced way</span>
val := obj.AsArray.S[0]; <span id="cmt">// get a string</span>
val := obj.AsArray.I[0]; <span id="cmt">// get an Int64</span>
val := obj.AsArray.B[0]; <span id="cmt">// get a Boolean</span>
val := obj.AsArray.D[0]; <span id="cmt">// get a Double</span>
val := obj.AsArray.O[0]; <span id="cmt">// get an Object (default)</span>
val := obj.AsArray.M[0]; <span id="cmt">// get a Method</span>
val := obj.AsArray.N[0]; <span id="cmt">// get a null object</span>
</pre>
<h4>Using paths</h4>
<p>Using paths is a <b>very productive</b> method to find an object when you know where is it.<br>
This is some usage cases:
</p>
<pre id="exCode">
obj[<span id="lit">'foo'</span>]; <span id="cmt">// get a property</span>
obj[<span id="lit">'123'</span>]; <span id="cmt">// get an item array</span>
obj[<span id="lit">'foo.list'</span>]; <span id="cmt">// get a property from an object</span>
obj[<span id="lit">'foo[123]'</span>]; <span id="cmt">// get an item array from an object</span>
obj[<span id="lit">'foo(1,2,3)'</span>]; <span id="cmt">// call a method</span>
obj[<span id="lit">'foo[]'</span>] := value; <span id="cmt">// add an item array</span>
</pre>
<p>you also can encapsulate paths:</p>
<pre id="exCode">
obj := so(<span id="lit">'{"index": 1, "items": ["item 1", "item 2", "item 3"]}'</span>);
obj[<span id="lit">'items[index]'</span>] <span id="cmt">// return "item 2"</span>
</pre>
<p>or recreate a new data structure from another:</p>
<pre id="exCode">
obj := so(<span id="lit">'{"index": 1, "items": ["item 1", "item 2", "item 3"]}'</span>);
obj[<span id="lit">'{"item": items[index], "index": index}'</span>] <span id="cmt">// return {"item": "item 2", "index": 1}</span>
</pre>
<h3>Browsing data structure</h3>
<h4>Using Delphi enumerator.</h4>
<p>Using Delphi enumerator you can browse item's array or property's object value in the same maner.</p>
<pre id="exCode">
<span id="keyw">var</span>
item: ISuperObject;
<span id="keyw">begin</span>
<span id="keyw">for</span> item <span id="keyw">in</span> obj[<span id="lit">'items'</span>] <span id="keyw">do</span> ...
</pre>
<p>you can also browse the keys and values of an object like this:</p>
<pre id="exCode">
<span id="keyw">var</span>
item: TSuperAvlEntry;
<span id="keyw">begin</span>
<span id="keyw">for</span> item <span id="keyw">in</span> obj.AsObject <span id="keyw">do</span> ...
<span id="keyw">begin</span>
item.Name;
item.Value;
<span id="keyw">end</span>;
</pre>
<h4>Browsing object properties without enumerator</h4>
<pre id="exCode">
<span id="keyw">var</span>
item: TSuperObjectIter;
<span id="keyw">begin</span>
<span id="keyw">if</span> ObjectFindFirst(obj, item) <span id="keyw">then</span>
<span id="keyw">repeat</span>
item.key;
item.val;
<span id="keyw">until</span> <span id="keyw">not</span> ObjectFindNext(item);
ObjectFindClose(item);
</pre>
<h4>Browsing array items without enumerator</h4>
<pre id="exCode">
<span id="keyw">var</span>
item: Integer;
<span id="keyw">begin</span>
<span id="keyw">for</span> item := 0 <span id="keyw">to</span> obj.AsArray.Length - 1 <span id="keyw">do</span>
obj.AsArray[item]
</pre>
<h3>RTTI & marshalling in Delphi 2010</h3>
<pre id="exCode">
<span id="keyw">type</span>
TData = <span id="keyw">record</span>
str: string;
int: Integer;
bool: Boolean;
flt: Double;
<span id="keyw">end;</span>
<span id="keyw">var</span>
ctx: TSuperRttiContext;
data: TData;
obj: ISuperObject;
<span id="keyw">begin</span>
ctx := TSuperRttiContext.Create;
<span id="keyw">try</span>
data := ctx.AsType<TData>(SO(<span id="lit">'{str: "foo", int: 123, bool: true, flt: 1.23}'</span>));
obj := ctx.AsJson<TData>(data);
<span id="keyw">finally</span>
ctx.Free;
<span id="keyw">end;</span>
<span id="keyw">end;</span>
</pre>
<h3>Saving data</h3>
<pre id="exCode">
obj.AsJSon(options);
obj.SaveTo(stream);
obj.SaveTo(filename);
</pre>
<h3>Helpers</h3>
<pre id="exCode">
SO([<span id="lit">'prop1'</span>, true, <span id="lit">'prop2'</span>, 123]); <span id="cmt">// return an object {"prop1": true, "prop2": 123}</span>
SA([true, 123]); <span id="cmt">// return an array [true, 123]</span>
</pre>
<h3>Non canonical forms</h3>
The SuperObject is able to parse non canonical forms.
<pre id="exCode">
<span id="cmt">// unquoted identifiers</span>
SO(<span id="lit">'{foo: true}'</span>);
<span id="cmt">// unescaped or unquoted strings</span>
SO(<span id="lit">'{собственность: bla bla bla}'</span>);
<span id="cmt">// excadecimal</span>
SO(<span id="lit">'{foo: \xFF}'</span>);
</pre>
</body>
</html>