1 | .. _api-zoo-format-json: |
---|
2 | |
---|
3 | ZOO.Format.JSON |
---|
4 | =============== |
---|
5 | |
---|
6 | A parser to read/write JSON safely. |
---|
7 | |
---|
8 | Properties |
---|
9 | ---------- |
---|
10 | |
---|
11 | .. list-table:: |
---|
12 | :widths: 12 50 |
---|
13 | :header-rows: 1 |
---|
14 | |
---|
15 | * - NAME |
---|
16 | - DESCRIPTION |
---|
17 | * - :ref:`indent <indent>` |
---|
18 | - {String} For "pretty" printing, the indent string will be used once for each indentation level. |
---|
19 | * - :ref:`space <space>` |
---|
20 | - {String} For "pretty" printing, the space string will be used after the ":" separating a name/value pair. |
---|
21 | * - :ref:`newline <newline>` |
---|
22 | - {String} For "pretty" printing, the newline string will be used at the end of each name/value pair or array item. |
---|
23 | * - :ref:`level <level>` |
---|
24 | - {Integer} For "pretty" printing, this is incremented/decremented during serialization. |
---|
25 | * - :ref:`pretty <pretty>` |
---|
26 | - {Boolean} Serialize with extra whitespace for structure. |
---|
27 | |
---|
28 | Functions |
---|
29 | --------- |
---|
30 | |
---|
31 | .. list-table:: |
---|
32 | :widths: 14 50 |
---|
33 | :header-rows: 1 |
---|
34 | |
---|
35 | * - NAME |
---|
36 | - DESCRIPTION |
---|
37 | * - :ref:`ZOO.Format.JSON <ZOO.Format.JSON>` |
---|
38 | - Create a new parser for JSON. |
---|
39 | * - :ref:`read <read>` |
---|
40 | - Deserialize a json string. |
---|
41 | * - :ref:`write <write>` |
---|
42 | - Serialize an object into a JSON string. |
---|
43 | * - :ref:`writeIndent <writeIndent>` |
---|
44 | - Output an indentation string depending on the indentation level. |
---|
45 | * - :ref:`writeNewline <writeNewline>` |
---|
46 | - Output a string representing a newline if in pretty printing mode. |
---|
47 | * - :ref:`writeSpace <writeSpace>` |
---|
48 | - Output a string representing a space if in pretty printing mode. |
---|
49 | |
---|
50 | Serialize Properties |
---|
51 | -------------------- |
---|
52 | |
---|
53 | .. list-table:: |
---|
54 | :widths: 14 50 |
---|
55 | :header-rows: 1 |
---|
56 | |
---|
57 | * - NAME |
---|
58 | - DESCRIPTION |
---|
59 | * - :ref:`serialize <serialize>` |
---|
60 | - Object with properties corresponding to the serializable data types. |
---|
61 | |
---|
62 | Serialize Functions |
---|
63 | ------------------- |
---|
64 | |
---|
65 | .. list-table:: |
---|
66 | :widths: 14 50 |
---|
67 | :header-rows: 1 |
---|
68 | |
---|
69 | * - NAME |
---|
70 | - DESCRIPTION |
---|
71 | * - :ref:`serialize.object <serialize.object>` |
---|
72 | - Transform an object into a JSON string. |
---|
73 | * - :ref:`serialize.array <serialize.array>` |
---|
74 | - Transform an array into a JSON string. |
---|
75 | * - :ref:`serialize.string <serialize.string>` |
---|
76 | - Transform a string into a JSON string. |
---|
77 | * - :ref:`serialize.number <serialize.number>` |
---|
78 | - Transform a number into a JSON string. |
---|
79 | * - :ref:`serialize.boolean <serialize.boolean>` |
---|
80 | - Transform a boolean into a JSON string. |
---|
81 | * - :ref:`serialize.date <serialize.date>` |
---|
82 | - Transform a date into a JSON string. |
---|
83 | |
---|
84 | **Properties** |
---|
85 | |
---|
86 | .. _indent: |
---|
87 | |
---|
88 | indent |
---|
89 | ``{String}`` For "pretty" printing, the indent string will be used once for each indentation level. |
---|
90 | |
---|
91 | .. _space: |
---|
92 | |
---|
93 | space |
---|
94 | ``{String}`` For "pretty" printing, the space string will be used after the ":" separating a name/value pair. |
---|
95 | |
---|
96 | .. _newline: |
---|
97 | |
---|
98 | newline |
---|
99 | ``{String}`` For "pretty" printing, the newline string will be used at the end of each name/value pair or array item. |
---|
100 | |
---|
101 | .. _level: |
---|
102 | |
---|
103 | level |
---|
104 | ``{Integer}`` For "pretty" printing, this is incremented/decremented during serialization. |
---|
105 | |
---|
106 | .. _pretty: |
---|
107 | |
---|
108 | pretty |
---|
109 | ``{Boolean}`` Serialize with extra whitespace for structure. This is set by the :ref:`write <write>` method. |
---|
110 | |
---|
111 | **Functions** |
---|
112 | |
---|
113 | .. _ZOO.Format.JSON: |
---|
114 | |
---|
115 | ZOO.Format.JSON |
---|
116 | Create a new parser for JSON. |
---|
117 | |
---|
118 | *Parameters* |
---|
119 | |
---|
120 | ``options {Object}`` An optional object whose properties will be set on this instance. |
---|
121 | |
---|
122 | .. _read: |
---|
123 | |
---|
124 | read |
---|
125 | :: |
---|
126 | |
---|
127 | read: function(json,filter) |
---|
128 | |
---|
129 | Deserialize a json string. |
---|
130 | |
---|
131 | *Parameters* |
---|
132 | |
---|
133 | | ``json {String}`` A JSON string |
---|
134 | | ``filter {Function}`` A function which will be called for every key and value at every level of the final result. Each value will be replaced by the result of the filter function. This can be used to reform generic objects into instances of classes, or to transform date strings into Date objects. |
---|
135 | |
---|
136 | *Returns* |
---|
137 | |
---|
138 | ``{Object}`` An object, array, string, or number. |
---|
139 | |
---|
140 | .. _write: |
---|
141 | |
---|
142 | write |
---|
143 | :: |
---|
144 | |
---|
145 | write: function(value,pretty) |
---|
146 | |
---|
147 | Serialize an object into a JSON string. |
---|
148 | |
---|
149 | *Parameters* |
---|
150 | |
---|
151 | | ``value {String}`` The object, array, string, number, boolean or date to be serialized. |
---|
152 | | ``pretty {Boolean}`` Structure the output with newlines and indentation. Default is false. |
---|
153 | |
---|
154 | *Returns* |
---|
155 | |
---|
156 | ``{String}`` The JSON string representation of the input value. |
---|
157 | |
---|
158 | .. _writeIndent: |
---|
159 | |
---|
160 | writeIndent |
---|
161 | :: |
---|
162 | |
---|
163 | writeIndent: function() |
---|
164 | |
---|
165 | Output an indentation string depending on the indentation level. |
---|
166 | |
---|
167 | *Returns* |
---|
168 | |
---|
169 | ``{String}`` An appropriate indentation string. |
---|
170 | |
---|
171 | .. _writeNewline: |
---|
172 | |
---|
173 | writeNewline |
---|
174 | :: |
---|
175 | |
---|
176 | writeNewline: function() |
---|
177 | |
---|
178 | Output a string representing a newline if in pretty printing mode. |
---|
179 | |
---|
180 | *Returns* |
---|
181 | |
---|
182 | ``{String}`` A string representing a new line. |
---|
183 | |
---|
184 | .. _writeSpace: |
---|
185 | |
---|
186 | writeSpace |
---|
187 | :: |
---|
188 | |
---|
189 | writeSpace: function() |
---|
190 | |
---|
191 | Output a string representing a space if in pretty printing mode. |
---|
192 | |
---|
193 | *Returns* |
---|
194 | |
---|
195 | ``{String}`` A space. |
---|
196 | |
---|
197 | **Serialize Properties** |
---|
198 | |
---|
199 | .. _serialize: |
---|
200 | |
---|
201 | serialize |
---|
202 | Object with properties corresponding to the serializable data types. Property |
---|
203 | values are functions that do the actual serializing. |
---|
204 | |
---|
205 | **Serialize Functions** |
---|
206 | |
---|
207 | .. _serialize.object: |
---|
208 | |
---|
209 | serialize.object |
---|
210 | Transform an object into a JSON string. |
---|
211 | |
---|
212 | *Parameters* |
---|
213 | |
---|
214 | ``object {Object}`` The object to be serialized. |
---|
215 | |
---|
216 | *Returns* |
---|
217 | |
---|
218 | ``{String}`` A JSON string representing the object. |
---|
219 | |
---|
220 | .. _serialize.array: |
---|
221 | |
---|
222 | serialize.array |
---|
223 | Transform an array into a JSON string. |
---|
224 | |
---|
225 | *Parameters* |
---|
226 | |
---|
227 | ``array {Array}`` The array to be serialized |
---|
228 | |
---|
229 | *Returns* |
---|
230 | |
---|
231 | ``{String}`` A JSON string representing the array. |
---|
232 | |
---|
233 | .. _serialize.string: |
---|
234 | |
---|
235 | serialize.string |
---|
236 | Transform a string into a JSON string. |
---|
237 | |
---|
238 | *Parameters* |
---|
239 | |
---|
240 | ``string {String}`` The string to be serialized |
---|
241 | |
---|
242 | *Returns* |
---|
243 | |
---|
244 | ``{String}`` A JSON string representing the string. |
---|
245 | |
---|
246 | .. _serialize.number: |
---|
247 | |
---|
248 | serialize.number |
---|
249 | Transform a number into a JSON string. |
---|
250 | |
---|
251 | *Parameters* |
---|
252 | |
---|
253 | ``number {Number}`` The number to be serialized. |
---|
254 | |
---|
255 | *Returns* |
---|
256 | |
---|
257 | ``{String}`` A JSON string representing the number. |
---|
258 | |
---|
259 | .. _serialize.boolean: |
---|
260 | |
---|
261 | serialize.boolean |
---|
262 | Transform a boolean into a JSON string. |
---|
263 | |
---|
264 | *Parameters* |
---|
265 | |
---|
266 | ``bool {Boolean}`` The boolean to be serialized. |
---|
267 | |
---|
268 | *Returns* |
---|
269 | |
---|
270 | ``{String}`` A JSON string representing the boolean. |
---|
271 | |
---|
272 | .. _serialize.date: |
---|
273 | |
---|
274 | serialize.date |
---|
275 | Transform a date into a JSON string. |
---|
276 | |
---|
277 | *Parameters* |
---|
278 | |
---|
279 | ``date {Date}`` The date to be serialized. |
---|
280 | |
---|
281 | *Returns* |
---|
282 | |
---|
283 | ``{String}`` A JSON string representing the date. |
---|