1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * Note: |
---|
8 | * This work draws heavily from the public domain JSON serializer/deserializer |
---|
9 | * at http://www.json.org/json.js. Rewritten so that it doesn't modify |
---|
10 | * basic data prototypes. |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * @requires OpenLayers/Format.js |
---|
15 | */ |
---|
16 | |
---|
17 | /** |
---|
18 | * Class: OpenLayers.Format.JSON |
---|
19 | * A parser to read/write JSON safely. Create a new instance with the |
---|
20 | * <OpenLayers.Format.JSON> constructor. |
---|
21 | * |
---|
22 | * Inherits from: |
---|
23 | * - <OpenLayers.Format> |
---|
24 | */ |
---|
25 | OpenLayers.Format.JSON = OpenLayers.Class(OpenLayers.Format, { |
---|
26 | |
---|
27 | /** |
---|
28 | * APIProperty: indent |
---|
29 | * {String} For "pretty" printing, the indent string will be used once for |
---|
30 | * each indentation level. |
---|
31 | */ |
---|
32 | indent: " ", |
---|
33 | |
---|
34 | /** |
---|
35 | * APIProperty: space |
---|
36 | * {String} For "pretty" printing, the space string will be used after |
---|
37 | * the ":" separating a name/value pair. |
---|
38 | */ |
---|
39 | space: " ", |
---|
40 | |
---|
41 | /** |
---|
42 | * APIProperty: newline |
---|
43 | * {String} For "pretty" printing, the newline string will be used at the |
---|
44 | * end of each name/value pair or array item. |
---|
45 | */ |
---|
46 | newline: "\n", |
---|
47 | |
---|
48 | /** |
---|
49 | * Property: level |
---|
50 | * {Integer} For "pretty" printing, this is incremented/decremented during |
---|
51 | * serialization. |
---|
52 | */ |
---|
53 | level: 0, |
---|
54 | |
---|
55 | /** |
---|
56 | * Property: pretty |
---|
57 | * {Boolean} Serialize with extra whitespace for structure. This is set |
---|
58 | * by the <write> method. |
---|
59 | */ |
---|
60 | pretty: false, |
---|
61 | |
---|
62 | /** |
---|
63 | * Constructor: OpenLayers.Format.JSON |
---|
64 | * Create a new parser for JSON. |
---|
65 | * |
---|
66 | * Parameters: |
---|
67 | * options - {Object} An optional object whose properties will be set on |
---|
68 | * this instance. |
---|
69 | */ |
---|
70 | initialize: function(options) { |
---|
71 | OpenLayers.Format.prototype.initialize.apply(this, [options]); |
---|
72 | }, |
---|
73 | |
---|
74 | /** |
---|
75 | * APIMethod: read |
---|
76 | * Deserialize a json string. |
---|
77 | * |
---|
78 | * Parameters: |
---|
79 | * json - {String} A JSON string |
---|
80 | * filter - {Function} A function which will be called for every key and |
---|
81 | * value at every level of the final result. Each value will be |
---|
82 | * replaced by the result of the filter function. This can be used to |
---|
83 | * reform generic objects into instances of classes, or to transform |
---|
84 | * date strings into Date objects. |
---|
85 | * |
---|
86 | * Returns: |
---|
87 | * {Object} An object, array, string, or number . |
---|
88 | */ |
---|
89 | read: function(json, filter) { |
---|
90 | /** |
---|
91 | * Parsing happens in three stages. In the first stage, we run the text |
---|
92 | * against a regular expression which looks for non-JSON |
---|
93 | * characters. We are especially concerned with '()' and 'new' |
---|
94 | * because they can cause invocation, and '=' because it can cause |
---|
95 | * mutation. But just to be safe, we will reject all unexpected |
---|
96 | * characters. |
---|
97 | */ |
---|
98 | try { |
---|
99 | if (/^[\],:{}\s]*$/.test(json.replace(/\\["\\\/bfnrtu]/g, '@'). |
---|
100 | replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). |
---|
101 | replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { |
---|
102 | |
---|
103 | /** |
---|
104 | * In the second stage we use the eval function to compile the |
---|
105 | * text into a JavaScript structure. The '{' operator is |
---|
106 | * subject to a syntactic ambiguity in JavaScript - it can |
---|
107 | * begin a block or an object literal. We wrap the text in |
---|
108 | * parens to eliminate the ambiguity. |
---|
109 | */ |
---|
110 | var object = eval('(' + json + ')'); |
---|
111 | |
---|
112 | /** |
---|
113 | * In the optional third stage, we recursively walk the new |
---|
114 | * structure, passing each name/value pair to a filter |
---|
115 | * function for possible transformation. |
---|
116 | */ |
---|
117 | if(typeof filter === 'function') { |
---|
118 | function walk(k, v) { |
---|
119 | if(v && typeof v === 'object') { |
---|
120 | for(var i in v) { |
---|
121 | if(v.hasOwnProperty(i)) { |
---|
122 | v[i] = walk(i, v[i]); |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | return filter(k, v); |
---|
127 | } |
---|
128 | object = walk('', object); |
---|
129 | } |
---|
130 | |
---|
131 | if(this.keepData) { |
---|
132 | this.data = object; |
---|
133 | } |
---|
134 | |
---|
135 | return object; |
---|
136 | } |
---|
137 | } catch(e) { |
---|
138 | // Fall through if the regexp test fails. |
---|
139 | } |
---|
140 | return null; |
---|
141 | }, |
---|
142 | |
---|
143 | /** |
---|
144 | * APIMethod: write |
---|
145 | * Serialize an object into a JSON string. |
---|
146 | * |
---|
147 | * Parameters: |
---|
148 | * value - {String} The object, array, string, number, boolean or date |
---|
149 | * to be serialized. |
---|
150 | * pretty - {Boolean} Structure the output with newlines and indentation. |
---|
151 | * Default is false. |
---|
152 | * |
---|
153 | * Returns: |
---|
154 | * {String} The JSON string representation of the input value. |
---|
155 | */ |
---|
156 | write: function(value, pretty) { |
---|
157 | this.pretty = !!pretty; |
---|
158 | var json = null; |
---|
159 | var type = typeof value; |
---|
160 | if(this.serialize[type]) { |
---|
161 | try { |
---|
162 | json = this.serialize[type].apply(this, [value]); |
---|
163 | } catch(err) { |
---|
164 | OpenLayers.Console.error("Trouble serializing: " + err); |
---|
165 | } |
---|
166 | } |
---|
167 | return json; |
---|
168 | }, |
---|
169 | |
---|
170 | /** |
---|
171 | * Method: writeIndent |
---|
172 | * Output an indentation string depending on the indentation level. |
---|
173 | * |
---|
174 | * Returns: |
---|
175 | * {String} An appropriate indentation string. |
---|
176 | */ |
---|
177 | writeIndent: function() { |
---|
178 | var pieces = []; |
---|
179 | if(this.pretty) { |
---|
180 | for(var i=0; i<this.level; ++i) { |
---|
181 | pieces.push(this.indent); |
---|
182 | } |
---|
183 | } |
---|
184 | return pieces.join(''); |
---|
185 | }, |
---|
186 | |
---|
187 | /** |
---|
188 | * Method: writeNewline |
---|
189 | * Output a string representing a newline if in pretty printing mode. |
---|
190 | * |
---|
191 | * Returns: |
---|
192 | * {String} A string representing a new line. |
---|
193 | */ |
---|
194 | writeNewline: function() { |
---|
195 | return (this.pretty) ? this.newline : ''; |
---|
196 | }, |
---|
197 | |
---|
198 | /** |
---|
199 | * Method: writeSpace |
---|
200 | * Output a string representing a space if in pretty printing mode. |
---|
201 | * |
---|
202 | * Returns: |
---|
203 | * {String} A space. |
---|
204 | */ |
---|
205 | writeSpace: function() { |
---|
206 | return (this.pretty) ? this.space : ''; |
---|
207 | }, |
---|
208 | |
---|
209 | /** |
---|
210 | * Property: serialize |
---|
211 | * Object with properties corresponding to the serializable data types. |
---|
212 | * Property values are functions that do the actual serializing. |
---|
213 | */ |
---|
214 | serialize: { |
---|
215 | /** |
---|
216 | * Method: serialize.object |
---|
217 | * Transform an object into a JSON string. |
---|
218 | * |
---|
219 | * Parameters: |
---|
220 | * object - {Object} The object to be serialized. |
---|
221 | * |
---|
222 | * Returns: |
---|
223 | * {String} A JSON string representing the object. |
---|
224 | */ |
---|
225 | 'object': function(object) { |
---|
226 | // three special objects that we want to treat differently |
---|
227 | if(object == null) { |
---|
228 | return "null"; |
---|
229 | } |
---|
230 | if(object.constructor == Date) { |
---|
231 | return this.serialize.date.apply(this, [object]); |
---|
232 | } |
---|
233 | if(object.constructor == Array) { |
---|
234 | return this.serialize.array.apply(this, [object]); |
---|
235 | } |
---|
236 | var pieces = ['{']; |
---|
237 | this.level += 1; |
---|
238 | var key, keyJSON, valueJSON; |
---|
239 | |
---|
240 | var addComma = false; |
---|
241 | for(key in object) { |
---|
242 | if(object.hasOwnProperty(key)) { |
---|
243 | // recursive calls need to allow for sub-classing |
---|
244 | keyJSON = OpenLayers.Format.JSON.prototype.write.apply(this, |
---|
245 | [key, this.pretty]); |
---|
246 | valueJSON = OpenLayers.Format.JSON.prototype.write.apply(this, |
---|
247 | [object[key], this.pretty]); |
---|
248 | if(keyJSON != null && valueJSON != null) { |
---|
249 | if(addComma) { |
---|
250 | pieces.push(','); |
---|
251 | } |
---|
252 | pieces.push(this.writeNewline(), this.writeIndent(), |
---|
253 | keyJSON, ':', this.writeSpace(), valueJSON); |
---|
254 | addComma = true; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | this.level -= 1; |
---|
260 | pieces.push(this.writeNewline(), this.writeIndent(), '}'); |
---|
261 | return pieces.join(''); |
---|
262 | }, |
---|
263 | |
---|
264 | /** |
---|
265 | * Method: serialize.array |
---|
266 | * Transform an array into a JSON string. |
---|
267 | * |
---|
268 | * Parameters: |
---|
269 | * array - {Array} The array to be serialized |
---|
270 | * |
---|
271 | * Returns: |
---|
272 | * {String} A JSON string representing the array. |
---|
273 | */ |
---|
274 | 'array': function(array) { |
---|
275 | var json; |
---|
276 | var pieces = ['[']; |
---|
277 | this.level += 1; |
---|
278 | |
---|
279 | for(var i=0, len=array.length; i<len; ++i) { |
---|
280 | // recursive calls need to allow for sub-classing |
---|
281 | json = OpenLayers.Format.JSON.prototype.write.apply(this, |
---|
282 | [array[i], this.pretty]); |
---|
283 | if(json != null) { |
---|
284 | if(i > 0) { |
---|
285 | pieces.push(','); |
---|
286 | } |
---|
287 | pieces.push(this.writeNewline(), this.writeIndent(), json); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | this.level -= 1; |
---|
292 | pieces.push(this.writeNewline(), this.writeIndent(), ']'); |
---|
293 | return pieces.join(''); |
---|
294 | }, |
---|
295 | |
---|
296 | /** |
---|
297 | * Method: serialize.string |
---|
298 | * Transform a string into a JSON string. |
---|
299 | * |
---|
300 | * Parameters: |
---|
301 | * string - {String} The string to be serialized |
---|
302 | * |
---|
303 | * Returns: |
---|
304 | * {String} A JSON string representing the string. |
---|
305 | */ |
---|
306 | 'string': function(string) { |
---|
307 | // If the string contains no control characters, no quote characters, and no |
---|
308 | // backslash characters, then we can simply slap some quotes around it. |
---|
309 | // Otherwise we must also replace the offending characters with safe |
---|
310 | // sequences. |
---|
311 | var m = { |
---|
312 | '\b': '\\b', |
---|
313 | '\t': '\\t', |
---|
314 | '\n': '\\n', |
---|
315 | '\f': '\\f', |
---|
316 | '\r': '\\r', |
---|
317 | '"' : '\\"', |
---|
318 | '\\': '\\\\' |
---|
319 | }; |
---|
320 | if(/["\\\x00-\x1f]/.test(string)) { |
---|
321 | return '"' + string.replace(/([\x00-\x1f\\"])/g, function(a, b) { |
---|
322 | var c = m[b]; |
---|
323 | if(c) { |
---|
324 | return c; |
---|
325 | } |
---|
326 | c = b.charCodeAt(); |
---|
327 | return '\\u00' + |
---|
328 | Math.floor(c / 16).toString(16) + |
---|
329 | (c % 16).toString(16); |
---|
330 | }) + '"'; |
---|
331 | } |
---|
332 | return '"' + string + '"'; |
---|
333 | }, |
---|
334 | |
---|
335 | /** |
---|
336 | * Method: serialize.number |
---|
337 | * Transform a number into a JSON string. |
---|
338 | * |
---|
339 | * Parameters: |
---|
340 | * number - {Number} The number to be serialized. |
---|
341 | * |
---|
342 | * Returns: |
---|
343 | * {String} A JSON string representing the number. |
---|
344 | */ |
---|
345 | 'number': function(number) { |
---|
346 | return isFinite(number) ? String(number) : "null"; |
---|
347 | }, |
---|
348 | |
---|
349 | /** |
---|
350 | * Method: serialize.boolean |
---|
351 | * Transform a boolean into a JSON string. |
---|
352 | * |
---|
353 | * Parameters: |
---|
354 | * bool - {Boolean} The boolean to be serialized. |
---|
355 | * |
---|
356 | * Returns: |
---|
357 | * {String} A JSON string representing the boolean. |
---|
358 | */ |
---|
359 | 'boolean': function(bool) { |
---|
360 | return String(bool); |
---|
361 | }, |
---|
362 | |
---|
363 | /** |
---|
364 | * Method: serialize.object |
---|
365 | * Transform a date into a JSON string. |
---|
366 | * |
---|
367 | * Parameters: |
---|
368 | * date - {Date} The date to be serialized. |
---|
369 | * |
---|
370 | * Returns: |
---|
371 | * {String} A JSON string representing the date. |
---|
372 | */ |
---|
373 | 'date': function(date) { |
---|
374 | function format(number) { |
---|
375 | // Format integers to have at least two digits. |
---|
376 | return (number < 10) ? '0' + number : number; |
---|
377 | } |
---|
378 | return '"' + date.getFullYear() + '-' + |
---|
379 | format(date.getMonth() + 1) + '-' + |
---|
380 | format(date.getDate()) + 'T' + |
---|
381 | format(date.getHours()) + ':' + |
---|
382 | format(date.getMinutes()) + ':' + |
---|
383 | format(date.getSeconds()) + '"'; |
---|
384 | } |
---|
385 | }, |
---|
386 | |
---|
387 | CLASS_NAME: "OpenLayers.Format.JSON" |
---|
388 | |
---|
389 | }); |
---|