4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 function serialize(o, quote) {
23 v = '\bb\tt\nn\ff\rr\""\'\'\\\\';
25 return quote + o.replace(/([\u0080-\uFFFF\x00-\x1f\"\'\\])/g, function(a, b) {
26 // Make sure single quotes never get encoded inside double quotes for JSON compatibility
27 if (quote === '"' && a === "'")
33 return '\\' + v.charAt(i + 1);
35 a = b.charCodeAt().toString(16);
37 return '\\u' + '0000'.substring(a.length) + a;
42 if (o.hasOwnProperty && o instanceof Array) {
43 for (i=0, v = '['; i<o.length; i++)
44 v += (i > 0 ? ',' : '') + serialize(o[i], quote);
52 v += typeof o[i] != 'function' ? (v.length > 1 ? ',' + quote : quote) + i + quote +':' + serialize(o[i], quote) : '';
61 * JSON parser and serializer class.
63 * @class tinymce.util.JSON
66 * // JSON parse a string into an object
67 * var obj = tinymce.util.JSON.parse(somestring);
69 * // JSON serialize a object into an string
70 * var str = tinymce.util.JSON.serialize(obj);
74 * Serializes the specified object as a JSON string.
77 * @param {Object} obj Object to serialize as a JSON string.
78 * @param {String} quote Optional quote string defaults to ".
79 * @return {string} JSON string serialized from input.
84 * Unserializes/parses the specified JSON string into a object.
87 * @param {string} s JSON String to parse into a JavaScript object.
88 * @return {Object} Object from input JSON string or undefined if it failed.
92 return eval('(' + s + ')');