JsonEncoder.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.JsonEncoder = void 0;
  4. const toBase64Bin_1 = require("@jsonjoy.com/base64/lib/toBase64Bin");
  5. class JsonEncoder {
  6. constructor(writer) {
  7. this.writer = writer;
  8. }
  9. encode(value) {
  10. const writer = this.writer;
  11. writer.reset();
  12. this.writeAny(value);
  13. return writer.flush();
  14. }
  15. writeUnknown(value) {
  16. this.writeNull();
  17. }
  18. writeAny(value) {
  19. switch (typeof value) {
  20. case 'boolean':
  21. return this.writeBoolean(value);
  22. case 'number':
  23. return this.writeNumber(value);
  24. case 'string':
  25. return this.writeStr(value);
  26. case 'object': {
  27. if (value === null)
  28. return this.writeNull();
  29. const constructor = value.constructor;
  30. switch (constructor) {
  31. case Object:
  32. return this.writeObj(value);
  33. case Array:
  34. return this.writeArr(value);
  35. case Uint8Array:
  36. return this.writeBin(value);
  37. default:
  38. if (value instanceof Uint8Array)
  39. return this.writeBin(value);
  40. if (Array.isArray(value))
  41. return this.writeArr(value);
  42. return this.writeUnknown(value);
  43. }
  44. }
  45. case 'undefined': {
  46. return this.writeUndef();
  47. }
  48. default:
  49. return this.writeUnknown(value);
  50. }
  51. }
  52. writeNull() {
  53. this.writer.u32(0x6e756c6c);
  54. }
  55. writeUndef() {
  56. const writer = this.writer;
  57. const length = 35;
  58. writer.ensureCapacity(length);
  59. const view = writer.view;
  60. let x = writer.x;
  61. view.setUint32(x, 577003892);
  62. x += 4;
  63. view.setUint32(x, 1631215984);
  64. x += 4;
  65. view.setUint32(x, 1886153059);
  66. x += 4;
  67. view.setUint32(x, 1635019119);
  68. x += 4;
  69. view.setUint32(x, 1848599394);
  70. x += 4;
  71. view.setUint32(x, 1869753442);
  72. x += 4;
  73. view.setUint32(x, 1634952502);
  74. x += 4;
  75. view.setUint32(x, 876296567);
  76. x += 4;
  77. view.setUint16(x, 15677);
  78. x += 2;
  79. writer.uint8[x++] = 0x22;
  80. writer.x = x;
  81. }
  82. writeBoolean(bool) {
  83. if (bool)
  84. this.writer.u32(0x74727565);
  85. else
  86. this.writer.u8u32(0x66, 0x616c7365);
  87. }
  88. writeNumber(num) {
  89. const str = num.toString();
  90. this.writer.ascii(str);
  91. }
  92. writeInteger(int) {
  93. this.writeNumber(int >> 0 === int ? int : Math.trunc(int));
  94. }
  95. writeUInteger(uint) {
  96. this.writeInteger(uint < 0 ? -uint : uint);
  97. }
  98. writeFloat(float) {
  99. this.writeNumber(float);
  100. }
  101. writeBin(buf) {
  102. const writer = this.writer;
  103. const length = buf.length;
  104. writer.ensureCapacity(38 + 3 + (length << 1));
  105. const view = writer.view;
  106. let x = writer.x;
  107. view.setUint32(x, 577003892);
  108. x += 4;
  109. view.setUint32(x, 1631215984);
  110. x += 4;
  111. view.setUint32(x, 1886153059);
  112. x += 4;
  113. view.setUint32(x, 1635019119);
  114. x += 4;
  115. view.setUint32(x, 1848602467);
  116. x += 4;
  117. view.setUint32(x, 1952805933);
  118. x += 4;
  119. view.setUint32(x, 1937011301);
  120. x += 4;
  121. view.setUint32(x, 1634548578);
  122. x += 4;
  123. view.setUint32(x, 1634952502);
  124. x += 4;
  125. view.setUint16(x, 13356);
  126. x += 2;
  127. x = (0, toBase64Bin_1.toBase64Bin)(buf, 0, length, view, x);
  128. writer.uint8[x++] = 0x22;
  129. writer.x = x;
  130. }
  131. writeStr(str) {
  132. const writer = this.writer;
  133. const length = str.length;
  134. writer.ensureCapacity(length * 4 + 2);
  135. if (length < 256) {
  136. const startX = writer.x;
  137. let x = startX;
  138. const uint8 = writer.uint8;
  139. uint8[x++] = 0x22;
  140. for (let i = 0; i < length; i++) {
  141. const code = str.charCodeAt(i);
  142. switch (code) {
  143. case 34:
  144. case 92:
  145. uint8[x++] = 0x5c;
  146. break;
  147. }
  148. if (code < 32 || code > 126) {
  149. writer.x = startX;
  150. const jsonStr = JSON.stringify(str);
  151. writer.ensureCapacity(jsonStr.length * 4 + 4);
  152. writer.utf8(jsonStr);
  153. return;
  154. }
  155. else
  156. uint8[x++] = code;
  157. }
  158. uint8[x++] = 0x22;
  159. writer.x = x;
  160. return;
  161. }
  162. const jsonStr = JSON.stringify(str);
  163. writer.ensureCapacity(jsonStr.length * 4 + 4);
  164. writer.utf8(jsonStr);
  165. }
  166. writeAsciiStr(str) {
  167. const length = str.length;
  168. const writer = this.writer;
  169. writer.ensureCapacity(length * 2 + 2);
  170. const uint8 = writer.uint8;
  171. let x = writer.x;
  172. uint8[x++] = 0x22;
  173. for (let i = 0; i < length; i++) {
  174. const code = str.charCodeAt(i);
  175. switch (code) {
  176. case 34:
  177. case 92:
  178. uint8[x++] = 0x5c;
  179. break;
  180. }
  181. uint8[x++] = code;
  182. }
  183. uint8[x++] = 0x22;
  184. writer.x = x;
  185. }
  186. writeArr(arr) {
  187. const writer = this.writer;
  188. writer.u8(0x5b);
  189. const length = arr.length;
  190. const last = length - 1;
  191. for (let i = 0; i < last; i++) {
  192. this.writeAny(arr[i]);
  193. writer.u8(0x2c);
  194. }
  195. if (last >= 0)
  196. this.writeAny(arr[last]);
  197. writer.u8(0x5d);
  198. }
  199. writeArrSeparator() {
  200. this.writer.u8(0x2c);
  201. }
  202. writeObj(obj) {
  203. const writer = this.writer;
  204. const keys = Object.keys(obj);
  205. const length = keys.length;
  206. if (!length)
  207. return writer.u16(0x7b7d);
  208. writer.u8(0x7b);
  209. for (let i = 0; i < length; i++) {
  210. const key = keys[i];
  211. const value = obj[key];
  212. this.writeStr(key);
  213. writer.u8(0x3a);
  214. this.writeAny(value);
  215. writer.u8(0x2c);
  216. }
  217. writer.uint8[writer.x - 1] = 0x7d;
  218. }
  219. writeObjSeparator() {
  220. this.writer.u8(0x2c);
  221. }
  222. writeObjKeySeparator() {
  223. this.writer.u8(0x3a);
  224. }
  225. writeStartStr() {
  226. throw new Error('Method not implemented.');
  227. }
  228. writeStrChunk(str) {
  229. throw new Error('Method not implemented.');
  230. }
  231. writeEndStr() {
  232. throw new Error('Method not implemented.');
  233. }
  234. writeStartBin() {
  235. throw new Error('Method not implemented.');
  236. }
  237. writeBinChunk(buf) {
  238. throw new Error('Method not implemented.');
  239. }
  240. writeEndBin() {
  241. throw new Error('Method not implemented.');
  242. }
  243. writeStartArr() {
  244. this.writer.u8(0x5b);
  245. }
  246. writeArrChunk(item) {
  247. throw new Error('Method not implemented.');
  248. }
  249. writeEndArr() {
  250. this.writer.u8(0x5d);
  251. }
  252. writeStartObj() {
  253. this.writer.u8(0x7b);
  254. }
  255. writeObjChunk(key, value) {
  256. throw new Error('Method not implemented.');
  257. }
  258. writeEndObj() {
  259. this.writer.u8(0x7d);
  260. }
  261. }
  262. exports.JsonEncoder = JsonEncoder;
  263. //# sourceMappingURL=JsonEncoder.js.map