JsonDecoderPartial.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.JsonDecoderPartial = exports.DecodeFinishError = void 0;
  4. const JsonDecoder_1 = require("./JsonDecoder");
  5. class DecodeFinishError extends Error {
  6. constructor(value) {
  7. super('DECODE_FINISH');
  8. this.value = value;
  9. }
  10. }
  11. exports.DecodeFinishError = DecodeFinishError;
  12. class JsonDecoderPartial extends JsonDecoder_1.JsonDecoder {
  13. readAny() {
  14. try {
  15. return super.readAny();
  16. }
  17. catch (error) {
  18. if (error instanceof DecodeFinishError)
  19. return error.value;
  20. throw error;
  21. }
  22. }
  23. readArr() {
  24. const reader = this.reader;
  25. if (reader.u8() !== 0x5b)
  26. throw new Error('Invalid JSON');
  27. const arr = [];
  28. const uint8 = reader.uint8;
  29. let first = true;
  30. while (true) {
  31. this.skipWhitespace();
  32. const char = uint8[reader.x];
  33. if (char === 0x5d)
  34. return reader.x++, arr;
  35. if (char === 0x2c)
  36. reader.x++;
  37. else if (!first)
  38. return arr;
  39. this.skipWhitespace();
  40. try {
  41. arr.push(this.readAny());
  42. }
  43. catch (error) {
  44. if (error instanceof DecodeFinishError)
  45. return arr.push(error.value), arr;
  46. if (error instanceof Error && error.message === 'Invalid JSON')
  47. throw new DecodeFinishError(arr);
  48. throw error;
  49. }
  50. first = false;
  51. }
  52. }
  53. readObj() {
  54. const reader = this.reader;
  55. if (reader.u8() !== 0x7b)
  56. throw new Error('Invalid JSON');
  57. const obj = {};
  58. const uint8 = reader.uint8;
  59. while (true) {
  60. this.skipWhitespace();
  61. let char = uint8[reader.x];
  62. if (char === 0x7d)
  63. return reader.x++, obj;
  64. if (char === 0x2c) {
  65. reader.x++;
  66. continue;
  67. }
  68. try {
  69. char = uint8[reader.x++];
  70. if (char !== 0x22)
  71. throw new Error('Invalid JSON');
  72. const key = (0, JsonDecoder_1.readKey)(reader);
  73. if (key === '__proto__')
  74. throw new Error('Invalid JSON');
  75. this.skipWhitespace();
  76. if (reader.u8() !== 0x3a)
  77. throw new Error('Invalid JSON');
  78. this.skipWhitespace();
  79. try {
  80. obj[key] = this.readAny();
  81. }
  82. catch (error) {
  83. if (error instanceof DecodeFinishError) {
  84. obj[key] = error.value;
  85. return obj;
  86. }
  87. throw error;
  88. }
  89. }
  90. catch (error) {
  91. if (error instanceof DecodeFinishError)
  92. return obj;
  93. if (error instanceof Error && error.message === 'Invalid JSON')
  94. throw new DecodeFinishError(obj);
  95. throw error;
  96. }
  97. }
  98. }
  99. }
  100. exports.JsonDecoderPartial = JsonDecoderPartial;
  101. //# sourceMappingURL=JsonDecoderPartial.js.map