[795] | 1 | |
---|
| 2 | var JSON = |
---|
| 3 | { |
---|
| 4 | // transform [object/number/string/boolean] into JSON-String |
---|
| 5 | stringify: function (arg) |
---|
| 6 | { |
---|
| 7 | var c, i, l, s = '', v; |
---|
| 8 | |
---|
| 9 | switch (typeof arg) |
---|
| 10 | { |
---|
| 11 | case 'object': if (arg) |
---|
| 12 | { |
---|
| 13 | if (arg.constructor == Array) |
---|
| 14 | { |
---|
| 15 | for (i = 0; i < arg.length; ++i) |
---|
| 16 | { |
---|
| 17 | v = this.stringify(arg[i]); |
---|
| 18 | |
---|
| 19 | if (s) |
---|
| 20 | { |
---|
| 21 | s += ','; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | s += v; |
---|
| 25 | } |
---|
| 26 | return '[' + s + ']'; |
---|
| 27 | } |
---|
| 28 | else if (typeof arg.toString != 'undefined') |
---|
| 29 | { |
---|
| 30 | for (i in arg) |
---|
| 31 | { |
---|
| 32 | v = arg[i]; |
---|
| 33 | |
---|
| 34 | if (typeof v != 'undefined' && typeof v != 'function') |
---|
| 35 | { |
---|
| 36 | v = this.stringify(v); |
---|
| 37 | |
---|
| 38 | if (s) |
---|
| 39 | { |
---|
| 40 | s += ','; |
---|
| 41 | } |
---|
| 42 | s += this.stringify(i) + ':' + v; |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | return '{' + s + '}'; |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | return 'null'; |
---|
| 49 | |
---|
| 50 | case 'number' : return isFinite(arg) ? String(arg) : 'null'; |
---|
| 51 | |
---|
| 52 | case 'string' : l = arg.length; |
---|
| 53 | s = '"'; |
---|
| 54 | |
---|
| 55 | for (i = 0; i < l; i += 1) |
---|
| 56 | { |
---|
| 57 | c = arg.charAt(i); |
---|
| 58 | if (c >= ' ') |
---|
| 59 | { |
---|
| 60 | if (c == '\\' || c == '"') { |
---|
| 61 | s += '\\'; |
---|
| 62 | } |
---|
| 63 | s += c; |
---|
| 64 | } |
---|
| 65 | else |
---|
| 66 | { |
---|
| 67 | switch (c) |
---|
| 68 | { |
---|
| 69 | case '\b': s += '\\b'; |
---|
| 70 | break; |
---|
| 71 | |
---|
| 72 | case '\f': s += '\\f'; |
---|
| 73 | break; |
---|
| 74 | |
---|
| 75 | case '\n': s += '\\n'; |
---|
| 76 | break; |
---|
| 77 | |
---|
| 78 | case '\r': s += '\\r'; |
---|
| 79 | break; |
---|
| 80 | |
---|
| 81 | case '\t': s += '\\t'; |
---|
| 82 | break; |
---|
| 83 | |
---|
| 84 | default: c = c.charCodeAt(); |
---|
| 85 | s += '\\u00' + Math.floor(c / 16).toString(16) + |
---|
| 86 | (c % 16).toString(16); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | return s + '"'; |
---|
| 91 | |
---|
| 92 | case 'boolean': return String(arg); |
---|
| 93 | |
---|
| 94 | default : return 'null'; |
---|
| 95 | } |
---|
| 96 | }, |
---|
| 97 | |
---|
| 98 | parse: function (text) |
---|
| 99 | { |
---|
| 100 | var at = 0; |
---|
| 101 | var ch = ' '; |
---|
| 102 | |
---|
| 103 | function error(m) |
---|
| 104 | { |
---|
| 105 | throw { name : 'JSONError', |
---|
| 106 | message : m, |
---|
| 107 | at : at - 1, |
---|
| 108 | text : text }; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | function next() |
---|
| 112 | { |
---|
| 113 | ch = text.charAt(at); |
---|
| 114 | at += 1; |
---|
| 115 | return ch; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | function white() |
---|
| 119 | { |
---|
| 120 | while (ch) |
---|
| 121 | { |
---|
| 122 | if (ch <= ' ') |
---|
| 123 | { |
---|
| 124 | next(); |
---|
| 125 | } |
---|
| 126 | else if (ch == '/') |
---|
| 127 | { |
---|
| 128 | switch (next()) |
---|
| 129 | { |
---|
| 130 | case '/': while (next() && ch != '\n' && ch != '\r') {} |
---|
| 131 | break; |
---|
| 132 | |
---|
| 133 | case '*': next(); |
---|
| 134 | for (;;) |
---|
| 135 | { |
---|
| 136 | if (ch) |
---|
| 137 | { |
---|
| 138 | if (ch == '*') |
---|
| 139 | { |
---|
| 140 | if (next() == '/') |
---|
| 141 | { |
---|
| 142 | next(); |
---|
| 143 | break; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | else |
---|
| 147 | { |
---|
| 148 | next(); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | error("Unterminated comment"); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | break; |
---|
| 157 | |
---|
| 158 | default: error("Syntax error"); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | else |
---|
| 162 | { |
---|
| 163 | break; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | function string() |
---|
| 169 | { |
---|
| 170 | var i, s = '', t, u; |
---|
| 171 | |
---|
| 172 | if (ch == '"') |
---|
| 173 | { |
---|
| 174 | outer: while (next()) |
---|
| 175 | { |
---|
| 176 | if (ch == '"') |
---|
| 177 | { |
---|
| 178 | next(); |
---|
| 179 | return s; |
---|
| 180 | } |
---|
| 181 | else if (ch == '\\') |
---|
| 182 | { |
---|
| 183 | switch (next()) |
---|
| 184 | { |
---|
| 185 | case 'b': s += '\b'; |
---|
| 186 | break; |
---|
| 187 | |
---|
| 188 | case 'f': s += '\f'; |
---|
| 189 | break; |
---|
| 190 | |
---|
| 191 | case 'n': s += '\n'; |
---|
| 192 | break; |
---|
| 193 | |
---|
| 194 | case 'r': s += '\r'; |
---|
| 195 | break; |
---|
| 196 | |
---|
| 197 | case 't': s += '\t'; |
---|
| 198 | break; |
---|
| 199 | |
---|
| 200 | case 'u': u = 0; |
---|
| 201 | for (i = 0; i < 4; i += 1) |
---|
| 202 | { |
---|
| 203 | t = parseInt(next(), 16); |
---|
| 204 | if (!isFinite(t)) |
---|
| 205 | { |
---|
| 206 | break outer; |
---|
| 207 | } |
---|
| 208 | u = u * 16 + t; |
---|
| 209 | } |
---|
| 210 | s += String.fromCharCode(u); |
---|
| 211 | break; |
---|
| 212 | |
---|
| 213 | default: s += ch; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | s += ch; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | error("Bad string"); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | function array() |
---|
| 226 | { |
---|
| 227 | var a = []; |
---|
| 228 | |
---|
| 229 | if (ch == '[') |
---|
| 230 | { |
---|
| 231 | next(); |
---|
| 232 | white(); |
---|
| 233 | |
---|
| 234 | if (ch == ']') |
---|
| 235 | { |
---|
| 236 | next(); |
---|
| 237 | return a; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | while (ch) |
---|
| 241 | { |
---|
| 242 | a.push(value()); |
---|
| 243 | white(); |
---|
| 244 | |
---|
| 245 | if (ch == ']') |
---|
| 246 | { |
---|
| 247 | next(); |
---|
| 248 | return a; |
---|
| 249 | } |
---|
| 250 | else if (ch != ',') |
---|
| 251 | { |
---|
| 252 | break; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | next(); |
---|
| 256 | white(); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | error("Bad array"); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | function object() |
---|
| 263 | { |
---|
| 264 | var k, o = {}; |
---|
| 265 | |
---|
| 266 | if (ch == '{') |
---|
| 267 | { |
---|
| 268 | next(); |
---|
| 269 | white(); |
---|
| 270 | |
---|
| 271 | if (ch == '}') |
---|
| 272 | { |
---|
| 273 | next(); |
---|
| 274 | return o; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | while (ch) |
---|
| 278 | { |
---|
| 279 | k = string(); |
---|
| 280 | white(); |
---|
| 281 | |
---|
| 282 | if (ch != ':') |
---|
| 283 | { |
---|
| 284 | break; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | next(); |
---|
| 288 | o[k] = value(); |
---|
| 289 | white(); |
---|
| 290 | |
---|
| 291 | if (ch == '}') |
---|
| 292 | { |
---|
| 293 | next(); |
---|
| 294 | return o; |
---|
| 295 | } |
---|
| 296 | else if (ch != ',') |
---|
| 297 | { |
---|
| 298 | break; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | next(); |
---|
| 302 | white(); |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | error("Bad object"); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | function number() |
---|
| 309 | { |
---|
| 310 | var n = '', v; |
---|
| 311 | |
---|
| 312 | if (ch == '-') |
---|
| 313 | { |
---|
| 314 | n = '-'; |
---|
| 315 | next(); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | while (ch >= '0' && ch <= '9') |
---|
| 319 | { |
---|
| 320 | n += ch; |
---|
| 321 | next(); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | if (ch == '.') |
---|
| 325 | { |
---|
| 326 | n += '.'; |
---|
| 327 | |
---|
| 328 | while (next() && ch >= '0' && ch <= '9') |
---|
| 329 | { |
---|
| 330 | n += ch; |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | if (ch == 'e' || ch == 'E') |
---|
| 335 | { |
---|
| 336 | n += 'e'; |
---|
| 337 | next(); |
---|
| 338 | |
---|
| 339 | if (ch == '-' || ch == '+') |
---|
| 340 | { |
---|
| 341 | n += ch; |
---|
| 342 | next(); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | while (ch >= '0' && ch <= '9') |
---|
| 346 | { |
---|
| 347 | n += ch; |
---|
| 348 | next(); |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | v = +n; |
---|
| 353 | |
---|
| 354 | if (!isFinite(v)) |
---|
| 355 | { |
---|
| 356 | ////error("Bad number"); |
---|
| 357 | } |
---|
| 358 | else |
---|
| 359 | { |
---|
| 360 | return v; |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | function word() |
---|
| 365 | { |
---|
| 366 | switch (ch) |
---|
| 367 | { |
---|
| 368 | case 't': if (next() == 'r' && next() == 'u' && next() == 'e') |
---|
| 369 | { |
---|
| 370 | next(); |
---|
| 371 | return true; |
---|
| 372 | } |
---|
| 373 | break; |
---|
| 374 | |
---|
| 375 | case 'f': if (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') |
---|
| 376 | { |
---|
| 377 | next(); |
---|
| 378 | return false; |
---|
| 379 | } |
---|
| 380 | break; |
---|
| 381 | |
---|
| 382 | case 'n': if (next() == 'u' && next() == 'l' && next() == 'l') |
---|
| 383 | { |
---|
| 384 | next(); |
---|
| 385 | return null; |
---|
| 386 | } |
---|
| 387 | break; |
---|
| 388 | } |
---|
| 389 | error("Syntax error"); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | function value() |
---|
| 393 | { |
---|
| 394 | white(); |
---|
| 395 | |
---|
| 396 | switch (ch) { |
---|
| 397 | case '{': return object(); |
---|
| 398 | |
---|
| 399 | case '[': return array(); |
---|
| 400 | |
---|
| 401 | case '"': return string(); |
---|
| 402 | |
---|
| 403 | case '-': return number(); |
---|
| 404 | |
---|
| 405 | default : return ch >= '0' && ch <= '9' ? number() : word(); |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | return value(); |
---|
| 410 | } |
---|
| 411 | }; |
---|