src/pyams_skin/resources/js/ext/tinymce/dev/classes/util/JSON.js
changeset 565 318533413200
parent 564 a1c75f3e0bc7
child 566 a1707c607eec
equal deleted inserted replaced
564:a1c75f3e0bc7 565:318533413200
     1 /**
       
     2  * JSON.js
       
     3  *
       
     4  * Copyright, Moxiecode Systems AB
       
     5  * Released under LGPL License.
       
     6  *
       
     7  * License: http://www.tinymce.com/license
       
     8  * Contributing: http://www.tinymce.com/contributing
       
     9  */
       
    10 
       
    11 /**
       
    12  * JSON parser and serializer class.
       
    13  *
       
    14  * @class tinymce.util.JSON
       
    15  * @static
       
    16  * @example
       
    17  * // JSON parse a string into an object
       
    18  * var obj = tinymce.util.JSON.parse(somestring);
       
    19  *
       
    20  * // JSON serialize a object into an string
       
    21  * var str = tinymce.util.JSON.serialize(obj);
       
    22  */
       
    23 define("tinymce/util/JSON", [], function() {
       
    24 	function serialize(o, quote) {
       
    25 		var i, v, t, name;
       
    26 
       
    27 		quote = quote || '"';
       
    28 
       
    29 		if (o === null) {
       
    30 			return 'null';
       
    31 		}
       
    32 
       
    33 		t = typeof o;
       
    34 
       
    35 		if (t == 'string') {
       
    36 			v = '\bb\tt\nn\ff\rr\""\'\'\\\\';
       
    37 
       
    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 === "'") {
       
    41 					return a;
       
    42 				}
       
    43 
       
    44 				i = v.indexOf(b);
       
    45 
       
    46 				if (i + 1) {
       
    47 					return '\\' + v.charAt(i + 1);
       
    48 				}
       
    49 
       
    50 				a = b.charCodeAt().toString(16);
       
    51 
       
    52 				return '\\u' + '0000'.substring(a.length) + a;
       
    53 			}) + quote;
       
    54 		}
       
    55 
       
    56 		if (t == 'object') {
       
    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);
       
    60 				}
       
    61 
       
    62 				return v + ']';
       
    63 			}
       
    64 
       
    65 			v = '{';
       
    66 
       
    67 			for (name in o) {
       
    68 				if (o.hasOwnProperty(name)) {
       
    69 					v += typeof o[name] != 'function' ? (v.length > 1 ? ',' + quote : quote) + name +
       
    70 						quote + ':' + serialize(o[name], quote) : '';
       
    71 				}
       
    72 			}
       
    73 
       
    74 			return v + '}';
       
    75 		}
       
    76 
       
    77 		return '' + o;
       
    78 	}
       
    79 
       
    80 	return {
       
    81 		/**
       
    82 		 * Serializes the specified object as a JSON string.
       
    83 		 *
       
    84 		 * @method serialize
       
    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.
       
    88 		 */
       
    89 		serialize: serialize,
       
    90 
       
    91 		/**
       
    92 		 * Unserializes/parses the specified JSON string into a object.
       
    93 		 *
       
    94 		 * @method parse
       
    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.
       
    97 		 */
       
    98 		parse: function(text) {
       
    99 			try {
       
   100 				// Trick uglify JS
       
   101 				return window[String.fromCharCode(101) + 'val']('(' + text + ')');
       
   102 			} catch (ex) {
       
   103 				// Ignore
       
   104 			}
       
   105 		}
       
   106 
       
   107 		/**#@-*/
       
   108 	};
       
   109 });