All files / json-pack/src/json JsonDecoder.ts

82.8% Statements 342/413
81.17% Branches 332/409
100% Functions 19/19
90.41% Lines 330/365

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 68512x 12x 12x 12x     12x 12x 2279x           83x       1x   1111x       1084x           12x 285x                                                                             12x     225x                                                                   12x   12x 42797x 42797x 42797x 42797x 42797x 42797x 430062x 430062x 428236x 5051x               191x 191x               2357x 2357x         2503x 2503x       5051x   423185x 380391x 380391x     1826x 1826x 1351x   475x 475x 471x   4x 4x 4x 4x 4x 4x 4x 4x 4x               382217x   42797x 42797x     12x 13x     2x 2x       667x 667x       48767x 48767x 48767x 48767x 48767x 48767x   14829x 283x 283x 225x 2x 2x     14769x     6266x   1364x   1536x   1998x   9044x   13730x 6x         277250x 277250x 277250x 277250x 277250x 277630x 277630x         380x 380x   277250x 277250x           1848x 1848x       2431x 2431x       1690x 1690x 1690x       2x 2x   1x   1x             16475x 16475x 16475x 16475x 16475x 16475x 16475x 393x 393x 393x 393x   16082x 16082x 16082x 243x 243x 243x 243x   15839x 15839x 15839x 466x 466x 466x 466x   15373x 15373x 15373x 472x 472x 472x 472x   14901x 14901x 14901x 299x 299x 299x 299x   14602x 14602x 14602x 251x 251x 251x 251x   14351x 14351x 14351x 43x 43x 43x 43x   14308x 14308x 14308x 14x 14x 14x 14x   14294x 14294x 14294x           14294x 14294x 14294x 34x 34x 34x 34x   14260x 14260x 14260x 10x 10x 10x 10x   14250x 14250x 14250x 7x 7x 7x 7x   14243x 14243x 14243x 9x 9x 9x 9x   14234x 14234x 14234x 19x 19x 19x 19x   14215x 14215x 14215x 214x 214x 214x 214x   14001x 14001x 14001x 1906x 1906x 1906x 1906x   12095x 12095x 12095x 8986x 8986x 8986x 8986x   3109x 3109x 3109x 3102x 3102x 3102x 3102x   7x 7x 7x           7x 7x 7x                                                     7x 7x 7x                                                       7x 7x 7x 1x 1x                                               1x 1x   6x 6x 6x 5x 5x                                                 5x 5x   1x 1x 1x 1x 1x                                                   1x 1x           18063x 18063x 18063x 18063x 18063x 18063x 18062x   18062x 18062x 18062x       283x 283x 283x 283x 283x 283x 58x 58x 58x 58x 58x 58x 58x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       4409x 4409x 4409x 4409x 4409x 4409x 15234x 15234x 15234x 10826x 3255x 10825x 10825x 10825x         6298x 6298x 6298x 6298x 6298x 6298x 35172x 35172x 35172x 28875x 5695x 28874x 28874x 28874x 28874x 28874x 28874x 28874x 28874x 28874x 28874x        
import {decodeUtf8} from '@jsonjoy.com/buffers/lib/utf8/decodeUtf8';
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {fromBase64Bin} from '@jsonjoy.com/base64/lib/fromBase64Bin';
import {findEndingQuote} from './util';
import type {BinaryJsonDecoder, PackValue} from '../types';
 
const REGEX_REPLACE_ESCAPED_CHARS = /\\(b|f|n|r|t|"|\/|\\)/g;
const escapedCharReplacer = (char: string) => {
  switch (char) {
    case '\\b':
      return '\b';
    case '\\f':
      return '\f';
    case '\\n':
      return '\n';
    case '\\r':
      return '\r';
    case '\\t':
      return '\t';
    case '\\"':
      return '"';
    case '\\/':
      return '/';
    case '\\\\':
      return '\\';
  }
  return char;
};
 
// Starts with data:application/octet-stream;base64, - 64 61 74 61 3a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73 74 72 65 61 6d 3b 62 61 73 65 36 34 2c
const hasBinaryPrefix = (u8: Uint8Array, x: number) =>
  u8[x] === 0x64 &&
  u8[x + 1] === 0x61 &&
  u8[x + 2] === 0x74 &&
  u8[x + 3] === 0x61 &&
  u8[x + 4] === 0x3a &&
  u8[x + 5] === 0x61 &&
  u8[x + 6] === 0x70 &&
  u8[x + 7] === 0x70 &&
  u8[x + 8] === 0x6c &&
  u8[x + 9] === 0x69 &&
  u8[x + 10] === 0x63 &&
  u8[x + 11] === 0x61 &&
  u8[x + 12] === 0x74 &&
  u8[x + 13] === 0x69 &&
  u8[x + 14] === 0x6f &&
  u8[x + 15] === 0x6e &&
  u8[x + 16] === 0x2f &&
  u8[x + 17] === 0x6f &&
  u8[x + 18] === 0x63 &&
  u8[x + 19] === 0x74 &&
  u8[x + 20] === 0x65 &&
  u8[x + 21] === 0x74 &&
  u8[x + 22] === 0x2d &&
  u8[x + 23] === 0x73 &&
  u8[x + 24] === 0x74 &&
  u8[x + 25] === 0x72 &&
  u8[x + 26] === 0x65 &&
  u8[x + 27] === 0x61 &&
  u8[x + 28] === 0x6d &&
  u8[x + 29] === 0x3b &&
  u8[x + 30] === 0x62 &&
  u8[x + 31] === 0x61 &&
  u8[x + 32] === 0x73 &&
  u8[x + 33] === 0x65 &&
  u8[x + 34] === 0x36 &&
  u8[x + 35] === 0x34 &&
  u8[x + 36] === 0x2c;
 
// Matches "data:application/cbor,base64;9w=="
const isUndefined = (u8: Uint8Array, x: number) =>
  // u8[x++] === 0x22 &&  // "
  // u8[x++] === 0x64 &&  // d
  u8[x++] === 0x61 && // a
  u8[x++] === 0x74 && // t
  u8[x++] === 0x61 && // a
  u8[x++] === 0x3a && // :
  u8[x++] === 0x61 && // a
  u8[x++] === 0x70 && // p
  u8[x++] === 0x70 && // p
  u8[x++] === 0x6c && // l
  u8[x++] === 0x69 && // i
  u8[x++] === 0x63 && // c
  u8[x++] === 0x61 && // a
  u8[x++] === 0x74 && // t
  u8[x++] === 0x69 && // i
  u8[x++] === 0x6f && // o
  u8[x++] === 0x6e && // n
  u8[x++] === 0x2f && // /
  u8[x++] === 0x63 && // c
  u8[x++] === 0x62 && // b
  u8[x++] === 0x6f && // o
  u8[x++] === 0x72 && // r
  u8[x++] === 0x2c && // ,
  u8[x++] === 0x62 && // b
  u8[x++] === 0x61 && // a
  u8[x++] === 0x73 && // s
  u8[x++] === 0x65 && // e
  u8[x++] === 0x36 && // 6
  u8[x++] === 0x34 && // 4
  u8[x++] === 0x3b && // ;
  u8[x++] === 0x39 && // 9
  u8[x++] === 0x77 && // w
  u8[x++] === 0x3d && // =
  u8[x++] === 0x3d && // =
  u8[x++] === 0x22; // "
 
const fromCharCode = String.fromCharCode;
 
export const readKey = (reader: Reader): string => {
  const buf = reader.uint8;
  const len = buf.length;
  const points: number[] = [];
  let x = reader.x;
  let prev = 0;
  while (x < len) {
    let code = buf[x++]!;
    if ((code & 0x80) === 0) {
      if (prev === 92) {
        switch (code) {
          case 98: // \b
            code = 8;
            break;
          case 102: // \f
            code = 12;
            break;
          case 110: // \n
            code = 10;
            break;
          case 114: // \r
            code = 13;
            break;
          case 116: // \t
            code = 9;
            break;
          case 34: // \"
            code = 34;
            break;
          case 47: // \/
            code = 47;
            break;
          case 92: // \\
            code = 92;
            break;
          default:
            throw new Error('Invalid JSON');
        }
        prev = 0;
      } else {
        if (code === 34) break;
        prev = code;
        if (prev === 92) continue;
      }
    } else {
      const octet2 = buf[x++]! & 0x3f;
      if ((code & 0xe0) === 0xc0) {
        code = ((code & 0x1f) << 6) | octet2;
      } else {
        const octet3 = buf[x++]! & 0x3f;
        if ((code & 0xf0) === 0xe0) {
          code = ((code & 0x1f) << 12) | (octet2 << 6) | octet3;
        } else {
          Eif ((code & 0xf8) === 0xf0) {
            const octet4 = buf[x++]! & 0x3f;
            let unit = ((code & 0x07) << 0x12) | (octet2 << 0x0c) | (octet3 << 0x06) | octet4;
            if (unit > 0xffff) {
              unit -= 0x10000;
              const unit0 = ((unit >>> 10) & 0x3ff) | 0xd800;
              unit = 0xdc00 | (unit & 0x3ff);
              points.push(unit0);
              code = unit;
            } else E{
              code = unit;
            }
          }
        }
      }
    }
    points.push(code);
  }
  reader.x = x;
  return fromCharCode.apply(String, points);
};
 
export class JsonDecoder implements BinaryJsonDecoder {
  public reader = new Reader();
 
  public read(uint8: Uint8Array): unknown {
    this.reader.reset(uint8);
    return this.readAny();
  }
 
  public decode(uint8: Uint8Array): unknown {
    this.reader.reset(uint8);
    return this.readAny();
  }
 
  public readAny(): unknown {
    this.skipWhitespace();
    const reader = this.reader;
    const x = reader.x;
    const uint8 = reader.uint8;
    const char = uint8[x];
    switch (char) {
      case 34 /* " */: {
        if (uint8[x + 1] === 0x64 /* d */) {
          const bin = this.tryReadBin();
          if (bin) return bin;
          if (isUndefined(uint8, x + 2)) {
            reader.x = x + 35;
            return undefined;
          }
        }
        return this.readStr();
      }
      case 91 /* [ */:
        return this.readArr();
      case 102 /* f */:
        return this.readFalse();
      case 110 /* n */:
        return this.readNull();
      case 116 /* t */:
        return this.readTrue();
      case 123 /* { */:
        return this.readObj();
      default:
        if ((char >= 48 /* 0 */ && char <= 57) /* 9 */ || char === 45 /* - */) return this.readNum();
        throw new Error('Invalid JSON');
    }
  }
 
  public skipWhitespace(): void {
    const reader = this.reader;
    const uint8 = reader.uint8;
    let x = reader.x;
    let char: number = 0;
    while (true) {
      char = uint8[x];
      switch (char) {
        case 32 /* <space> */:
        case 9 /* <tab> */:
        case 10 /* <line feed> */:
        case 13 /* <carriage return> */:
          x++;
          continue;
        default:
          reader.x = x;
          return;
      }
    }
  }
 
  public readNull(): null {
    Iif (this.reader.u32() !== 0x6e756c6c /* null */) throw new Error('Invalid JSON');
    return null;
  }
 
  public readTrue(): true {
    Iif (this.reader.u32() !== 0x74727565 /* true */) throw new Error('Invalid JSON');
    return true;
  }
 
  public readFalse(): false {
    const reader = this.reader;
    Iif (reader.u8() !== 0x66 /* f */ || reader.u32() !== 0x616c7365 /* alse */) throw new Error('Invalid JSON');
    return false;
  }
 
  public readBool(): unknown {
    const reader = this.reader;
    switch (reader.uint8[reader.x]) {
      case 102 /* f */:
        return this.readFalse();
      case 116 /* t */:
        return this.readTrue();
      default:
        throw new Error('Invalid JSON');
    }
  }
 
  public readNum(): number {
    const reader = this.reader;
    const uint8 = reader.uint8;
    let x = reader.x;
    let c = uint8[x++];
    const c1 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c2 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c3 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c4 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c5 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c6 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c7 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c8 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c9 = c;
    c = uint8[x++];
    Iif (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9);
      if (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c10 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c11 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c12 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c13 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c14 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c15 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c16 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c17 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c18 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18);
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c19 = c;
    c = uint8[x++];
    Iif (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19);
      if (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c20 = c;
    c = uint8[x++];
    Iif (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(
        c1,
        c2,
        c3,
        c4,
        c5,
        c6,
        c7,
        c8,
        c9,
        c10,
        c11,
        c12,
        c13,
        c14,
        c15,
        c16,
        c17,
        c18,
        c19,
        c20,
      );
      if (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c21 = c;
    c = uint8[x++];
    Iif (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(
        c1,
        c2,
        c3,
        c4,
        c5,
        c6,
        c7,
        c8,
        c9,
        c10,
        c11,
        c12,
        c13,
        c14,
        c15,
        c16,
        c17,
        c18,
        c19,
        c20,
        c21,
      );
      if (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c22 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(
        c1,
        c2,
        c3,
        c4,
        c5,
        c6,
        c7,
        c8,
        c9,
        c10,
        c11,
        c12,
        c13,
        c14,
        c15,
        c16,
        c17,
        c18,
        c19,
        c20,
        c21,
        c22,
      );
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c23 = c;
    c = uint8[x++];
    if (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(
        c1,
        c2,
        c3,
        c4,
        c5,
        c6,
        c7,
        c8,
        c9,
        c10,
        c11,
        c12,
        c13,
        c14,
        c15,
        c16,
        c17,
        c18,
        c19,
        c20,
        c21,
        c22,
        c23,
      );
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    const c24 = c;
    c = uint8[x++];
    Eif (!c || ((c < 45 || c > 57) && c !== 43 && c !== 69 && c !== 101)) {
      reader.x = x - 1;
      const num = +fromCharCode(
        c1,
        c2,
        c3,
        c4,
        c5,
        c6,
        c7,
        c8,
        c9,
        c10,
        c11,
        c12,
        c13,
        c14,
        c15,
        c16,
        c17,
        c18,
        c19,
        c20,
        c21,
        c22,
        c23,
        c24,
      );
      Iif (num !== num) throw new Error('Invalid JSON');
      return num;
    }
    throw new Error('Invalid JSON');
  }
 
  public readStr(): string {
    const reader = this.reader;
    const uint8 = reader.uint8;
    const char = uint8[reader.x++];
    Iif (char !== 0x22) throw new Error('Invalid JSON');
    const x0 = reader.x;
    const x1 = findEndingQuote(uint8, x0);
    let str = decodeUtf8(uint8, x0, x1 - x0);
    /** @todo perf: maybe faster is to first check if there are any escaped chars. */
    str = str.replace(REGEX_REPLACE_ESCAPED_CHARS, escapedCharReplacer);
    reader.x = x1 + 1;
    return str;
  }
 
  public tryReadBin(): Uint8Array | undefined {
    const reader = this.reader;
    const u8 = reader.uint8;
    let x = reader.x;
    Iif (u8[x++] !== 0x22) return undefined;
    const hasDataUrlPrefix = hasBinaryPrefix(u8, x);
    if (!hasDataUrlPrefix) return undefined;
    x += 37;
    const x0 = x;
    x = findEndingQuote(u8, x);
    reader.x = x0;
    const bin = fromBase64Bin(reader.view, x0, x - x0);
    reader.x = x + 1;
    return bin;
  }
 
  public readBin(): Uint8Array {
    const reader = this.reader;
    const u8 = reader.uint8;
    let x = reader.x;
    Iif (u8[x++] !== 0x22) throw new Error('Invalid JSON');
    const hasDataUrlPrefix = hasBinaryPrefix(u8, x);
    Iif (!hasDataUrlPrefix) throw new Error('Invalid JSON');
    x += 37;
    const x0 = x;
    x = findEndingQuote(u8, x);
    reader.x = x0;
    const bin = fromBase64Bin(reader.view, x0, x - x0);
    reader.x = x + 1;
    return bin;
  }
 
  public readArr(): unknown[] {
    const reader = this.reader;
    Iif (reader.u8() !== 0x5b /* [ */) throw new Error('Invalid JSON');
    const arr: unknown[] = [];
    const uint8 = reader.uint8;
    let first = true;
    while (true) {
      this.skipWhitespace();
      const char = uint8[reader.x];
      if (char === 0x5d /* ] */) return reader.x++, arr;
      if (char === 0x2c /* , */) reader.x++;
      else if (!first) throw new Error('Invalid JSON');
      this.skipWhitespace();
      arr.push(this.readAny());
      first = false;
    }
  }
 
  public readObj(): PackValue | Record<string, unknown> | unknown {
    const reader = this.reader;
    Iif (reader.u8() !== 0x7b /* { */) throw new Error('Invalid JSON');
    const obj: Record<string, unknown> = {};
    const uint8 = reader.uint8;
    let first = true;
    while (true) {
      this.skipWhitespace();
      let char = uint8[reader.x];
      if (char === 0x7d /* } */) return reader.x++, obj;
      if (char === 0x2c /* , */) reader.x++;
      else if (!first) throw new Error('Invalid JSON');
      this.skipWhitespace();
      char = uint8[reader.x++];
      Iif (char !== 0x22 /* " */) throw new Error('Invalid JSON');
      const key = readKey(reader);
      Iif (key === '__proto__') throw new Error('Invalid JSON');
      this.skipWhitespace();
      Iif (reader.u8() !== 0x3a /* : */) throw new Error('Invalid JSON');
      this.skipWhitespace();
      obj[key] = this.readAny();
      first = false;
    }
  }
}