|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>JSON</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>JSON</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro component"> |
|
|
31 |
<p> |
|
|
32 |
The JSON module is an implementation of the ECMA 5 specification for |
|
|
33 |
transforming data to and from <a |
|
|
34 |
href="http://en.wikipedia.org/wiki/JSON">JavaScript Object |
|
|
35 |
Notation</a>. JSON is a safe, efficient, and reliable data interchange |
|
|
36 |
format. This module provides a JavaScript implementation of the spec, |
|
|
37 |
based on <a |
|
|
38 |
href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">Douglas |
|
|
39 |
Crockford's json2.js</a>. For browsers with native support it defers |
|
|
40 |
to the native implementation. |
|
|
41 |
</p> |
|
|
42 |
</div> |
|
|
43 |
|
|
|
44 |
<h2 id="getting-started">Getting Started</h2> |
|
|
45 |
|
|
|
46 |
<p> |
|
|
47 |
To include the source files for JSON and its dependencies, first load |
|
|
48 |
the YUI seed file if you haven't already loaded it. |
|
|
49 |
</p> |
|
|
50 |
|
|
|
51 |
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre> |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
<p> |
|
|
55 |
Next, create a new YUI instance for your application and populate it with the |
|
|
56 |
modules you need by specifying them as arguments to the <code>YUI().use()</code> method. |
|
|
57 |
YUI will automatically load any dependencies required by the modules you |
|
|
58 |
specify. |
|
|
59 |
</p> |
|
|
60 |
|
|
|
61 |
<pre class="code prettyprint"><script> |
|
|
62 |
// Create a new YUI instance and populate it with the required modules. |
|
|
63 |
YUI().use('json-parse', 'json-stringify', function (Y) { |
|
|
64 |
// JSON is available and ready for use. Add implementation |
|
|
65 |
// code here. |
|
|
66 |
}); |
|
|
67 |
</script></pre> |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
<p> |
|
|
71 |
For more information on creating YUI instances and on the |
|
|
72 |
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the |
|
|
73 |
documentation for the <a href="../yui/index.html">YUI Global Object</a>. |
|
|
74 |
</p> |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
<h2 id="using">Using the JSON Utility</h2> |
|
|
78 |
|
|
|
79 |
<h3 id="modules">JSON module overview</h3> |
|
|
80 |
|
|
|
81 |
<p> |
|
|
82 |
The JSON module adds the namespace <code>JSON</code> to your YUI instance. |
|
|
83 |
Its methods are static, available from this namespace. |
|
|
84 |
</p> |
|
|
85 |
|
|
|
86 |
<p> |
|
|
87 |
To minimize the code footprint when some functionality is not required, |
|
|
88 |
JSON has been broken up into the following modules: |
|
|
89 |
</p> |
|
|
90 |
|
|
|
91 |
<dl> |
|
|
92 |
<dt><code>json-parse</code></dt> |
|
|
93 |
<dd> |
|
|
94 |
Adds <code>Y.JSON.parse(..)</code> method to the YUI instance. Use |
|
|
95 |
this module if all you will be doing is parsing JSON strings. |
|
|
96 |
</dd> |
|
|
97 |
|
|
|
98 |
<dt><code>json-stringify</code></dt> |
|
|
99 |
<dd> |
|
|
100 |
Adds <code>Y.JSON.stringify(..)</code> method and its supporting |
|
|
101 |
methods and properties to the YUI instance. Use this module if all you |
|
|
102 |
will be doing is serializing JavaScript objects to JSON strings. |
|
|
103 |
</dd> |
|
|
104 |
|
|
|
105 |
<dt><code>json</code></dt> |
|
|
106 |
<dd> |
|
|
107 |
A rollup of <code>json-parse</code> and <code>json-stringify</code> |
|
|
108 |
modules. Use this if you need to both parse JSON strings and serialize |
|
|
109 |
objects to JSON strings. |
|
|
110 |
</dd> |
|
|
111 |
</dl> |
|
|
112 |
|
|
|
113 |
<h3 id="parse">Parsing JSON strings into native JavaScript values</h3> |
|
|
114 |
|
|
|
115 |
<p> |
|
|
116 |
Provided a string containing data in JSON format, simply pass the string to |
|
|
117 |
<code>parse</code> and capture the return value. |
|
|
118 |
</p> |
|
|
119 |
|
|
|
120 |
<pre class="code prettyprint">YUI().use('json-parse', function (Y) { |
|
|
121 |
|
|
|
122 |
var jsonString = '{"products":['+ |
|
|
123 |
'{"id":1,"price":0.99,"inStock":true,"name":"grapes"},'+ |
|
|
124 |
'{"id":2,"price":3.5,"inStock":false,"name":"passion fruit"},'+ |
|
|
125 |
'{"id":3,"price":2.5,"inStock":true,"name":"bananas"}'+ |
|
|
126 |
']}'; |
|
|
127 |
|
|
|
128 |
// JSON.parse throws a SyntaxError when passed invalid JSON |
|
|
129 |
try { |
|
|
130 |
var data = Y.JSON.parse(jsonString); |
|
|
131 |
} |
|
|
132 |
catch (e) { |
|
|
133 |
alert("Invalid product data"); |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
// We can now interact with the data |
|
|
137 |
for (var i = data.products.length - 1; i >= 0; --i) { |
|
|
138 |
var p = data.products[i]; |
|
|
139 |
if (p.price < 1) { |
|
|
140 |
p.price += 1; // Price increase! |
|
|
141 |
} |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
});</pre> |
|
|
145 |
|
|
|
146 |
|
|
|
147 |
<h4 id="reviver">Using the "reviver" parameter</h4> |
|
|
148 |
|
|
|
149 |
<p> |
|
|
150 |
The optional second parameter to <code>parse</code> accepts a function that |
|
|
151 |
will be executed on each member of the parsed JavaScript object. Each call |
|
|
152 |
to the reviver function is passed the key and associated value, and is |
|
|
153 |
executed from the context of the object containing the key. If the return |
|
|
154 |
value of the reviver is <code>undefined</code>, the key will be omitted |
|
|
155 |
from the returned object. |
|
|
156 |
</p> |
|
|
157 |
|
|
|
158 |
<p> |
|
|
159 |
Typical uses of reviver functions are filtering, formatting, and value |
|
|
160 |
conversion. |
|
|
161 |
</p> |
|
|
162 |
|
|
|
163 |
|
|
|
164 |
<pre class="code prettyprint">YUI().use('json-parse', function (Y) { |
|
|
165 |
|
|
|
166 |
var jsonString = '{"products":['+ |
|
|
167 |
'{"id":1,"price":0.99,"inStock":true,"name":"grapes"},'+ |
|
|
168 |
'{"id":2,"price":3.5,"inStock":false,"name":"passion fruit"},'+ |
|
|
169 |
'{"id":3,"price":2.5,"inStock":true,"name":"bananas"}'+ |
|
|
170 |
']}'; |
|
|
171 |
|
|
|
172 |
// Remove all out of stock entries and bananas. Format prices. |
|
|
173 |
var currencySymbol = '$'; |
|
|
174 |
var reviver = function (key,val) { |
|
|
175 |
if (key === 'inStock' && !val) { |
|
|
176 |
return undefined; |
|
|
177 |
} else if (val === 'bananas') { |
|
|
178 |
return undefined; |
|
|
179 |
} else if (key === 'price') { |
|
|
180 |
val += val % 1 ? "0" : ".00"; |
|
|
181 |
var pIdx = val.indexOf('.'); |
|
|
182 |
val = pIdx ? "0" + val : val; |
|
|
183 |
val = currencySymbol + val.substr(0,pIdx + 3); |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
return val; |
|
|
187 |
}; |
|
|
188 |
|
|
|
189 |
// JSON.parse throws a SyntaxError when passed invalid JSON |
|
|
190 |
try { |
|
|
191 |
var data = Y.JSON.parse(jsonString,reviver); |
|
|
192 |
} |
|
|
193 |
catch (e) { |
|
|
194 |
alert("Invalid product data"); |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
// We can now interact with the data |
|
|
198 |
alert(data.products.length); // 1 |
|
|
199 |
alert(data.products[0].price); // $0.99 |
|
|
200 |
|
|
|
201 |
});</pre> |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
<h4 id="avoid_eval">A word of caution against using <code>eval</code></h4> |
|
|
205 |
|
|
|
206 |
<p> |
|
|
207 |
JSON data format is a <em>subset</em> of JavaScript notation, meaning that |
|
|
208 |
it is possible to use JavaScript <code>eval</code> to transform JSON data |
|
|
209 |
to live data. However, it is unsafe practice to assume that data reaching |
|
|
210 |
your code is not malicious. <code>eval</code> is capable of executing |
|
|
211 |
JavaScript's full syntax, including calling functions and accessing cookies |
|
|
212 |
with all the privileges of a <code><script></code>. To ensure that |
|
|
213 |
the data is safe, the JSON module's <code>parse</code> method inspects the |
|
|
214 |
incoming string (using methods derived from Douglas Crockford's <a |
|
|
215 |
href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">json2.js</a>) |
|
|
216 |
and verifies that it is safe before giving it the green light to parse. |
|
|
217 |
</p> |
|
|
218 |
|
|
|
219 |
<pre class="code prettyprint">// UNSAFE |
|
|
220 |
var data = eval('(' + shouldBeJsonData + ')'); |
|
|
221 |
|
|
|
222 |
// Safe |
|
|
223 |
var data = Y.JSON.parse(shouldBeJsonData);</pre> |
|
|
224 |
|
|
|
225 |
|
|
|
226 |
<h3 id="stringify">Creating JSON strings from JavaScript objects</h3> |
|
|
227 |
|
|
|
228 |
<p> |
|
|
229 |
To convert a JavaScript object (or any permissable value) to a JSON string, |
|
|
230 |
pass that object to <code>Y.JSON.stringify</code> and capture the returned |
|
|
231 |
string. |
|
|
232 |
</p> |
|
|
233 |
|
|
|
234 |
<pre class="code prettyprint">YUI().use("json-stringify", function (Y) { |
|
|
235 |
|
|
|
236 |
var myData = { |
|
|
237 |
troop : [ |
|
|
238 |
{ name: "Ashley", age: 12 }, |
|
|
239 |
{ name: "Abby", age:9 } |
|
|
240 |
] |
|
|
241 |
}; |
|
|
242 |
|
|
|
243 |
var jsonStr = Y.JSON.stringify(myData); |
|
|
244 |
|
|
|
245 |
alert(jsonStr); // {"troop":[{"name":"Ashley","age":12},{"name":"Abby","age":9}]} |
|
|
246 |
});</pre> |
|
|
247 |
|
|
|
248 |
|
|
|
249 |
<h4 id="whitelist">Using a whitelist</h4> |
|
|
250 |
|
|
|
251 |
<p> |
|
|
252 |
To serialize only a fixed subset of keys, provide an array of key names as |
|
|
253 |
the second parameter to <code>stringify</code>. |
|
|
254 |
</p> |
|
|
255 |
|
|
|
256 |
<pre class="code prettyprint">YUI().use("json-stringify", function (Y) { |
|
|
257 |
|
|
|
258 |
// a detailed object record set |
|
|
259 |
var records = [ |
|
|
260 |
{id:1, name: "Bob", age: 47, favorite_color: "blue"}, |
|
|
261 |
{id:2, name: "Sally", age: 30, favorite_color: "mauve"}, |
|
|
262 |
{id:3, name: "Tommy", age: 13, favorite_color: "black"}, |
|
|
263 |
{id:4, name: "Chaz", age: 26, favorite_color: "plaid"} |
|
|
264 |
]; |
|
|
265 |
|
|
|
266 |
// Use an array of acceptable object key names as a whitelist. |
|
|
267 |
// To create a JSON string with only age and id |
|
|
268 |
var jsonStr = Y.JSON.stringify(records,["id","age"]); |
|
|
269 |
|
|
|
270 |
alert(jsonStr); |
|
|
271 |
// [{"id":1,"age":47},{"id":2,"age":30},{"id":3,"age":13},{"id":4,"age":26}] |
|
|
272 |
|
|
|
273 |
});</pre> |
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<h4 id="replacer">Using a custom "replacer" function</h4> |
|
|
277 |
|
|
|
278 |
<p> |
|
|
279 |
For more granular control of how values in your object are serialized, you |
|
|
280 |
can pass a replacer function as the second parameter to |
|
|
281 |
<code>stringify</code>. The replacer will be called for each key value |
|
|
282 |
pair, and executed from the context of the key's host object. The return |
|
|
283 |
value of the replacer will be serialized in place of the raw value. |
|
|
284 |
</p> |
|
|
285 |
|
|
|
286 |
<pre class="code prettyprint">YUI().use("json-stringify", function (Y) { |
|
|
287 |
|
|
|
288 |
// a detailed object record set |
|
|
289 |
var records = [ |
|
|
290 |
{id:1, name: "Bob", birthdate: "2/27/1964", favorite_color: "blue"}, |
|
|
291 |
{id:2, name: "Sally", birthdate: "9/30/1983", favorite_color: "mauve"}, |
|
|
292 |
{id:3, name: "Tommy", birthdate: "3/11/1990", favorite_color: "black"}, |
|
|
293 |
{id:4, name: "Chaz", birthdate: "5/22/1975", favorite_color: "plaid"} |
|
|
294 |
]; |
|
|
295 |
|
|
|
296 |
// Create a replacer to blacklist the id and convert the birthdate to a Date |
|
|
297 |
var replacer = function (key,val) { |
|
|
298 |
// blacklist id and favorite_color |
|
|
299 |
if (key === 'id' || key === 'favorite_color') { |
|
|
300 |
return undefined; |
|
|
301 |
|
|
|
302 |
// convert birthdate to a Date instance (serialized as UTC date string) |
|
|
303 |
} else if (key === 'birthdate') { |
|
|
304 |
var d = new Date(), |
|
|
305 |
bd = val.split('/'); |
|
|
306 |
d.setFullYear(bd[2],bd[0]-1,bd[1]); |
|
|
307 |
d.setHours(0,0,0); |
|
|
308 |
return d; |
|
|
309 |
} |
|
|
310 |
|
|
|
311 |
return val; |
|
|
312 |
}; |
|
|
313 |
|
|
|
314 |
var jsonStr = Y.JSON.stringify(records,replacer); |
|
|
315 |
|
|
|
316 |
alert(jsonStr); |
|
|
317 |
// [{"name":"Bobby","birthdate":"1964-02-28T08:00:00Z"},{"name":"Sally","birthdate":"1983-09-30T07:00:00Z"},{"name":"Tommy","birthdate":"1990-03-11T08:00:00Z"},{"name":"Chaz","birthdate":"1975-05-23T07:00:00Z"}] |
|
|
318 |
|
|
|
319 |
});</pre> |
|
|
320 |
|
|
|
321 |
|
|
|
322 |
<h4 id="format">Formatting JSON output</h4> |
|
|
323 |
|
|
|
324 |
<p> |
|
|
325 |
To create readable JSON, pass a string or number as the third parameter to |
|
|
326 |
<code>stringify</code>. Object and Array members will be separated with |
|
|
327 |
newlines and indented. If a string is supplied, that string will be used |
|
|
328 |
for the indentation. If a number is passed, that number of spaces will be |
|
|
329 |
used. |
|
|
330 |
</p> |
|
|
331 |
|
|
|
332 |
<pre class="code prettyprint">YUI().use('json-stringify', function (Y) { |
|
|
333 |
|
|
|
334 |
var fam = { |
|
|
335 |
family: "Franklin", |
|
|
336 |
children: [ "Bob", "Emily", "Sam" ] |
|
|
337 |
}; |
|
|
338 |
|
|
|
339 |
// format serialization with four spaces |
|
|
340 |
var jsonStr = Y.JSON.stringify(fam,null,4); |
|
|
341 |
|
|
|
342 |
alert(jsonStr); |
|
|
343 |
/* |
|
|
344 |
{ |
|
|
345 |
"family": "Franklin", |
|
|
346 |
"children": [ |
|
|
347 |
"Bob", |
|
|
348 |
"Emily", |
|
|
349 |
"Sam" |
|
|
350 |
] |
|
|
351 |
} |
|
|
352 |
*/ |
|
|
353 |
|
|
|
354 |
});</pre> |
|
|
355 |
|
|
|
356 |
|
|
|
357 |
<h3 id="errors">Catching JSON errors</h3> |
|
|
358 |
|
|
|
359 |
<p> |
|
|
360 |
ECMA 5 states that <code>parse</code> should throw an error when an invalid |
|
|
361 |
JSON string is input. It also states that <code>stringify</code> should |
|
|
362 |
throw an error when an object with cyclical references is input. |
|
|
363 |
</p> |
|
|
364 |
|
|
|
365 |
<p> |
|
|
366 |
For this reason, it is recommended that both <code>parse</code> and |
|
|
367 |
<code>stringify</code> be called from within |
|
|
368 |
<code>try</code>/<code>catch</code> blocks. |
|
|
369 |
</p> |
|
|
370 |
|
|
|
371 |
<pre class="code prettyprint">try { |
|
|
372 |
// BOOM |
|
|
373 |
Y.JSON.parse("{'this string': is, not_valid: ['J','S','O','N'] }"); |
|
|
374 |
} |
|
|
375 |
catch (e) { |
|
|
376 |
alert("A string may eval to the same object, but might not be valid JSON"); |
|
|
377 |
} |
|
|
378 |
|
|
|
379 |
// This is safe to stringify |
|
|
380 |
var myData = { |
|
|
381 |
troop : [ |
|
|
382 |
{ name: "Ashley", age: 12 }, |
|
|
383 |
{ name: "Abby", age:9 } |
|
|
384 |
] |
|
|
385 |
}; |
|
|
386 |
|
|
|
387 |
// Create a cyclical reference |
|
|
388 |
myData.troop[0]["NEWKEY"] = myData; |
|
|
389 |
|
|
|
390 |
try { |
|
|
391 |
// BOOM |
|
|
392 |
jsonStr = Y.JSON.stringify(myData); |
|
|
393 |
} catch (e) { |
|
|
394 |
alert("Cyclical references can sneak in. Remember to wrap stringify."); |
|
|
395 |
}</pre> |
|
|
396 |
|
|
|
397 |
|
|
|
398 |
<h3 id="native">Notes about native JSON support</h3> |
|
|
399 |
|
|
|
400 |
<p> |
|
|
401 |
Native JSON support in JavaScript is defined in the ECMAScript 5 |
|
|
402 |
specification, which was finalized in September 2009. However, most of the |
|
|
403 |
major browser vendors had already implemented this feature in their |
|
|
404 |
JavaScript engines while the spec was still in draft. As |
|
|
405 |
a result of the timing and the fact that native JSON is a new feature, |
|
|
406 |
there are gotchas involved in leveraging the disparate native |
|
|
407 |
behaviors in certain browsers. |
|
|
408 |
</p> |
|
|
409 |
|
|
|
410 |
<p> |
|
|
411 |
In general, it is preferable to use the native behavior for |
|
|
412 |
<code>JSON.parse</code> as it is much faster and safer than any JavaScript |
|
|
413 |
implementation. There are also very few known critical issues with |
|
|
414 |
supporting browsers. |
|
|
415 |
</p> |
|
|
416 |
|
|
|
417 |
<p> |
|
|
418 |
Stringify has more pitfalls and inconsistencies, but they may not affect |
|
|
419 |
your use cases. As with <code>parse</code>, the native implementation of |
|
|
420 |
<code>stringify</code> is faster, but only marginally so with reasonably |
|
|
421 |
sized input. In most cases, choosing the JavaScript implementation will |
|
|
422 |
not affect performance and may be preferable for its cross browser |
|
|
423 |
consistency. |
|
|
424 |
</p> |
|
|
425 |
|
|
|
426 |
|
|
|
427 |
<p> |
|
|
428 |
As noted above, the JSON module will leverage native behavior in |
|
|
429 |
implementing browsers by default. However, you can choose to opt out of |
|
|
430 |
leveraging native <code>parse</code> or <code>stringify</code> by setting |
|
|
431 |
the <code>useNativeJSONParse</code> and |
|
|
432 |
<code>useNativeJSONStringify</code> configuration options. |
|
|
433 |
</p> |
|
|
434 |
|
|
|
435 |
<pre class="code prettyprint">YUI({ |
|
|
436 |
// Always use the JavaScript implementation for parsing |
|
|
437 |
useNativeJSONParse: false, |
|
|
438 |
|
|
|
439 |
// Always use the JavaScript implementation for stringifying |
|
|
440 |
useNativeJSONStringify: false |
|
|
441 |
}).use('json', function (Y) { |
|
|
442 |
//... |
|
|
443 |
});</pre> |
|
|
444 |
|
|
|
445 |
|
|
|
446 |
<h4 id="changes-in-390">Changes in 3.9.0</h4> |
|
|
447 |
|
|
|
448 |
<p> |
|
|
449 |
As of version 3.9.0 YUI has changed to loading the JavaScript |
|
|
450 |
implementation of JSON only when the browser does not provide a native |
|
|
451 |
API. You can no longer change the desired implementation at run-time |
|
|
452 |
by changing <code>Y.JSON.useNativeParse</code>. Since native JSON implementations |
|
|
453 |
have become much more stable this results in a smaller download |
|
|
454 |
for most users. You can still chose to use the JavaScript fallback |
|
|
455 |
by calling <code>Y.use('json-parse-shim')</code> or <code>Y.use('json-stringify-shim')</code>. |
|
|
456 |
</p> |
|
|
457 |
<h2 id="issues">Known Issues</h2> |
|
|
458 |
<ul class="spaced"> |
|
|
459 |
<li> |
|
|
460 |
<p>Native JSON.stringify treats regex values as a function on Android 2.3.</p> |
|
|
461 |
<pre class="code prettyprint">Y.JSON.stringify([{ |
|
|
462 |
"str" : "string", |
|
|
463 |
"arr" : [5, /some regex/] |
|
|
464 |
}]); |
|
|
465 |
//modern browsers return |
|
|
466 |
[{"str":"string","arr":[5,{}]}] |
|
|
467 |
//Android 2.3.4 returns |
|
|
468 |
[{"str":"string","arr":[5,null]}]</pre> |
|
|
469 |
|
|
|
470 |
</li> |
|
|
471 |
</ul> |
|
|
472 |
</div> |
|
|
473 |
</div> |
|
|
474 |
</div> |
|
|
475 |
|
|
|
476 |
<div class="yui3-u-1-4"> |
|
|
477 |
<div class="sidebar"> |
|
|
478 |
|
|
|
479 |
<div id="toc" class="sidebox"> |
|
|
480 |
<div class="hd"> |
|
|
481 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
482 |
</div> |
|
|
483 |
|
|
|
484 |
<div class="bd"> |
|
|
485 |
<ul class="toc"> |
|
|
486 |
<li> |
|
|
487 |
<a href="#getting-started">Getting Started</a> |
|
|
488 |
</li> |
|
|
489 |
<li> |
|
|
490 |
<a href="#using">Using the JSON Utility</a> |
|
|
491 |
<ul class="toc"> |
|
|
492 |
<li> |
|
|
493 |
<a href="#modules">JSON module overview</a> |
|
|
494 |
</li> |
|
|
495 |
<li> |
|
|
496 |
<a href="#parse">Parsing JSON strings into native JavaScript values</a> |
|
|
497 |
<ul class="toc"> |
|
|
498 |
<li> |
|
|
499 |
<a href="#reviver">Using the "reviver" parameter</a> |
|
|
500 |
</li> |
|
|
501 |
<li> |
|
|
502 |
<a href="#avoid_eval">A word of caution against using <code>eval</code></a> |
|
|
503 |
</li> |
|
|
504 |
</ul> |
|
|
505 |
</li> |
|
|
506 |
<li> |
|
|
507 |
<a href="#stringify">Creating JSON strings from JavaScript objects</a> |
|
|
508 |
<ul class="toc"> |
|
|
509 |
<li> |
|
|
510 |
<a href="#whitelist">Using a whitelist</a> |
|
|
511 |
</li> |
|
|
512 |
<li> |
|
|
513 |
<a href="#replacer">Using a custom "replacer" function</a> |
|
|
514 |
</li> |
|
|
515 |
<li> |
|
|
516 |
<a href="#format">Formatting JSON output</a> |
|
|
517 |
</li> |
|
|
518 |
</ul> |
|
|
519 |
</li> |
|
|
520 |
<li> |
|
|
521 |
<a href="#errors">Catching JSON errors</a> |
|
|
522 |
</li> |
|
|
523 |
<li> |
|
|
524 |
<a href="#native">Notes about native JSON support</a> |
|
|
525 |
<ul class="toc"> |
|
|
526 |
<li> |
|
|
527 |
<a href="#changes-in-390">Changes in 3.9.0</a> |
|
|
528 |
</li> |
|
|
529 |
</ul> |
|
|
530 |
</li> |
|
|
531 |
</ul> |
|
|
532 |
</li> |
|
|
533 |
<li> |
|
|
534 |
<a href="#issues">Known Issues</a> |
|
|
535 |
</li> |
|
|
536 |
</ul> |
|
|
537 |
</div> |
|
|
538 |
</div> |
|
|
539 |
|
|
|
540 |
|
|
|
541 |
|
|
|
542 |
<div class="sidebox"> |
|
|
543 |
<div class="hd"> |
|
|
544 |
<h2 class="no-toc">Examples</h2> |
|
|
545 |
</div> |
|
|
546 |
|
|
|
547 |
<div class="bd"> |
|
|
548 |
<ul class="examples"> |
|
|
549 |
|
|
|
550 |
|
|
|
551 |
<li data-description="Use JSON to parse data received via XMLHttpRequest via Y.io calls — a simple JSON use case."> |
|
|
552 |
<a href="json-connect.html">JSON with Y.io</a> |
|
|
553 |
</li> |
|
|
554 |
|
|
|
555 |
|
|
|
556 |
|
|
|
557 |
<li data-description="Use the replacer and reviver parameters to reconstitute object instances that have been serialized to JSON."> |
|
|
558 |
<a href="json-freeze-thaw.html">Rebuilding Class Instances from JSON Data</a> |
|
|
559 |
</li> |
|
|
560 |
|
|
|
561 |
|
|
|
562 |
|
|
|
563 |
<li data-description="Use a currency conversion calculation to add a new price member to a JSON response, demonstrating how JSON data, once retrieved, can be transformed during parsing."> |
|
|
564 |
<a href="json-convert-values.html">Adding New Object Members During Parsing</a> |
|
|
565 |
</li> |
|
|
566 |
|
|
|
567 |
|
|
|
568 |
|
|
|
569 |
|
|
|
570 |
|
|
|
571 |
|
|
|
572 |
</ul> |
|
|
573 |
</div> |
|
|
574 |
</div> |
|
|
575 |
|
|
|
576 |
|
|
|
577 |
|
|
|
578 |
<div class="sidebox"> |
|
|
579 |
<div class="hd"> |
|
|
580 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
581 |
</div> |
|
|
582 |
|
|
|
583 |
<div class="bd"> |
|
|
584 |
<ul class="examples"> |
|
|
585 |
|
|
|
586 |
|
|
|
587 |
|
|
|
588 |
|
|
|
589 |
|
|
|
590 |
|
|
|
591 |
|
|
|
592 |
|
|
|
593 |
<li data-description="A basic todo list built with the Model, Model List, and View components."> |
|
|
594 |
<a href="../app/app-todo.html">Todo List</a> |
|
|
595 |
</li> |
|
|
596 |
|
|
|
597 |
|
|
|
598 |
|
|
|
599 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
600 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
601 |
</li> |
|
|
602 |
|
|
|
603 |
|
|
|
604 |
</ul> |
|
|
605 |
</div> |
|
|
606 |
</div> |
|
|
607 |
|
|
|
608 |
</div> |
|
|
609 |
</div> |
|
|
610 |
</div> |
|
|
611 |
</div> |
|
|
612 |
|
|
|
613 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
614 |
<script>prettyPrint();</script> |
|
|
615 |
|
|
|
616 |
<script> |
|
|
617 |
YUI.Env.Tests = { |
|
|
618 |
examples: [], |
|
|
619 |
project: '../assets', |
|
|
620 |
assets: '../assets/json', |
|
|
621 |
name: 'json', |
|
|
622 |
title: 'JSON', |
|
|
623 |
newWindow: '', |
|
|
624 |
auto: false |
|
|
625 |
}; |
|
|
626 |
YUI.Env.Tests.examples.push('json-connect'); |
|
|
627 |
YUI.Env.Tests.examples.push('json-freeze-thaw'); |
|
|
628 |
YUI.Env.Tests.examples.push('json-convert-values'); |
|
|
629 |
YUI.Env.Tests.examples.push('app-todo'); |
|
|
630 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
631 |
|
|
|
632 |
</script> |
|
|
633 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
634 |
|
|
|
635 |
|
|
|
636 |
|
|
|
637 |
</body> |
|
|
638 |
</html> |