source: cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/api/json-parse.js.html @ 5259

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/api/json-parse.js.html@5259
Revision 5259, 17.3 KB checked in by domlowe, 11 years ago (diff)

adding yui javascript

Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2<html>
3<head>
4        <title>API: json   json-parse.js  (YUI Library)</title>
5        <link rel="stylesheet" type="text/css" href="assets/api.css">
6    <script type="text/javascript" src="assets/api-js"></script>
7    <script type="text/javascript" src="assets/ac-js"></script>
8</head>
9
10<body id="yahoo-com">
11
12<div id="doc3" class="yui-t2">
13
14        <div id="hd">
15        <a href="http://developer.yahoo.com/yui/"><h1>Yahoo! UI Library</h1></a>
16        <h3>json&nbsp; <span class="subtitle">3.0.0pr1</span></h3>
17        <p>
18        <a href="./index.html">Yahoo! UI Library</a> 
19            &gt; <a href="./module_json.html">json</a>
20               
21                 &gt; json-parse.js (source view)
22            </p>
23
24
25        </div>
26
27        <div id="bd">
28                <div id="yui-main">
29                        <div class="yui-b">
30            <form name="yui-classopts-form">
31    <span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> <label for="showprivate">Show Private</label></span>
32    <span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> <label for="showprotected">Show Protected</label></span>
33    <span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> <label for="showdeprecated">Show Deprecated</label></span>
34            </form>
35
36                    <div id="srcout">
37                        <style>
38                            #doc3 #classopts { display:none; }
39                        </style>
40<div class="highlight" ><pre><span class="c">/**</span>
41<span class="c"> * The JSON Utility provides methods to serialize JavaScript objects into</span>
42<span class="c"> * JSON strings and parse JavaScript objects from strings containing JSON data.</span>
43<span class="c"> * Three modules are available for inclusion:</span>
44<span class="c"> * &lt;ol&gt;</span>
45<span class="c"> * &lt;li&gt;1. &lt;code&gt;json-parse&lt;/code&gt; for parsing JSON strings into native JavaScript data&lt;/li&gt;</span>
46<span class="c"> * &lt;li&gt;2. &lt;code&gt;json-stringify&lt;/code&gt; for stringification of JavaScript objects into JSON strings&lt;/li&gt;</span>
47<span class="c"> * &lt;li&gt;3. &lt;code&gt;json&lt;/code&gt; for both parsing and stringification&lt;/li&gt;</span>
48<span class="c"> * &lt;/ol&gt;</span>
49<span class="c"> * </span>
50<span class="c"> * Both &lt;code&gt;json-parse&lt;/code&gt; and &lt;code&gt;json-stringify&lt;/code&gt; create functions in a static JSON class under your YUI instance (e.g. Y.JSON.parse(..)).</span>
51<span class="c"> * @module json</span>
52<span class="c"> * @class JSON</span>
53<span class="c"> * @static</span>
54<span class="c"> */</span>
55
56<span class="c">/**</span>
57<span class="c"> * Provides Y.JSON.parse method to take JSON strings and return native</span>
58<span class="c"> * JavaScript objects.</span>
59<span class="c"> * @module json</span>
60<span class="c"> * @submodule json-parse</span>
61<span class="c"> * @for JSON</span>
62<span class="c"> * @static</span>
63<span class="c"> */</span>
64<span class="nx">Y</span><span class="o">.</span><span class="nx">JSON</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">JSON</span> <span class="o">||</span> <span class="o">{};</span>
65
66<span class="c">// All internals kept private for security reasons</span>
67<span class="c"></span>
68<span class="c">/**</span>
69<span class="c"> * Replace certain Unicode characters that JavaScript may handle incorrectly</span>
70<span class="c"> * during eval--either by deleting them or treating them as line endings--with</span>
71<span class="c"> * escae sequences.</span>
72<span class="c"> * IMPORTANT NOTE: This regex will be used to modify the input if a match is</span>
73<span class="c"> * found.</span>
74<span class="c"> * @property _UNICODE_EXCEPTIONS</span>
75<span class="c"> * @type {RegExp}</span>
76<span class="c"> * @private</span>
77<span class="c"> */</span>
78<span class="k">var</span> <span class="nx">_UNICODE_EXCEPTIONS</span> <span class="o">=</span> <span class="sr">/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g</span><span class="o">,</span>
79
80
81<span class="c">/**</span>
82<span class="c"> * First step in the validation.  Regex used to replace all escape</span>
83<span class="c"> * sequences (i.e. &quot;\\&quot;, etc) with &#39;@&#39; characters (a non-JSON character).</span>
84<span class="c"> * @property _ESCAPES</span>
85<span class="c"> * @type {RegExp}</span>
86<span class="c"> * @private</span>
87<span class="c"> */</span>
88    <span class="nx">_ESCAPES</span> <span class="o">=</span> <span class="sr">/\\(?:[&quot;\\\/bfnrt]|u[0-9a-fA-F]{4})/g</span><span class="o">,</span>
89
90<span class="c">/**</span>
91<span class="c"> * Second step in the validation.  Regex used to replace all simple</span>
92<span class="c"> * values with &#39;]&#39; characters.</span>
93<span class="c"> * @property _VALUES</span>
94<span class="c"> * @type {RegExp}</span>
95<span class="c"> * @private</span>
96<span class="c"> */</span>
97    <span class="nx">_VALUES</span>  <span class="o">=</span> <span class="sr">/&quot;[^&quot;\\\n\r]*&quot;|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g</span><span class="o">,</span>
98
99<span class="c">/**</span>
100<span class="c"> * Third step in the validation.  Regex used to remove all open square</span>
101<span class="c"> * brackets following a colon, comma, or at the beginning of the string.</span>
102<span class="c"> * @property _BRACKETS</span>
103<span class="c"> * @type {RegExp}</span>
104<span class="c"> * @private</span>
105<span class="c"> */</span>
106
107    <span class="nx">_BRACKETS</span> <span class="o">=</span> <span class="sr">/(?:^|:|,)(?:\s*\[)+/g</span><span class="o">,</span>
108
109<span class="c">/**</span>
110<span class="c"> * Final step in the validation.  Regex used to test the string left after</span>
111<span class="c"> * all previous replacements for invalid characters.</span>
112<span class="c"> * @property _INVALID</span>
113<span class="c"> * @type {RegExp}</span>
114<span class="c"> * @private</span>
115<span class="c"> */</span>
116    <span class="nx">_INVALID</span>  <span class="o">=</span> <span class="sr">/^[\],:{}\s]*$/</span><span class="o">,</span>
117
118    <span class="nx">has</span> <span class="o">=</span> <span class="nb">Object</span><span class="o">.</span><span class="nx">prototype</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">,</span>
119<span class="c">/**</span>
120<span class="c"> * Traverses nested objects, applying a reviver function to each (key,value)</span>
121<span class="c"> * from the scope if the key:value&#39;s containing object.  The value returned</span>
122<span class="c"> * from the function will replace the original value in the key:value pair.</span>
123<span class="c"> * If the value returned is undefined, the key will be omitted from the</span>
124<span class="c"> * returned object.</span>
125<span class="c"> * @method _revive</span>
126<span class="c"> * @param data {MIXED} Any JavaScript data</span>
127<span class="c"> * @param reviver {Function} filter or mutation function</span>
128<span class="c"> * @return {MIXED} The results of the filtered data</span>
129<span class="c"> * @private</span>
130<span class="c"> */</span>
131    <span class="nx">_revive</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">data</span><span class="o">,</span> <span class="nx">reviver</span><span class="o">)</span> <span class="o">{</span>
132        <span class="k">var</span> <span class="nx">walk</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">key</span><span class="o">)</span> <span class="o">{</span>
133            <span class="k">var</span> <span class="nx">k</span><span class="o">,</span><span class="nx">v</span><span class="o">,</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">key</span><span class="o">];</span>
134            <span class="k">if</span> <span class="o">(</span><span class="nx">value</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">value</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span><span class="o">)</span> <span class="o">{</span>
135                <span class="k">for</span> <span class="o">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">value</span><span class="o">)</span> <span class="o">{</span>
136                    <span class="k">if</span> <span class="o">(</span><span class="nx">has</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">value</span><span class="o">,</span><span class="nx">k</span><span class="o">))</span> <span class="o">{</span>
137                        <span class="nx">v</span> <span class="o">=</span> <span class="nx">walk</span><span class="o">(</span><span class="nx">value</span><span class="o">,</span> <span class="nx">k</span><span class="o">);</span>
138                        <span class="k">if</span> <span class="o">(</span><span class="nx">v</span> <span class="o">===</span> <span class="kc">undefined</span><span class="o">)</span> <span class="o">{</span>
139                            <span class="nx">delete</span> <span class="nx">value</span><span class="o">[</span><span class="nx">k</span><span class="o">];</span>
140                        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
141                            <span class="nx">value</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
142                        <span class="o">}</span>
143                    <span class="o">}</span>
144                <span class="o">}</span>
145            <span class="o">}</span>
146            <span class="k">return</span> <span class="nx">reviver</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">key</span><span class="o">,</span><span class="nx">value</span><span class="o">);</span>
147        <span class="o">};</span>
148
149        <span class="k">return</span> <span class="k">typeof</span> <span class="nx">reviver</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">walk</span><span class="o">({</span><span class="s1">&#39;&#39;</span><span class="o">:</span><span class="nx">data</span><span class="o">},</span><span class="s1">&#39;&#39;</span><span class="o">)</span> <span class="o">:</span> <span class="nx">data</span><span class="o">;</span>
150    <span class="o">};</span>
151
152<span class="c">/**</span>
153<span class="c"> * Parse a JSON string, returning the native JavaScript representation.</span>
154<span class="c"> * @param s {string} JSON string data</span>
155<span class="c"> * @param reviver {function} (optional) function(k,v) passed each key value pair of object literals, allowing pruning or altering values</span>
156<span class="c"> * @return {MIXED} the native JavaScript representation of the JSON string</span>
157<span class="c"> * @throws SyntaxError</span>
158<span class="c"> * @method parse</span>
159<span class="c"> * @static</span>
160<span class="c"> * @public</span>
161<span class="c"> */</span>
162<span class="nx">Y</span><span class="o">.</span><span class="nx">JSON</span><span class="o">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">s</span><span class="o">,</span><span class="nx">reviver</span><span class="o">)</span> <span class="o">{</span>
163    <span class="c">// Ensure valid JSON</span>
164<span class="c"></span>    <span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">s</span> <span class="o">===</span> <span class="s1">&#39;string&#39;</span><span class="o">)</span> <span class="o">{</span>
165        <span class="c">// Replace certain Unicode characters that are otherwise handled</span>
166<span class="c"></span>        <span class="c">// incorrectly by some browser implementations.</span>
167<span class="c"></span>        <span class="c">// NOTE: This modifies the input if such characters are found!</span>
168<span class="c"></span>        <span class="nx">s</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="nx">_UNICODE_EXCEPTIONS</span><span class="o">,</span> <span class="k">function</span> <span class="o">(</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
169            <span class="k">return</span> <span class="s1">&#39;\\u&#39;</span><span class="o">+(</span><span class="s1">&#39;0000&#39;</span><span class="o">+(+(</span><span class="nx">c</span><span class="o">.</span><span class="nx">charCodeAt</span><span class="o">(</span><span class="m">0</span><span class="o">))).</span><span class="nx">toString</span><span class="o">(</span><span class="m">16</span><span class="o">)).</span><span class="nx">slice</span><span class="o">(-</span><span class="m">4</span><span class="o">);</span>
170        <span class="o">});</span>
171       
172        <span class="c">// Test for validity</span>
173<span class="c"></span>        <span class="k">if</span> <span class="o">(</span><span class="nx">_INVALID</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">s</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="nx">_ESCAPES</span><span class="o">,</span><span class="s1">&#39;@&#39;</span><span class="o">).</span>
174                            <span class="nx">replace</span><span class="o">(</span><span class="nx">_VALUES</span><span class="o">,</span><span class="s1">&#39;]&#39;</span><span class="o">).</span>
175                            <span class="nx">replace</span><span class="o">(</span><span class="nx">_BRACKETS</span><span class="o">,</span><span class="s1">&#39;&#39;</span><span class="o">)))</span> <span class="o">{</span>
176
177            <span class="c">// Eval the text into a JavaScript data structure, apply any</span>
178<span class="c"></span>            <span class="c">// reviver function, and return</span>
179<span class="c"></span>            <span class="k">return</span> <span class="nx">_revive</span><span class="o">(</span> <span class="nb">eval</span><span class="o">(</span><span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">s</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="o">),</span> <span class="nx">reviver</span> <span class="o">);</span>
180        <span class="o">}</span>
181    <span class="o">}</span>
182
183    <span class="c">// The text is not JSON parsable</span>
184<span class="c"></span>    <span class="k">throw</span> <span class="k">new</span> <span class="nx">SyntaxError</span><span class="o">(</span><span class="s1">&#39;parseJSON&#39;</span><span class="o">);</span>
185<span class="o">};</span>
186</pre></div>
187                    </div>
188                        </div>
189                </div>
190                <div class="yui-b">
191            <div class="nav">
192
193                    <div class="module">
194                        <h4>Modules</h4>
195                        <ul class="content">
196
197                                <li class=""><a href="module_anim.html">anim</a></li>
198
199                                <li class=""><a href="module_attribute.html">attribute</a></li>
200
201                                <li class=""><a href="module_base.html">base</a></li>
202
203                                <li class=""><a href="module_cookie.html">cookie</a></li>
204
205                                <li class=""><a href="module_dd.html">dd</a></li>
206
207                                <li class=""><a href="module_dd-plugin.html">dd-plugin</a></li>
208
209                                <li class=""><a href="module_dom.html">dom</a></li>
210
211                                <li class=""><a href="module_dump.html">dump</a></li>
212
213                                <li class=""><a href="module_event.html">event</a></li>
214
215                                <li class=""><a href="module_io.html">io</a></li>
216
217                                <li class="selected"><a href="module_json.html">json</a></li>
218
219                                <li class=""><a href="module_node.html">node</a></li>
220
221                                <li class=""><a href="module_oop.html">oop</a></li>
222
223                                <li class=""><a href="module_queue.html">queue</a></li>
224
225                                <li class=""><a href="module_substitute.html">substitute</a></li>
226
227                                <li class=""><a href="module_yui.html">yui</a></li>
228                        </ul>
229                    </div>
230
231                    <div class="module">
232                        <h4>Classes</h4>
233                        <ul class="content">
234                                <li class=""><a href="JSON.html">JSON</a></li>
235                        </ul>
236                    </div>
237
238                    <div class="module">
239                        <h4>Files</h4>
240                        <ul class="content">       
241                                <li class="selected"><a href="json-parse.js.html">json-parse.js</a></li>
242                                <li class=""><a href="json-stringify.js.html">json-stringify.js</a></li>
243                        </ul>
244                    </div>
245
246
247
248
249
250            </div>
251                </div>
252        </div>
253        <div id="ft">
254        <hr />
255        Copyright &copy; 2008 Yahoo! Inc. All rights reserved.
256        </div>
257</div>
258</body>
259</html>
Note: See TracBrowser for help on using the repository browser.