json.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. var json = json || {};
  2. var text = text || require('./text');
  3. json.TextReader = class {
  4. static open(data) {
  5. const decoder = text.Decoder.open(data);
  6. let state = 'start';
  7. for (let i = 0; i < 0x1000; i++) {
  8. const c = decoder.decode();
  9. if (c === undefined || c === '\0') {
  10. if (i === 0) {
  11. return null;
  12. }
  13. break;
  14. }
  15. if (c <= ' ') {
  16. if (c !== ' ' && c !== '\n' && c !== '\r' && c !== '\t') {
  17. return null;
  18. }
  19. continue;
  20. }
  21. switch (state) {
  22. case 'start':
  23. if (c === '#') {
  24. state = 'comment';
  25. }
  26. else if (c === '[') {
  27. state = 'list';
  28. }
  29. else if (c === '{') {
  30. state = 'object';
  31. }
  32. else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
  33. state = '';
  34. }
  35. else {
  36. return null;
  37. }
  38. break;
  39. case 'list':
  40. if (c === '"' || c === '-' || c === '+' || c === '{' || c === '[' || (c >= '0' && c <= '9')) {
  41. state = '';
  42. break;
  43. }
  44. return null;
  45. case 'object':
  46. if (c != '"') {
  47. return null;
  48. }
  49. state = '';
  50. continue;
  51. default:
  52. break;
  53. }
  54. }
  55. return new json.TextReader(data);
  56. }
  57. constructor(data) {
  58. this._data = data;
  59. this._escape = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' };
  60. }
  61. read() {
  62. const decoder = text.Decoder.open(this._data);
  63. const stack = [];
  64. this._decoder = decoder;
  65. this._position = 0;
  66. this._char = decoder.decode();
  67. this._whitespace();
  68. let obj = undefined;
  69. let first = true;
  70. for (;;) {
  71. if (Array.isArray(obj)) {
  72. this._whitespace();
  73. let c = this._char;
  74. if (c === ']') {
  75. this._next();
  76. this._whitespace();
  77. if (stack.length > 0) {
  78. obj = stack.pop();
  79. first = false;
  80. continue;
  81. }
  82. if (this._char !== undefined) {
  83. this._unexpected();
  84. }
  85. return obj;
  86. }
  87. if (!first) {
  88. if (this._char !== ',') {
  89. this._unexpected();
  90. }
  91. this._next();
  92. this._whitespace();
  93. c = this._char;
  94. }
  95. first = false;
  96. switch (c) {
  97. case '{': {
  98. this._next();
  99. stack.push(obj);
  100. const item = {};
  101. obj.push(item);
  102. obj = item;
  103. first = true;
  104. break;
  105. }
  106. case '[': {
  107. this._next();
  108. stack.push(obj);
  109. const item = [];
  110. obj.push(item);
  111. obj = item;
  112. first = true;
  113. break;
  114. }
  115. default: {
  116. obj.push(c === '"' ? this._string() : this._literal());
  117. break;
  118. }
  119. }
  120. }
  121. else if (obj instanceof Object) {
  122. this._whitespace();
  123. let c = this._char;
  124. if (c === '}') {
  125. this._next();
  126. this._whitespace();
  127. if (stack.length > 0) {
  128. obj = stack.pop();
  129. first = false;
  130. continue;
  131. }
  132. if (this._char !== undefined) {
  133. this._unexpected();
  134. }
  135. return obj;
  136. }
  137. if (!first) {
  138. if (this._char !== ',') {
  139. this._unexpected();
  140. }
  141. this._next();
  142. this._whitespace();
  143. c = this._char;
  144. }
  145. first = false;
  146. if (c === '"') {
  147. const key = this._string();
  148. switch (key) {
  149. case '__proto__':
  150. case 'constructor':
  151. case 'prototype':
  152. throw new json.Error("Invalid key '" + key + "'" + this._location());
  153. default:
  154. break;
  155. }
  156. this._whitespace();
  157. if (this._char !== ':') {
  158. this._unexpected();
  159. }
  160. this._next();
  161. this._whitespace();
  162. c = this._char;
  163. switch (c) {
  164. case '{': {
  165. this._next();
  166. stack.push(obj);
  167. const value = {};
  168. obj[key] = value;
  169. obj = value;
  170. first = true;
  171. break;
  172. }
  173. case '[': {
  174. this._next();
  175. stack.push(obj);
  176. const value = [];
  177. obj[key] = value;
  178. obj = value;
  179. first = true;
  180. break;
  181. }
  182. default: {
  183. obj[key] = c === '"' ? this._string() : this._literal();
  184. break;
  185. }
  186. }
  187. this._whitespace();
  188. continue;
  189. }
  190. this._unexpected();
  191. }
  192. else {
  193. const c = this._char;
  194. switch (c) {
  195. case '{': {
  196. this._next();
  197. this._whitespace();
  198. obj = {};
  199. first = true;
  200. break;
  201. }
  202. case '[': {
  203. this._next();
  204. this._whitespace();
  205. obj = [];
  206. first = true;
  207. break;
  208. }
  209. default: {
  210. const value = c === '"' ? this._string() : c >= '0' && c <= '9' ? this._number() : this._literal();
  211. this._whitespace();
  212. if (this._char !== undefined) {
  213. this._unexpected();
  214. }
  215. return value;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. _next() {
  222. if (this._char === undefined) {
  223. this._unexpected();
  224. }
  225. this._position = this._decoder.position;
  226. this._char = this._decoder.decode();
  227. }
  228. _whitespace() {
  229. while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t') {
  230. this._next();
  231. }
  232. }
  233. _literal() {
  234. const c = this._char;
  235. if (c >= '0' && c <= '9') {
  236. return this._number();
  237. }
  238. switch (c) {
  239. case 't': this._expect('true'); return true;
  240. case 'f': this._expect('false'); return false;
  241. case 'n': this._expect('null'); return null;
  242. case 'N': this._expect('NaN'); return NaN;
  243. case 'I': this._expect('Infinity'); return Infinity;
  244. case '-': return this._number();
  245. default: this._unexpected();
  246. }
  247. return null;
  248. }
  249. _number() {
  250. let value = '';
  251. if (this._char === '-') {
  252. value = '-';
  253. this._next();
  254. }
  255. if (this._char === 'I') {
  256. this._expect('Infinity');
  257. return -Infinity;
  258. }
  259. const c = this._char;
  260. if (c < '0' || c > '9') {
  261. this._unexpected();
  262. }
  263. value += c;
  264. this._next();
  265. if (c === '0') {
  266. const n = this._char;
  267. if (n >= '0' && n <= '9') {
  268. this._unexpected();
  269. }
  270. }
  271. while (this._char >= '0' && this._char <= '9') {
  272. value += this._char;
  273. this._next();
  274. }
  275. if (this._char === '.') {
  276. value += '.';
  277. this._next();
  278. const n = this._char;
  279. if (n < '0' || n > '9') {
  280. this._unexpected();
  281. }
  282. while (this._char >= '0' && this._char <= '9') {
  283. value += this._char;
  284. this._next();
  285. }
  286. }
  287. if (this._char === 'e' || this._char === 'E') {
  288. value += this._char;
  289. this._next();
  290. const s = this._char;
  291. if (s === '-' || s === '+') {
  292. value += this._char;
  293. this._next();
  294. }
  295. const c = this._char;
  296. if (c < '0' || c > '9') {
  297. this._unexpected();
  298. }
  299. value += this._char;
  300. this._next();
  301. while (this._char >= '0' && this._char <= '9') {
  302. value += this._char;
  303. this._next();
  304. }
  305. }
  306. return +value;
  307. }
  308. _string() {
  309. let value = '';
  310. this._next();
  311. while (this._char != '"') {
  312. if (this._char === '\\') {
  313. this._next();
  314. if (this._char === 'u') {
  315. this._next();
  316. let uffff = 0;
  317. for (let i = 0; i < 4; i ++) {
  318. const hex = parseInt(this._char, 16);
  319. if (!isFinite(hex)) {
  320. this._unexpected();
  321. }
  322. this._next();
  323. uffff = uffff * 16 + hex;
  324. }
  325. value += String.fromCharCode(uffff);
  326. }
  327. else if (this._escape[this._char]) {
  328. value += this._escape[this._char];
  329. this._next();
  330. }
  331. else {
  332. this._unexpected();
  333. }
  334. }
  335. else if (this._char < ' ') {
  336. this._unexpected();
  337. }
  338. else {
  339. value += this._char;
  340. this._next();
  341. }
  342. }
  343. this._next();
  344. return value;
  345. }
  346. _expect(value) {
  347. for (let i = 0; i < value.length; i++) {
  348. if (value[i] !== this._char) {
  349. this._unexpected();
  350. }
  351. this._next();
  352. }
  353. }
  354. _unexpected() {
  355. let c = this._char;
  356. if (c === undefined) {
  357. throw new json.Error('Unexpected end of JSON input.');
  358. }
  359. else if (c === '"') {
  360. c = 'string';
  361. }
  362. else if ((c >= '0' && c <= '9') || c === '-') {
  363. c = 'number';
  364. }
  365. else {
  366. if (c < ' ' || c > '\x7F') {
  367. const name = Object.keys(this._escape).filter((key) => this._escape[key] === c);
  368. c = (name.length === 1) ? '\\' + name : '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4);
  369. }
  370. c = "token '" + c + "'";
  371. }
  372. throw new json.Error('Unexpected ' + c + this._location());
  373. }
  374. _location() {
  375. let line = 1;
  376. let column = 1;
  377. this._decoder.position = 0;
  378. let c;
  379. do {
  380. if (this._decoder.position === this._position) {
  381. return ' at ' + line.toString() + ':' + column.toString() + '.';
  382. }
  383. c = this._decoder.decode();
  384. if (c === '\n') {
  385. line++;
  386. column = 1;
  387. }
  388. else {
  389. column++;
  390. }
  391. }
  392. while (c !== undefined);
  393. return ' at ' + line.toString() + ':' + column.toString() + '.';
  394. }
  395. };
  396. json.BinaryReader = class {
  397. static open(data) {
  398. return data ? new json.BinaryReader(data) : null;
  399. }
  400. constructor(data) {
  401. this._buffer = data instanceof Uint8Array ? data : data.peek();
  402. }
  403. read() {
  404. const buffer = this._buffer;
  405. const length = buffer.length;
  406. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  407. const asciiDecoder = new TextDecoder('ascii');
  408. const utf8Decoder = new TextDecoder('utf-8');
  409. let position = 0;
  410. const skip = (offset) => {
  411. position += offset;
  412. if (position > length) {
  413. throw new json.Error('Expected ' + (position + length) + ' more bytes. The file might be corrupted. Unexpected end of file.', true);
  414. }
  415. };
  416. const header = () => {
  417. const start = position;
  418. skip(4);
  419. const size = view.getInt32(start, 4);
  420. if (size < 5 || start + size > length || buffer[start + size - 1] != 0x00) {
  421. throw new json.Error('Invalid file size.', true);
  422. }
  423. };
  424. header();
  425. const stack = [];
  426. let obj = {};
  427. for (;;) {
  428. skip(1);
  429. const type = buffer[position - 1];
  430. if (type == 0x00) {
  431. if (stack.length === 0) {
  432. break;
  433. }
  434. obj = stack.pop();
  435. continue;
  436. }
  437. const start = position;
  438. position = buffer.indexOf(0x00, start) + 1;
  439. const key = asciiDecoder.decode(buffer.subarray(start, position - 1));
  440. let value = null;
  441. switch (type) {
  442. case 0x01: { // float64
  443. const start = position;
  444. skip(8);
  445. value = view.getFloat64(start, true);
  446. break;
  447. }
  448. case 0x02: { // string
  449. skip(4);
  450. const size = view.getInt32(position - 4, true);
  451. const start = position;
  452. skip(size);
  453. value = utf8Decoder.decode(buffer.subarray(start, position - 1));
  454. if (buffer[position - 1] != '0x00') {
  455. throw new json.Error('String missing terminal 0.', true);
  456. }
  457. break;
  458. }
  459. case 0x03: { // object
  460. header();
  461. value = {};
  462. break;
  463. }
  464. case 0x04: { // array
  465. header();
  466. value = [];
  467. break;
  468. }
  469. case 0x05: { // bytes
  470. const start = position;
  471. skip(5);
  472. const size = view.getInt32(start, true);
  473. const subtype = buffer[start + 4];
  474. if (subtype !== 0x00) {
  475. throw new json.Error("Unsupported binary subtype '" + subtype + "'.", true);
  476. }
  477. skip(size);
  478. value = buffer.subarray(start + 5, position);
  479. break;
  480. }
  481. case 0x08: { // boolean
  482. skip(1);
  483. value = buffer[position - 1];
  484. if (value > 1) {
  485. throw new json.Error("Invalid boolean value '" + value + "'.", true);
  486. }
  487. value = value === 1 ? true : false;
  488. break;
  489. }
  490. case 0x0A:
  491. value = null;
  492. break;
  493. case 0x10: {
  494. const start = position;
  495. skip(4);
  496. value = view.getInt32(start, true);
  497. break;
  498. }
  499. case 0x11: { // uint64
  500. const start = position;
  501. skip(8);
  502. value = view.getUint64(start, true).toNumber();
  503. break;
  504. }
  505. case 0x12: { // int64
  506. const start = position;
  507. skip(8);
  508. value = view.getInt64(start, true).toNumber();
  509. break;
  510. }
  511. default: {
  512. throw new json.Error("Unsupported value type '" + type + "'.", true);
  513. }
  514. }
  515. if (Array.isArray(obj)) {
  516. if (obj.length !== parseInt(key, 10)) {
  517. throw new json.Error("Invalid array index '" + key + "'.", true);
  518. }
  519. obj.push(value);
  520. }
  521. else {
  522. switch (key) {
  523. case '__proto__':
  524. case 'constructor':
  525. case 'prototype':
  526. throw new json.Error("Invalid key '" + key + "' at " + position.toString() + "'.", true);
  527. default:
  528. break;
  529. }
  530. obj[key] = value;
  531. }
  532. if (type === 0x03 || type === 0x04) {
  533. stack.push(obj);
  534. obj = value;
  535. }
  536. }
  537. if (position !== length) {
  538. throw new json.Error("Unexpected data at '" + position.toString() + "'.", true);
  539. }
  540. return obj;
  541. }
  542. };
  543. json.Error = class extends Error {
  544. constructor(message, binary) {
  545. super(message);
  546. this.name = binary ? 'BSON Error' : 'JSON Error';
  547. }
  548. };
  549. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  550. module.exports.TextReader = json.TextReader;
  551. module.exports.BinaryReader = json.BinaryReader;
  552. }