[7219] | 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 | |
---|
| 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 | throw a 'JSONError' exception if there is an error. |
---|
| 30 | */ |
---|
| 31 | var JSON = { |
---|
| 32 | copyright: '(c)2005 JSON.org', |
---|
| 33 | license: 'http://www.crockford.com/JSON/license.html', |
---|
| 34 | /* |
---|
| 35 | Stringify a JavaScript value, producing a JSON text. |
---|
| 36 | */ |
---|
| 37 | stringify: function (v) { |
---|
| 38 | var a = []; |
---|
| 39 | |
---|
| 40 | /* |
---|
| 41 | Emit a string. |
---|
| 42 | */ |
---|
| 43 | function e(s) { |
---|
| 44 | a[a.length] = s; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /* |
---|
| 48 | Convert a value. |
---|
| 49 | */ |
---|
| 50 | function g(x) { |
---|
| 51 | var c, i, l, v; |
---|
| 52 | |
---|
| 53 | switch (typeof x) { |
---|
| 54 | case 'object': |
---|
| 55 | if (x) { |
---|
| 56 | if (x instanceof Array) { |
---|
| 57 | e('['); |
---|
| 58 | l = a.length; |
---|
| 59 | for (i = 0; i < x.length; i += 1) { |
---|
| 60 | v = x[i]; |
---|
| 61 | if (typeof v != 'undefined' && |
---|
| 62 | typeof v != 'function') { |
---|
| 63 | if (l < a.length) { |
---|
| 64 | e(','); |
---|
| 65 | } |
---|
| 66 | g(v); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | e(']'); |
---|
| 70 | return; |
---|
| 71 | } else if (typeof x.valueOf == 'function') { |
---|
| 72 | e('{'); |
---|
| 73 | l = a.length; |
---|
| 74 | for (i in x) { |
---|
| 75 | v = x[i]; |
---|
| 76 | if (typeof v != 'undefined' && |
---|
| 77 | typeof v != 'function' && |
---|
| 78 | (!v || typeof v != 'object' || |
---|
| 79 | typeof v.valueOf == 'function')) { |
---|
| 80 | if (l < a.length) { |
---|
| 81 | e(','); |
---|
| 82 | } |
---|
| 83 | g(i); |
---|
| 84 | e(':'); |
---|
| 85 | g(v); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | return e('}'); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | e('null'); |
---|
| 92 | return; |
---|
| 93 | case 'number': |
---|
| 94 | e(isFinite(x) ? +x : 'null'); |
---|
| 95 | return; |
---|
| 96 | case 'string': |
---|
| 97 | l = x.length; |
---|
| 98 | e('"'); |
---|
| 99 | for (i = 0; i < l; i += 1) { |
---|
| 100 | c = x.charAt(i); |
---|
| 101 | if (c >= ' ') { |
---|
| 102 | if (c == '\\' || c == '"') { |
---|
| 103 | e('\\'); |
---|
| 104 | } |
---|
| 105 | e(c); |
---|
| 106 | } else { |
---|
| 107 | switch (c) { |
---|
| 108 | case '\b': |
---|
| 109 | e('\\b'); |
---|
| 110 | break; |
---|
| 111 | case '\f': |
---|
| 112 | e('\\f'); |
---|
| 113 | break; |
---|
| 114 | case '\n': |
---|
| 115 | e('\\n'); |
---|
| 116 | break; |
---|
| 117 | case '\r': |
---|
| 118 | e('\\r'); |
---|
| 119 | break; |
---|
| 120 | case '\t': |
---|
| 121 | e('\\t'); |
---|
| 122 | break; |
---|
| 123 | default: |
---|
| 124 | c = c.charCodeAt(); |
---|
| 125 | e('\\u00' + Math.floor(c / 16).toString(16) + |
---|
| 126 | (c % 16).toString(16)); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | e('"'); |
---|
| 131 | return; |
---|
| 132 | case 'boolean': |
---|
| 133 | e(String(x)); |
---|
| 134 | return; |
---|
| 135 | default: |
---|
| 136 | e('null'); |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | g(v); |
---|
| 141 | return a.join(''); |
---|
| 142 | }, |
---|
| 143 | /* |
---|
| 144 | Parse a JSON text, producing a JavaScript value. |
---|
| 145 | */ |
---|
| 146 | parse: function (text) { |
---|
| 147 | return (/^(\s+|[,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text)) && |
---|
| 148 | eval('(' + text + ')'); |
---|
| 149 | } |
---|
| 150 | }; |
---|