]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/JSON.js
Release 6.2.1
[Github/sugarcrm.git] / jssource / src_files / include / JSON.js
1 /*
2 Copyright (c) 2005 JSON.org
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The Software shall be used for Good, not Evil.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE.
20
21     json.js
22
23     The global object JSON contains two methods.
24
25     JSON.stringify(value) takes a JavaScript value and produces a JSON text.
26     The value must not be cyclical.
27
28     JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
29     return false if there is an error.
30
31 2008-10-10: New regular expressions copied in from the new json2.js file on http://json.org (released into the public domain), work better on Safari and IE for more complicated datasets
32 */
33 var JSON = function () {
34     var m = {
35             '\b': '\\b',
36             '\t': '\\t',
37             '\n': '\\n',
38             '\f': '\\f',
39             '\r': '\\r',
40             '"' : '\\"',
41             '\\': '\\\\'
42         },
43         s = {
44             array: function (x) {
45                 var a = ['['], b, f, i, l = x.length, v;
46                 for (i = 0; i < l; i += 1) {
47                     v = x[i];
48                     f = s[typeof v];
49                     if (f) {
50                         v = f(v);
51                         if (typeof v == 'string') {
52                             if (b) {
53                                 a[a.length] = ',';
54                             }
55                             a[a.length] = v;
56                             b = true;
57                         }
58                     }
59                 }
60                 a[a.length] = ']';
61                 return a.join('');
62             },
63             'boolean': function (x) {
64                 return String(x);
65             },
66             'null': function (x) {
67                 return "null";
68             },
69             number: function (x) {
70                 return isFinite(x) ? String(x) : 'null';
71             },
72             object: function (x) {
73                 if (x) {
74                     if (x instanceof Array) {
75                         return s.array(x);
76                     }
77                     var a = ['{'], b, f, i, v;
78                     for (i in x) {
79                         if (!x.hasOwnProperty || x.hasOwnProperty(i)) {
80                             v = x[i];
81                             f = s[typeof v];
82                             if (f) {
83                                 v = f(v);
84                                 if (typeof v == 'string') {
85                                     if (b) {
86                                         a[a.length] = ',';
87                                     }
88                                     a.push(s.string(i), ':', v);
89                                     b = true;
90                                 }
91                             }
92                         }
93                     }
94                     a[a.length] = '}';
95                     return a.join('');
96                 }
97                 return 'null';
98             },
99             string: function (x) {
100
101                                 var unicode = new String;
102                                 for(var i=0; i<x.length; i++) {
103                                         var temp = x.charCodeAt(i).toString(16) ;
104                                         while(temp.length < 4) {
105                                                 temp = "0" + temp;
106                                         }
107                                         unicode += '\\u' + temp;
108                                 }
109                                 return '"' + unicode + '"';
110             }
111         };
112     return {
113 /*
114     Stringify a JavaScript value, adding a security envelope, producing a JSON text.
115 */
116         stringify: function (v) {
117             var f = s[typeof v];
118             if (f) {
119                 v = f(v);
120                 if (typeof v === 'string') {
121                     // cn: bug 12274 - add a security envelope to protect against CSRF
122                                         var securityEnvelope = '{"asynchronous_key": "' + asynchronous_key +'", "jsonObject": '+ v +'}';
123                                         return securityEnvelope;
124                 }
125             }
126             return;
127         },
128
129         destringify: function(str) {
130
131         },
132 /*
133     Stringify a JavaScript value, NO security envelope, producing a JSON text.
134 */
135         stringifyNoSecurity : function (v) {
136             var f = s[typeof v];
137             if (f) {
138                 v = f(v);
139                 if (typeof v === 'string') {
140                                         return v;
141                 }
142             }
143             return;
144         },
145
146         destringify: function(str) {
147
148         },
149
150 /*
151     Parse a JSON text, producing a JavaScript value.
152     It returns false if there is a syntax error.
153 */
154         parse: function (text) {
155                 // mfh: bug 14599
156                 text = text.replace(/^\s*|\s*$/,'');
157                         // cn: bug 12274 - the below defend against CSRF (see desc for whitepaper)
158                         if(text.substr) {
159                         if(text.substr(0,11) == "while(1);/*") {
160                                 text = text.substr(11);
161                                 text = text.substr(0, (text.length - 2));
162                         }
163                         }
164
165             try {
166 // In the second stage, we run the text against regular expressions that look
167 // for non-JSON patterns. We are especially concerned with '()' and 'new'
168 // because they can cause invocation, and '=' because it can cause mutation.
169 // But just to be safe, we want to reject all unexpected forms.
170
171 // We split the second stage into 4 regexp operations in order to work around
172 // crippling inefficiencies in IE's and Safari's regexp engines. First we
173 // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
174 // replace all simple value tokens with ']' characters. Third, we delete all
175 // open brackets that follow a colon or comma or that begin the text. Finally,
176 // we look to see that the remaining characters are only whitespace or ']' or
177 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
178
179               if (/^[\],:{}\s]*$/.test(text
180                   .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
181                   .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
182                   .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
183
184 // In the third stage we use the eval function to compile the text into a
185 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
186 // in JavaScript: it can begin a block or an object literal. We wrap the text
187 // in parens to eliminate the ambiguity.
188                  return eval('(' + text + ')');
189               } else {
190                  return false;
191               }
192             } catch (e) {
193                 return false;
194             }
195         },
196
197                 /**
198                  * SUGAR: in response to CSRF, keep a standard copy of the parse function
199                  */
200         parseNoSecurity: function (text) {
201             try {
202               // See the comments in the above function for a description of what this mess is
203               if (/^[\],:{}\s]*$/.test(text
204                   .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
205                   .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
206                   .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
207
208                  return eval('(' + text + ')');
209               } else {
210                  return false;
211               }
212             } catch (e) {
213                 return false;
214             }
215         }
216     };
217 }();