4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * JSON parser and serializer class.
14 * @class tinymce.util.JSON
17 * // JSON parse a string into an object
18 * var obj = tinymce.util.JSON.parse(somestring);
20 * // JSON serialize a object into an string
21 * var str = tinymce.util.JSON.serialize(obj);
23 define("tinymce/util/JSON", [], function() {
24 function serialize(o, quote) {
36 v = '\bb\tt\nn\ff\rr\""\'\'\\\\';
38 return quote + o.replace(/([\u0080-\uFFFF\x00-\x1f\"\'\\])/g, function(a, b) {
39 // Make sure single quotes never get encoded inside double quotes for JSON compatibility
40 if (quote === '"' && a === "'") {
47 return '\\' + v.charAt(i + 1);
50 a = b.charCodeAt().toString(16);
52 return '\\u' + '0000'.substring(a.length) + a;
57 if (o.hasOwnProperty && Object.prototype.toString.call(o) === '[object Array]') {
58 for (i=0, v = '['; i<o.length; i++) {
59 v += (i > 0 ? ',' : '') + serialize(o[i], quote);
68 if (o.hasOwnProperty(name)) {
69 v += typeof o[name] != 'function' ? (v.length > 1 ? ',' + quote : quote) + name +
70 quote +':' + serialize(o[name], quote) : '';
82 * Serializes the specified object as a JSON string.
85 * @param {Object} obj Object to serialize as a JSON string.
86 * @param {String} quote Optional quote string defaults to ".
87 * @return {string} JSON string serialized from input.
92 * Unserializes/parses the specified JSON string into a object.
95 * @param {string} s JSON String to parse into a JavaScript object.
96 * @return {Object} Object from input JSON string or undefined if it failed.
98 parse: function(text) {
101 return window[String.fromCharCode(101) + 'val']('(' + text + ')');