]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/util/JSON.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / util / JSON.js
1 /**
2  * JSON.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 (function() {
12         function serialize(o, quote) {
13                 var i, v, t;
14
15                 quote = quote || '"';
16
17                 if (o == null)
18                         return 'null';
19
20                 t = typeof o;
21
22                 if (t == 'string') {
23                         v = '\bb\tt\nn\ff\rr\""\'\'\\\\';
24
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 === "'")
28                                         return a;
29
30                                 i = v.indexOf(b);
31
32                                 if (i + 1)
33                                         return '\\' + v.charAt(i + 1);
34
35                                 a = b.charCodeAt().toString(16);
36
37                                 return '\\u' + '0000'.substring(a.length) + a;
38                         }) + quote;
39                 }
40
41                 if (t == 'object') {
42                         if (o.hasOwnProperty && o instanceof Array) {
43                                         for (i=0, v = '['; i<o.length; i++)
44                                                 v += (i > 0 ? ',' : '') + serialize(o[i], quote);
45
46                                         return v + ']';
47                                 }
48
49                                 v = '{';
50
51                                 for (i in o)
52                                         v += typeof o[i] != 'function' ? (v.length > 1 ? ',' + quote : quote) + i + quote +':' + serialize(o[i], quote) : '';
53
54                                 return v + '}';
55                 }
56
57                 return '' + o;
58         };
59
60         /**
61          * JSON parser and serializer class.
62          *
63          * @class tinymce.util.JSON
64          * @static
65          * @example
66          * // JSON parse a string into an object
67          * var obj = tinymce.util.JSON.parse(somestring);
68          * 
69          * // JSON serialize a object into an string
70          * var str = tinymce.util.JSON.serialize(obj);
71          */
72         tinymce.util.JSON = {
73                 /**
74                  * Serializes the specified object as a JSON string.
75                  *
76                  * @method serialize
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.
80                  */
81                 serialize: serialize,
82
83                 /**
84                  * Unserializes/parses the specified JSON string into a object.
85                  *
86                  * @method parse
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.
89                  */
90                 parse: function(s) {
91                         try {
92                                 return eval('(' + s + ')');
93                         } catch (ex) {
94                                 // Ignore
95                         }
96                 }
97
98                 /**#@-*/
99         };
100 })();