json.js 18 KB

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