Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: af20e7e616
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 705 lines (699 sloc) 24.21 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
package skyboy.serialization {
import flash.utils.ByteArray;
import flash.utils.Endian;
import flash.utils.getQualifiedClassName;
import flash.utils.Dictionary;
/**
* JSON by skyboy. June 28th 2010.
* Visit http://github.com/skyboy for documentation, updates
* and more free code.
*
*
* Copyright (c) 2010, skyboy
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software with
* restriction, with limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions and limitations:
*
* ^ Attribution will be given to:
* skyboy, http://www.kongregate.com/accounts/skyboy;
* http://github.com/skyboy; http://skybov.deviantart.com
*
* ^ Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer in all copies or
* substantial portions of the Software.
*
* ^ Redistributions of modified source code must be marked as such, with
* the modifications marked and ducumented and the modifer's name clearly
* listed as having modified the source code.
*
* ^ Redistributions of source code may not add to, subtract from, or in
* any other way modify the above copyright notice, this list of conditions,
* or the following disclaimer for any reason.
*
* ^ Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* OR OTHER LIABILITY,(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER AN ACTION OF IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING FROM, OUT OF, IN CONNECTION OR
* IN ANY OTHER WAY OUT OF THE USE OF OR OTHER DEALINGS WITH THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
final public class JSON {
private static const instance:JSON = new JSON();
public function JSON() {
if (instance) throw new Error("This class has no instance methods.")
strArr.length = 0xFFFF; strArr.endian = Endian.LITTLE_ENDIAN;
strArrE.length = 0xFFFF; strArrE.endian = Endian.BIG_ENDIAN;
var i:int = 0x10000;
while (i--) {
encRL[i] = (0x30303030 | ((i & 0xF000) << 12) | ((i & 0xF00) << 8) | ((i & 0xF0) << 4) | (i & 0xF)) +
(((int((i & 0xF000) > 0x9000) * 0x7000) << 12) |
((int((i & 0xF00) > 0x900) * 0x700) << 8) |
((int((i & 0xF0) > 0x90) * 0x70) << 4) |
((int((i & 0xF) > 0x9) * 0x7)));
}
i = 0x100;
while (i--) {
encD[i] = i;
}
encD[0x62] = 8;
encD[0x66] = 12;
encD[0x6E] = 10;
encD[0x72] = 13;
encD[0x74] = 9;
}
private const preArrs:Vector.<Array> = new Vector.<Array>();
private const preObjs:Vector.<Object> = new Vector.<Object>();
private const strArr:ByteArray = new ByteArray();
private const strArrE:ByteArray = new ByteArray();
private const encRL:Vector.<int> = new Vector.<int>(0x10000, true);
private const encD:Vector.<int> = new Vector.<int>(0x100, true);
private var i:int;

public static const errorID:int = 0x4A534F4E;

private function decode(data:String):* {
if (!data) return null;
data = data.valueOf();
var e:int = data.length, temp:int;
var preObjs:Vector.<Object> = preObjs, preArrs:Vector.<Array> = preArrs;
var objs:int = -preObjs.length, o2:int = -objs;
while (~(temp = data.indexOf("}", temp + 1))) ++objs;
if (objs > 0) {
objs += o2;
preObjs.length = objs;
while (o2 < objs) preObjs[o2++] = new Object;
}
o2 = preArrs.length
objs = -o2;
temp = 0;
while (~(temp = data.indexOf("]", temp + 1))) ++objs;
if (objs > 0) {
objs += o2;
preArrs.length = objs;
temp = e / objs;
while (o2 < objs) preArrs[o2++] = new Array(temp);
}
strArr.length = e;
var c:int = data.charCodeAt(i = 0);
while (isSpace(c)) {
c = data.charCodeAt(++i);
}
var rtn:*;
if (isObject(c)) {
rtn = handleObject(data, e);
} else if (isArray(c)) {
rtn = handleArray(data, e);
} else if (isString(c)) {
rtn = handleString(data, e);
} else if (isNumber(c)) {
rtn = handleNumber2(data, e);
} else if (isLit(c)) {
rtn = handleLit(data, e);
} else error(data, i);
return rtn;
}
public static function decode(data:String):* {
return instance.decode(data);
}
public static function parse(data:String):* {
return instance.decode(data);
}
public static function get index():int {
return instance.i;
}

private function tryToJSON(data:*):String {
try {
return data.toJSON() as String;
} catch (e:ArgumentError) {
if (e.errorID != 1063) throw e;
}
return null;
}
private function encode2(data:*):void {
var ret:ByteArray = strArrE, c:String;
if (data == null) {
ret.writeUTFBytes("null");
return;
}
if ("toJSON" in data) if (data.toJSON is Function) {
c = tryToJSON(data);
if (c != null) {
handleStringE2(c, ret);
return;
}
}
if (data is Function) ret.writeUTFBytes("null");
else if (data is String) {
handleStringE2(data, ret, false);
} else if (data is Number) {
if ((data * 0) != 0) data = 0;
ret.writeUTFBytes(String(data));
} else if (data is Boolean) {
ret.writeUTFBytes(String(data));
} else if (data is Date) {
ret.writeUTFBytes(String(data.getTime()));
} else if (data is Array || getQualifiedClassName(data).indexOf("__AS3__.vec::Vector.<") == 0) {
var i:int, e:int = data.length - 1;
ret.writeByte(0x5B); // [
if (e > 0) {
if (e & 1) encode2(data[i++]), ret.writeByte(0x2C); // ,
e >>>= 1;
while (e--) {
encode2(data[i++]);
ret.writeByte(0x2C); // ,
encode2(data[i++]);
ret.writeByte(0x2C); // ,
}
encode2(data[i]);
} else if (!e) {
encode2(data[i]);
}
ret.writeByte(0x5D); // ]
} else if (data is Dictionary) {
ret.writeByte(0x7B); // {
for (var b:* in data) {
if (b is String) handleStringE2(b, ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is Number) handleStringE2(String(b), ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is Date) handleStringE2(String(b.getTime()), ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is XML) handleStringE2(b.toXMLString(), ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is Boolean) handleStringE2(String(b), ret), encode2(data[b]), ret.writeByte(0x2C);
}
if (b !== undefined) ret.position--;
ret.writeByte(0x7D); // }
} else if (data is XML) {
handleStringE2(data.toXMLString(), ret, false);
} else if (data is Object) {
ret.writeByte(0x7B); // {
for (c in data) {
handleStringE2(c, ret), encode2(data[c]), ret.writeByte(0x2C);
}
if (c != null) ret.position--;
ret.writeByte(0x7D); // }
} else ret.writeUTFBytes("null");
}
private function encode(data:*):String {
if (data == null) return "null";
var ret:ByteArray = strArrE, c:String;
ret.position = 0;
if ("toJSON" in data) if (data.toJSON is Function) {
c = tryToJSON(data);
if (c != null) return handleStringE(c, false);
}
if (data is Function) return "null";
if (data is String) {
handleStringE2(data, ret, false);
} else if (data is Number) {
if ((data * 0) != 0) data = 0;
ret.writeUTFBytes(String(data));
} else if (data is Boolean) {
ret.writeUTFBytes(String(data));
} else if (data is Date) {
ret.writeUTFBytes(String(data.getTime()));
} else if (data is Array || getQualifiedClassName(data).indexOf("__AS3__.vec::Vector.<") == 0) {
var i:int, e:int = data.length - 1;
ret.writeByte(0x5B); // [
if (e > 0) {
if (e & 1) encode2(data[i++]), ret.writeByte(0x2C); // ,
e >>>= 1;
while (e--) {
encode2(data[i++]);
ret.writeByte(0x2C); // ,
encode2(data[i++]);
ret.writeByte(0x2C); // ,
}
encode2(data[i]);
} else if (!e) {
encode2(data[i]);
}
ret.writeByte(0x5D); // ]
} else if (data is Dictionary) {
ret.writeByte(0x7B); // {
for (var b:* in data) {
if (b is String) handleStringE2(b, ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is Number) handleStringE2(String(b), ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is Date) handleStringE2(String(b.getTime()), ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is XML) handleStringE2(b.toXMLString(), ret), encode2(data[b]), ret.writeByte(0x2C);
else if (b is Boolean) handleStringE2(String(b), ret), encode2(data[b]), ret.writeByte(0x2C);
}
if (b !== undefined) ret.position--;
ret.writeByte(0x7D); // }
} else if (data is XML) {
handleStringE2(data.toXMLString(), ret, false);
} else if (data is Object) {
ret.writeByte(0x7B); // {
for (c in data) {
handleStringE2(c, ret), encode2(data[c]), ret.writeByte(0x2C);
}
if (c != null) ret.position--;
ret.writeByte(0x7D); // }
} else return "null";
i = ret.position;
ret.position = 0;
c = ret.readUTFBytes(i);
ret.length = 0;
return c;
}
public static function encode(data:*):String {
return instance.encode(data);
}
public static function stringify(data:*):String {
return instance.encode(data);
}
public static function toJSON(data:* = null):String {
return instance.encode(data);
}

private function isSpace(i:int):Boolean {
return Boolean(int(i == 0x20) | int(i == 0x09));
}
private function isString(i:int):Boolean {
return Boolean(int(i == 0x22) | int(i == 0x27));
}
private function isObject(i:int):Boolean {
return i == 0x7B;
}
private function isArray(i:int):Boolean {
return i == 0x5B;
}
private function isNumber(i:int):Boolean {
return Boolean(int(i == 0x2D) | int(i == 0x2E) | (int(i > 0x2F) & int(i < 0x3A)) | int(i == 0x2B));
}
private function isNumeric(i:int):Boolean {
return Boolean(int(i > 0x2F) & int(i < 0x3A));
}
private function isLit(i:int):Boolean {
i |= 0x20;
return Boolean(int(i == 0x74) | int(i == 0x66) | int(i == 0x6E));
}

private function min(a:Number, b:Number):Number {
var c:int = int(a < b);
return (c * a) + ((1 - c) * b); // fast a < b ? a : b;
}
private function handleStringE(data:String, colon:Boolean = true):String {
var rtn:ByteArray = strArrE, c:int, i:int;
var e:int = data.length, t:int;
var enc:Vector.<int> = encRL;
rtn.writeByte(0x22);
while (i < e) {
c = data.charCodeAt(i++);
if (int(c < 32) | int(c > 126)) {
t = int(c > 0xFFFF)
rtn.writeShort(0x5C75);
c = (t * 0xFFFF) | ((1 - t) * c);
rtn.writeInt(enc[c]);
/*
t = ((c & 0xF000) >> 12) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;
t = ((c & 0xF00) >> 8) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;
t = ((c & 0xF0) >> 4) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;
t = (c & 15) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;//*/
continue;
} else if (int(c == 0x22) | int(c == 0x5C)) {
rtn.writeByte(0x5C);
}
rtn.writeByte(c);
}
rtn.writeByte(0x22);
if (colon) rtn.writeByte(0x3A);
i = rtn.position;
rtn.position = 0;
data = rtn.readUTFBytes(i);
rtn.length = 0;
return data;
}
private function handleStringE2(data:String, rtn:ByteArray, colon:Boolean = true):void {
var c:int, i:int;
var e:int = data.length, t:int;
var enc:Vector.<int> = encRL;
rtn.writeByte(0x22);
while (i < e) {
c = data.charCodeAt(i++);
if (int(c < 32) | int(c > 126)) {
t = int(c > 0xFFFF)
rtn.writeShort(0x5C75);
c = (t * 0xFFFF) | ((1 - t) * c);
rtn.writeInt(enc[c]);
/*
t = ((c & 0xF000) >> 12) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;
t = ((c & 0xF00) >> 8) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;
t = ((c & 0xF0) >> 4) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;
t = (c & 15) + 0x30;
t += 7 * int(t > 0x39);
rtn[inx++] = t;//*/
continue;
} else if (int(c == 0x22) | int(c == 0x5C)) {
rtn.writeByte(0x5C);
}
rtn.writeByte(c);
}
rtn.writeByte(0x22);
if (colon) rtn.writeByte(0x3A);
}
private function handleString(data:String, e:int):String {
var c:int, rtn:ByteArray = strArr, inx:int, a:int = i, end:int = data.charCodeAt(i);
var t:int, p:int, p1:int, p2:int;
var enc:Vector.<int> = encD;
rtn.position = 0;
const low:int = 0x7F;
while (a < e) {
c = data.charCodeAt(++a);
if (c == 0x5C) {
c = enc[data.charCodeAt(++a)]; // multi-byte characters will throw an error.
if (c == 0x75) {
t = data.charCodeAt(++a) - 0x30;
t -= (int(t > 9) * 7) | (int(t > 22) * 0x20);
p1 = data.charCodeAt(++a) - 0x30;
p1 -= (int(p1 > 9) * 7) | (int(p1 > 22) * 0x20);
p2 = data.charCodeAt(++a) - 0x30;
p2 -= (int(p2 > 9) * 7) | (int(p2 > 22) * 0x20);
p = data.charCodeAt(++a) - 0x30;
p -= (int(p > 9) * 7) | (int(p > 22) * 0x20);
if (uint(t | p1 | p2 | p) > uint(15)) { // comparing with uint instead of int means the <0 check is combined
error(data, a - 6, "Expected 0-F after \\u", 6);
}
rtn.position = inx;
// 0xE00000 | ((i & 0xF000) << 4) | 0x8000 | ((i & 0xFC0) << 2) | 0x80 | (i & 0x3F)
p1 = (p1 << 2) | (p2 >> 2);
p = ((p2 << 2) | p) & 0x3F;
rtn.writeInt((0xE0 | t) | ((0x80 | p1) << 8) | ((0x80 | p) << 16));
++inx;
++inx;
++inx;
continue;
} else if (c == 0x78) {
t = data.charCodeAt(++a) - 0x30;
t -= (int(t > 9) * 7) | (int(t > 22) * 0x20);
p = data.charCodeAt(++a) - 0x30;
p -= (int(p > 9) * 7) | (int(p > 22) * 0x20);
if (uint(t | p) > uint(15)) { // comparing with uint instead of int means the <0 check is combined
error(data, a - 4, "Expected 0-F after \\x", 4);
}
c = (t << 4) | p;
rtn.position = inx;
rtn.writeShort((0xC0 | ((c >> 6) & 0x1F)) | ((0x80 | (c & 0x3F)) << 8));
++inx;
++inx;
}
} else if (c == end) {
i = a;
rtn.position = 0;
return rtn.readUTFBytes(inx);
} else if (c > low) {
return handleMBString(data, e, c, rtn, inx, a, end);
}
rtn[inx] = c;
++inx;
}
error(data, i, "Unterminated String.", 1);
return null; // not reached
}
private function handleMBString(data:String, e:int, c:int, rtn:ByteArray, inx:int, a:int, end:int):String {
var t:int, p:int, p1:int, p2:int;
var enc:Vector.<int> = encD;
rtn.position = inx;
while (a < e) {
c = data.charCodeAt(++a);
if (c == 0x5C) {
c = enc[data.charCodeAt(++a)]; // multi-byte characters will throw an error.
if (c == 0x75) {
t = data.charCodeAt(++a) - 0x30;
t -= (int(t > 9) * 7) | (int(t > 22) * 0x20);
p1 = data.charCodeAt(++a) - 0x30;
p1 -= (int(p1 > 9) * 7) | (int(p1 > 22) * 0x20);
p2 = data.charCodeAt(++a) - 0x30;
p2 -= (int(p2 > 9) * 7) | (int(p2 > 22) * 0x20);
p = data.charCodeAt(++a) - 0x30;
p -= (int(p > 9) * 7) | (int(p > 22) * 0x20);
if (uint(t | p1 | p2 | p) > uint(15)) { // comparing with uint instead of int means the <0 check is combined
error(data, a - 6, "Expected 0-F after \\u", 6);
}
c = ((((((t << 4) | p1) << 4) | p2) << 4) | p);
} else if (c == 0x78) {
t = data.charCodeAt(++a) - 0x30;
t -= (int(t > 9) * 7) | (int(t > 22) * 0x20);
p = data.charCodeAt(++a) - 0x30;
p -= (int(p > 9) * 7) | (int(p > 22) * 0x20);
if (uint(t | p) > uint(15)) { // comparing with uint instead of int means the <0 check is combined
error(data, a - 4, "Expected 0-F after \\x", 4);
}
c = (t << 4) | p;
}
} else if (c == end) {
i = a;
inx = rtn.position;
rtn.position = 0;
return rtn.readUTFBytes(inx);
}
c = (0xF0 | ((c & 0x1C0000) >> 18)) | (0x8000 | ((c & 0x3F000) >> 4)) | 0x800000 | ((c & 0xFC0) << 10) | ((0x80 | (c & 0x3F)) << 16);
rtn.writeInt(c);
}
error(data, i, "Unterminated String.", 1);
return null;
}
private function handleNumber2(data:String, e:int):Number {
var a:int = i, c:int = data.charCodeAt(a), r:Number = 0, t:int = 1;
var n:int, ex:int, exn:int, d:Number = 10;
if (c == 0x2D) {
c = data.charCodeAt(++a);
n = 2;
} else if (c == 0x2B) {
c = data.charCodeAt(++a);
}
if (isNumeric(c)) {
r = c - 0x30;
while (int(a < e) & (int(int(c = int(data.charCodeAt(++a))) > 0x2F) & int(c < 0x3A))) {
r = (r * 10) + (c - 0x30);
}
}
if (c == 0x2E) {
while (int(a < e) & (int(int(c = int(data.charCodeAt(++a))) > 0x2F) & int(c < 0x3A))) {
r += (c - 0x30) / (t *= 10);
}
}
if ((c | 0x20) == 0x65) {
c = data.charCodeAt(++a);
if (c == 0x2D) {
exn = 1;
c = data.charCodeAt(++a);
} else if (c == 0x2B) c = data.charCodeAt(++a);
t = 3;
while (int(c > 0x2F) & int(c < 0x3A) & int(Boolean(t--))) {
ex = (ex * 10) + (c - 0x30);
c = data.charCodeAt(++a);
}
while (int(c > 0x2F) & int(c < 0x3A)) c = data.charCodeAt(++a); // consume the remainder
t = 10;
if (exn) {
if (ex < 325) while (ex) {
r /= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
r /= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
} else r = 0; // >= 325 for negative exponents results in 0
} else {
if (ex < 309)while (ex) {
r *= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
r *= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
} else r = Infinity; // >= 309 for positive exponents results in Infinity
}
}
if (a < e) {
while (isSpace(c)) c = data.charCodeAt(++a);
if (a < e) {
error(data, a);
}
}
return (1 - n) * r;
}
private function handleNumber(data:String, e:int):Number {
var a:int = i, c:int = data.charCodeAt(a), r:Number = 0, t:int = 1;
var n:int, ex:int, exn:int, d:Number = 10;
if (c == 0x2D) {
c = data.charCodeAt(++a);
n = 2;
} else if (c == 0x2B) {
c = data.charCodeAt(++a);
}
if (isNumeric(c)) {
r = c - 0x30;
while (int((c = int(data.charCodeAt(++a))) > 0x2F) & int(c < 0x3A)) {
r = (r * 10) + (c - 0x30);
}
if (int(i == 0x20) | int(i == 0x09) | int(c == 0x2C) | int((c | 0x20) == 0x7D)) {
i = a - 1;
return (1 - n) * r;
}
}
if (c == 0x2E) {
while (int((c = int(data.charCodeAt(++a))) > 0x2F) & int(c < 0x3A)) {
r += (c - 0x30) / (t *= 10);
}
if (int(i == 0x20) | int(i == 0x09) | int(c == 0x2C) | int((c | 0x20) == 0x7D)) {
i = a - 1;
return (1 - n) * r;
}
}
if ((c | 0x20) == 0x65) {
c = data.charCodeAt(++a);
if (c == 0x2D) {
exn = 1;
c = data.charCodeAt(++a);
} else if (c == 0x2B) c = data.charCodeAt(++a);
t = 3;
while (int(c > 0x2F) & int(c < 0x3A) & int(Boolean(t--))) {
ex = (ex * 10) + (c - 0x30);
c = data.charCodeAt(++a);
}
while (int(c > 0x2F) & int(c < 0x3A)) c = data.charCodeAt(++a); // consume the remainder
t = 10;
if (exn) {
if (ex < 325) while (ex) {
r /= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
r /= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
} else r = 0; // >= 325 for negative exponents results in 0
} else {
if (ex < 309)while (ex) {
r *= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
r *= ((ex & 1) * t + (~ex & 1));
ex >>>= 1;
t *= t;
} else r = Infinity; // >= 309 for positive exponents results in Infinity
}
if (int(i == 0x20) | int(i == 0x09) | int(c == 0x2C) | int((c | 0x20) == 0x7D)) {
i = a - 1;
return (1 - n) * r;
}
}
if (a > e) {
i = e;
return (1 - n) * r;
}
error(data, a);
return NaN; // not reached
}
private function handleLit(data:String, e:int):* {
var a:int = data.charCodeAt(i++) | 0x20, b:int = data.charCodeAt(i++) | 0x20;
var c:int = data.charCodeAt(i++) | 0x20, d:int = data.charCodeAt(i) | 0x20;
if (a == 0x74) {
if (int(b == 0x72) | int(c == 0x75) | int(d == 0x65)) return true
error(data, i - 3, "Expected 'true'", 3);
} else if (a == 0x66) {
if (int(b == 0x61) | int(c == 0x6C) | int(d == 0x73) | int((data.charCodeAt(++i) | 0x20) == 0x65)) return false;
error(data, i - 4, "Expected 'false'", 4);
} else if (a == 0x6E) {
if (int(b == 0x75) | int(c == 0x6C) | int(d == 0x6C)) return null;
error(data, i - 3, "Expected 'null'", 3);
}
}
private function handleArray(data:String, e:int):Array {
var c:int, rtn:Array = preArrs.pop(), inx:int, p:Boolean = true;
while (i < e) {
do { c = data.charCodeAt(++i); } while (isSpace(c));
if (c == 0x5D) {
// throw error on ",]" ?
rtn.length = inx;
return rtn;
} else if (p) {
p = false;
if (isString(c)) {
rtn[inx] = handleString(data, e);++inx;
} else if (isNumber(c)) {
rtn[inx] = handleNumber(data, e);++inx;
} else if (isObject(c)) {
rtn[inx] = handleObject(data, e);++inx;
} else if (isArray(c)) {
rtn[inx] = handleArray(data, e);++inx;
} else if (isLit(c)) {
rtn[inx] = handleLit(data, e);++inx;
} else error(data, i);
} else if ((p = (c == 0x2C))) void;
else error(data, i, "Expected , or ]");
}
error(data, i, "Unterminated Array.", 1);
return null; // not reached
}
private function handleObject(data:String, e:int):Object {
var c:int, rtn:Object = preObjs.pop(), inx:String, p:Boolean = true;
while (i < e) {
do { c = data.charCodeAt(++i); } while (isSpace(c));
if (c == 0x7D) {
// error on ,} ?
return rtn;
} else if (p) {
p = false;
if (isString(c)) {
inx = handleString(data, e);
do { c = data.charCodeAt(++i); } while (isSpace(c));
if (c == 0x3A) {
do { c = data.charCodeAt(++i); } while (isSpace(c)); // wish i could omit these
if (isString(c)) {
rtn[inx] = handleString(data, e);
} else if (isNumber(c)) {
rtn[inx] = handleNumber(data, e);
} else if (isArray(c)) {
rtn[inx] = handleArray(data, e);
} else if (isObject(c)) {
rtn[inx] = handleObject(data, e);
} else if (isLit(c)) {
rtn[inx] = handleLit(data, e);
} else error(data, i, "Expected value.", 1); // values arranged in an attempt to get best performance
} else error(data, i, "Expected :");
} else error(data, i, "Expected \" or '"); // rearranging this saves one jump for every object property.
} else if ((p = (c == 0x2C))) void;
else error(data, i, "Expected , or }");
}
error(data, i, "Unterminated Object.", 1);
return null; // not reached
}
private function error(data:String, i:int, e:String = null, l:int = 0):void {
if (l) {
if (l > 1) {
throw new Error("Malformed JSON at: " + i + ", '" + data.substr(i, l) + (e ? "'. " + e : "'."), errorID);
} else {
throw new Error("Malformed JSON at: " + i + ", " + data.charAt(i) + (e ? ". " + e : '.'), errorID);
}
} else {
throw new Error("Malformed JSON at char: " + i + ", " + data.charAt(i) + (e ? ". " + e : '.'), errorID);
}
}
}
}
Something went wrong with that request. Please try again.