JsonParser.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2012 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "JsonParser.h"
  36. #include <cassert>
  37. #include "StructParserStateMachine.h"
  38. #include "util.h"
  39. namespace aria2 {
  40. namespace json {
  41. namespace {
  42. enum {
  43. JSON_FINISH,
  44. JSON_ERROR,
  45. JSON_VALUE,
  46. JSON_OBJECT_KEY,
  47. JSON_OBJECT_VAL,
  48. JSON_OBJECT_SEP,
  49. JSON_ARRAY,
  50. JSON_ARRAY_SEP,
  51. JSON_STRING,
  52. JSON_STRING_ESCAPE,
  53. JSON_STRING_UNICODE,
  54. JSON_STRING_LOW_SURROGATE_ESCAPE,
  55. JSON_STRING_LOW_SURROGATE_U,
  56. JSON_STRING_LOW_SURROGATE,
  57. JSON_NUMBER,
  58. JSON_NUMBER_FRAC,
  59. JSON_NUMBER_EXP_SIGN,
  60. JSON_NUMBER_EXP,
  61. JSON_TRUE,
  62. JSON_FALSE,
  63. JSON_NULL
  64. };
  65. } // namespace
  66. namespace {
  67. const char JSON_TRUE_STR[] = "true";
  68. const char JSON_FALSE_STR[] = "false";
  69. const char JSON_NULL_STR[] = "null";
  70. } // namespace
  71. JsonParser::JsonParser(StructParserStateMachine* psm)
  72. : psm_(psm),
  73. currentState_(JSON_VALUE),
  74. codepoint_(0),
  75. codepoint2_(0),
  76. numberSign_(1),
  77. number_(0),
  78. frac_(0),
  79. expSign_(1),
  80. exp_(0),
  81. numConsumed_(0),
  82. lastError_(0)
  83. {
  84. stateStack_.push(JSON_FINISH);
  85. }
  86. JsonParser::~JsonParser() = default;
  87. namespace {
  88. bool isSpace(char c) { return util::isLws(c) || util::isCRLF(c); }
  89. } // namespace
  90. ssize_t JsonParser::parseUpdate(const char* data, size_t size)
  91. {
  92. size_t i;
  93. if (currentState_ == JSON_FINISH) {
  94. return 0;
  95. }
  96. else if (currentState_ == JSON_ERROR) {
  97. return lastError_;
  98. }
  99. for (i = 0; i < size && currentState_ != JSON_FINISH; ++i) {
  100. char c = data[i];
  101. switch (currentState_) {
  102. case JSON_ARRAY:
  103. if (c == ']') {
  104. onArrayEnd();
  105. break;
  106. }
  107. else if (isSpace(c)) {
  108. break;
  109. }
  110. else {
  111. int rv = pushState(currentState_);
  112. if (rv < 0) {
  113. return rv;
  114. }
  115. currentState_ = JSON_VALUE;
  116. runBeginCallback(STRUCT_ARRAY_DATA_T);
  117. }
  118. // Fall through
  119. case JSON_VALUE:
  120. switch (c) {
  121. case '{':
  122. currentState_ = JSON_OBJECT_KEY;
  123. runBeginCallback(STRUCT_DICT_T);
  124. break;
  125. case '[':
  126. currentState_ = JSON_ARRAY;
  127. runBeginCallback(STRUCT_ARRAY_T);
  128. break;
  129. case '"':
  130. currentState_ = JSON_STRING;
  131. runBeginCallback(STRUCT_STRING_T);
  132. break;
  133. case '-':
  134. number_ = 0;
  135. numberSign_ = -1;
  136. numConsumed_ = 0;
  137. currentState_ = JSON_NUMBER;
  138. runBeginCallback(STRUCT_NUMBER_T);
  139. break;
  140. case 't':
  141. numConsumed_ = 1;
  142. currentState_ = JSON_TRUE;
  143. runBeginCallback(STRUCT_BOOL_T);
  144. break;
  145. case 'f':
  146. numConsumed_ = 1;
  147. currentState_ = JSON_FALSE;
  148. runBeginCallback(STRUCT_BOOL_T);
  149. break;
  150. case 'n':
  151. numConsumed_ = 1;
  152. currentState_ = JSON_NULL;
  153. runBeginCallback(STRUCT_NULL_T);
  154. break;
  155. default:
  156. if (util::isDigit(c)) {
  157. number_ = c - '0';
  158. numberSign_ = 1;
  159. numConsumed_ = 1;
  160. currentState_ = JSON_NUMBER;
  161. runBeginCallback(STRUCT_NUMBER_T);
  162. }
  163. else if (!isSpace(c)) {
  164. currentState_ = JSON_ERROR;
  165. return lastError_ = ERR_UNEXPECTED_CHAR_BEFORE_VAL;
  166. }
  167. }
  168. break;
  169. case JSON_TRUE:
  170. if (JSON_TRUE_STR[numConsumed_] == c) {
  171. ++numConsumed_;
  172. if (numConsumed_ == sizeof(JSON_TRUE_STR) - 1) {
  173. runBoolCallback(true);
  174. onBoolEnd();
  175. }
  176. }
  177. else {
  178. currentState_ = JSON_ERROR;
  179. return lastError_ = ERR_UNEXPECTED_LITERAL;
  180. }
  181. break;
  182. case JSON_FALSE:
  183. if (JSON_FALSE_STR[numConsumed_] == c) {
  184. ++numConsumed_;
  185. if (numConsumed_ == sizeof(JSON_FALSE_STR) - 1) {
  186. runBoolCallback(false);
  187. onBoolEnd();
  188. }
  189. }
  190. else {
  191. currentState_ = JSON_ERROR;
  192. return lastError_ = ERR_UNEXPECTED_LITERAL;
  193. }
  194. break;
  195. case JSON_NULL:
  196. if (JSON_NULL_STR[numConsumed_] == c) {
  197. ++numConsumed_;
  198. if (numConsumed_ == sizeof(JSON_NULL_STR) - 1) {
  199. onNullEnd();
  200. }
  201. }
  202. else {
  203. currentState_ = JSON_ERROR;
  204. return lastError_ = ERR_UNEXPECTED_LITERAL;
  205. }
  206. break;
  207. case JSON_OBJECT_KEY:
  208. switch (c) {
  209. case '"': {
  210. int rv = pushState(currentState_);
  211. if (rv < 0) {
  212. return rv;
  213. }
  214. currentState_ = JSON_STRING;
  215. runBeginCallback(STRUCT_DICT_KEY_T);
  216. break;
  217. }
  218. case '}':
  219. onObjectEnd();
  220. break;
  221. default:
  222. if (!isSpace(c)) {
  223. currentState_ = JSON_ERROR;
  224. return lastError_ = ERR_UNEXPECTED_CHAR_BEFORE_OBJ_KEY;
  225. }
  226. }
  227. break;
  228. case JSON_OBJECT_VAL:
  229. switch (c) {
  230. case ':':
  231. pushState(currentState_);
  232. currentState_ = JSON_VALUE;
  233. runBeginCallback(STRUCT_DICT_DATA_T);
  234. break;
  235. default:
  236. if (!isSpace(c)) {
  237. currentState_ = JSON_ERROR;
  238. return lastError_ = ERR_UNEXPECTED_CHAR_BEFORE_OBJ_VAL;
  239. }
  240. }
  241. break;
  242. case JSON_OBJECT_SEP:
  243. switch (c) {
  244. case ',':
  245. currentState_ = JSON_OBJECT_KEY;
  246. break;
  247. case '}':
  248. onObjectEnd();
  249. break;
  250. default:
  251. if (!isSpace(c)) {
  252. currentState_ = JSON_ERROR;
  253. return lastError_ = ERR_UNEXPECTED_CHAR_BEFORE_OBJ_SEP;
  254. }
  255. }
  256. break;
  257. case JSON_ARRAY_SEP:
  258. switch (c) {
  259. case ',':
  260. pushState(JSON_ARRAY);
  261. currentState_ = JSON_VALUE;
  262. runBeginCallback(STRUCT_ARRAY_DATA_T);
  263. break;
  264. case ']':
  265. onArrayEnd();
  266. break;
  267. default:
  268. if (!isSpace(c)) {
  269. currentState_ = JSON_ERROR;
  270. return lastError_ = ERR_UNEXPECTED_CHAR_BEFORE_ARRAY_SEP;
  271. }
  272. }
  273. break;
  274. case JSON_STRING:
  275. switch (c) {
  276. case '"':
  277. onStringEnd();
  278. break;
  279. case '\\':
  280. currentState_ = JSON_STRING_ESCAPE;
  281. break;
  282. default: {
  283. size_t j;
  284. for (j = i; j < size && data[j] != '\\' && data[j] != '"'; ++j)
  285. ;
  286. if (j - i >= 1) {
  287. runCharactersCallback(&data[i], j - i);
  288. }
  289. i = j - 1;
  290. break;
  291. }
  292. }
  293. break;
  294. case JSON_STRING_ESCAPE:
  295. switch (c) {
  296. case 'u':
  297. codepoint_ = 0;
  298. numConsumed_ = 0;
  299. currentState_ = JSON_STRING_UNICODE;
  300. break;
  301. default:
  302. switch (c) {
  303. case 'b':
  304. runCharactersCallback("\b", 1);
  305. break;
  306. case 'f':
  307. runCharactersCallback("\f", 1);
  308. break;
  309. case 'n':
  310. runCharactersCallback("\n", 1);
  311. break;
  312. case 'r':
  313. runCharactersCallback("\r", 1);
  314. break;
  315. case 't':
  316. runCharactersCallback("\t", 1);
  317. break;
  318. default: {
  319. char temp[1];
  320. temp[0] = c;
  321. runCharactersCallback(temp, 1);
  322. break;
  323. }
  324. }
  325. currentState_ = JSON_STRING;
  326. }
  327. break;
  328. case JSON_STRING_UNICODE: {
  329. size_t j;
  330. for (j = i; j < size && currentState_ == JSON_STRING_UNICODE; ++j) {
  331. if (util::isHexDigit(data[j])) {
  332. consumeUnicode(data[j]);
  333. }
  334. else {
  335. currentState_ = JSON_ERROR;
  336. return lastError_ = ERR_INVALID_UNICODE_POINT;
  337. }
  338. }
  339. i = j - 1;
  340. break;
  341. }
  342. case JSON_STRING_LOW_SURROGATE_ESCAPE:
  343. switch (c) {
  344. case '\\':
  345. currentState_ = JSON_STRING_LOW_SURROGATE_U;
  346. break;
  347. default:
  348. currentState_ = JSON_ERROR;
  349. return lastError_ = ERR_INVALID_UNICODE_POINT;
  350. }
  351. break;
  352. case JSON_STRING_LOW_SURROGATE_U:
  353. switch (c) {
  354. case 'u':
  355. codepoint2_ = 0;
  356. numConsumed_ = 0;
  357. currentState_ = JSON_STRING_LOW_SURROGATE;
  358. break;
  359. default:
  360. currentState_ = JSON_ERROR;
  361. return lastError_ = ERR_INVALID_UNICODE_POINT;
  362. }
  363. break;
  364. case JSON_STRING_LOW_SURROGATE: {
  365. size_t j;
  366. for (j = i; j < size && currentState_ == JSON_STRING_LOW_SURROGATE; ++j) {
  367. if (util::isHexDigit(data[j])) {
  368. int rv = consumeLowSurrogate(data[j]);
  369. if (rv != 0) {
  370. currentState_ = JSON_ERROR;
  371. lastError_ = rv;
  372. return rv;
  373. }
  374. }
  375. else {
  376. currentState_ = JSON_ERROR;
  377. return lastError_ = ERR_INVALID_UNICODE_POINT;
  378. }
  379. }
  380. i = j - 1;
  381. break;
  382. }
  383. case JSON_NUMBER: {
  384. size_t j;
  385. for (j = i; j < size && in(data[j], '0', '9'); ++j) {
  386. if ((INT64_MAX - (data[j] - '0')) / 10 < number_) {
  387. currentState_ = JSON_ERROR;
  388. return lastError_ = ERR_NUMBER_OUT_OF_RANGE;
  389. }
  390. number_ *= 10;
  391. number_ += data[j] - '0';
  392. }
  393. numConsumed_ += j - i;
  394. if (j != size) {
  395. if (numConsumed_ == 0) {
  396. currentState_ = JSON_ERROR;
  397. return lastError_ = ERR_INVALID_NUMBER;
  398. }
  399. switch (data[j]) {
  400. case '.':
  401. frac_ = 0;
  402. numConsumed_ = 0;
  403. currentState_ = JSON_NUMBER_FRAC;
  404. i = j;
  405. break;
  406. case 'e':
  407. case 'E':
  408. expSign_ = 1;
  409. exp_ = 0;
  410. numConsumed_ = 0;
  411. currentState_ = JSON_NUMBER_EXP_SIGN;
  412. i = j;
  413. break;
  414. default:
  415. onNumberEnd();
  416. i = j - 1;
  417. }
  418. }
  419. else {
  420. i = j - 1;
  421. }
  422. break;
  423. }
  424. case JSON_NUMBER_FRAC: {
  425. size_t j;
  426. for (j = i; j < size && in(data[j], '0', '9'); ++j) {
  427. // Take into account at most 8 digits
  428. if (frac_ < 100000000) {
  429. frac_ *= 10;
  430. frac_ += data[j] - '0';
  431. }
  432. }
  433. numConsumed_ += j - i;
  434. if (j != size) {
  435. if (numConsumed_ == 0) {
  436. currentState_ = JSON_ERROR;
  437. return lastError_ = ERR_INVALID_NUMBER;
  438. }
  439. switch (data[j]) {
  440. case 'e':
  441. case 'E':
  442. exp_ = 0;
  443. numConsumed_ = 0;
  444. currentState_ = JSON_NUMBER_EXP_SIGN;
  445. i = j;
  446. break;
  447. default:
  448. i = j - 1;
  449. onNumberEnd();
  450. }
  451. }
  452. else {
  453. i = j - 1;
  454. }
  455. break;
  456. }
  457. case JSON_NUMBER_EXP_SIGN:
  458. switch (c) {
  459. case '+':
  460. currentState_ = JSON_NUMBER_EXP;
  461. break;
  462. case '-':
  463. expSign_ = -1;
  464. currentState_ = JSON_NUMBER_EXP;
  465. break;
  466. default:
  467. break;
  468. }
  469. if (currentState_ == JSON_NUMBER_EXP) {
  470. break;
  471. }
  472. else {
  473. currentState_ = JSON_NUMBER_EXP;
  474. // Fall through
  475. }
  476. case JSON_NUMBER_EXP: {
  477. size_t j;
  478. for (j = i; j < size && in(data[j], '0', '9'); ++j) {
  479. // Take into account at most 8 digits
  480. exp_ *= 10;
  481. exp_ += data[j] - '0';
  482. if (exp_ > 18) {
  483. currentState_ = JSON_ERROR;
  484. return lastError_ = ERR_NUMBER_OUT_OF_RANGE;
  485. }
  486. }
  487. numConsumed_ += j - i;
  488. if (j != size) {
  489. if (numConsumed_ == 0) {
  490. currentState_ = JSON_ERROR;
  491. return lastError_ = ERR_INVALID_NUMBER;
  492. }
  493. switch (data[j]) {
  494. default:
  495. onNumberEnd();
  496. }
  497. }
  498. i = j - 1;
  499. break;
  500. }
  501. }
  502. }
  503. return i;
  504. }
  505. ssize_t JsonParser::parseFinal(const char* data, size_t len)
  506. {
  507. ssize_t rv;
  508. rv = parseUpdate(data, len);
  509. if (rv >= 0) {
  510. if (currentState_ != JSON_FINISH) {
  511. rv = ERR_PREMATURE_DATA;
  512. }
  513. }
  514. return rv;
  515. }
  516. void JsonParser::reset()
  517. {
  518. psm_->reset();
  519. currentState_ = JSON_VALUE;
  520. lastError_ = 0;
  521. while (!stateStack_.empty()) {
  522. stateStack_.pop();
  523. }
  524. stateStack_.push(JSON_FINISH);
  525. }
  526. void JsonParser::onStringEnd()
  527. {
  528. runEndCallback(stateTop() == JSON_OBJECT_KEY ? STRUCT_DICT_KEY_T
  529. : STRUCT_STRING_T);
  530. onValueEnd();
  531. }
  532. void JsonParser::onNumberEnd()
  533. {
  534. runNumberCallback(numberSign_ * number_, frac_, expSign_ * exp_);
  535. runEndCallback(STRUCT_NUMBER_T);
  536. onValueEnd();
  537. }
  538. void JsonParser::onObjectEnd()
  539. {
  540. runEndCallback(STRUCT_DICT_T);
  541. onValueEnd();
  542. }
  543. void JsonParser::onArrayEnd()
  544. {
  545. runEndCallback(STRUCT_ARRAY_T);
  546. onValueEnd();
  547. }
  548. void JsonParser::onBoolEnd()
  549. {
  550. runEndCallback(STRUCT_BOOL_T);
  551. onValueEnd();
  552. }
  553. void JsonParser::onNullEnd()
  554. {
  555. runEndCallback(STRUCT_NULL_T);
  556. onValueEnd();
  557. }
  558. void JsonParser::onValueEnd()
  559. {
  560. switch (stateTop()) {
  561. case JSON_OBJECT_KEY:
  562. popState();
  563. currentState_ = JSON_OBJECT_VAL;
  564. break;
  565. case JSON_OBJECT_VAL:
  566. runEndCallback(STRUCT_DICT_DATA_T);
  567. popState();
  568. currentState_ = JSON_OBJECT_SEP;
  569. break;
  570. case JSON_ARRAY:
  571. runEndCallback(STRUCT_ARRAY_DATA_T);
  572. popState();
  573. currentState_ = JSON_ARRAY_SEP;
  574. break;
  575. default:
  576. assert(stateTop() == JSON_FINISH);
  577. currentState_ = stateTop();
  578. break;
  579. }
  580. }
  581. int JsonParser::pushState(int state)
  582. {
  583. if (stateStack_.size() >= 50) {
  584. return ERR_STRUCTURE_TOO_DEEP;
  585. }
  586. else {
  587. stateStack_.push(state);
  588. return 0;
  589. }
  590. }
  591. int JsonParser::stateTop() const { return stateStack_.top(); }
  592. int JsonParser::popState()
  593. {
  594. int state = stateStack_.top();
  595. stateStack_.pop();
  596. return state;
  597. }
  598. void JsonParser::consumeUnicode(char c)
  599. {
  600. codepoint_ *= 16;
  601. codepoint_ += util::hexCharToUInt(c);
  602. ++numConsumed_;
  603. if (numConsumed_ == 4) {
  604. if (in(codepoint_, 0xD800u, 0xDBFFu)) {
  605. // This is high-surrogate codepoint
  606. currentState_ = JSON_STRING_LOW_SURROGATE_ESCAPE;
  607. }
  608. else {
  609. char temp[3];
  610. size_t len;
  611. if (codepoint_ <= 0x007fu) {
  612. temp[0] = static_cast<char>(codepoint_);
  613. len = 1;
  614. }
  615. else if (codepoint_ <= 0x07ffu) {
  616. temp[0] = 0xC0u | (codepoint_ >> 6);
  617. temp[1] = 0x80u | (codepoint_ & 0x003fu);
  618. len = 2;
  619. }
  620. else {
  621. temp[0] = 0xE0u | (codepoint_ >> 12);
  622. temp[1] = 0x80u | ((codepoint_ >> 6) & 0x003Fu);
  623. temp[2] = 0x80u | (codepoint_ & 0x003Fu);
  624. len = 3;
  625. }
  626. runCharactersCallback(temp, len);
  627. currentState_ = JSON_STRING;
  628. }
  629. }
  630. }
  631. int JsonParser::consumeLowSurrogate(char c)
  632. {
  633. codepoint2_ *= 16;
  634. codepoint2_ += util::hexCharToUInt(c);
  635. ++numConsumed_;
  636. if (numConsumed_ == 4) {
  637. if (!in(codepoint2_, 0xDC00u, 0xDFFFu)) {
  638. return ERR_INVALID_UNICODE_POINT;
  639. }
  640. uint32_t fullcodepoint = 0x010000u;
  641. fullcodepoint += (codepoint_ & 0x03FFu) << 10;
  642. fullcodepoint += (codepoint2_ & 0x03FFu);
  643. char temp[4];
  644. temp[0] = 0xf0u | (fullcodepoint >> 18);
  645. temp[1] = 0x80u | ((fullcodepoint >> 12) & 0x003Fu);
  646. temp[2] = 0x80u | ((fullcodepoint >> 6) & 0x003Fu);
  647. temp[3] = 0x80u | (fullcodepoint & 0x003Fu);
  648. runCharactersCallback(temp, sizeof(temp));
  649. currentState_ = JSON_STRING;
  650. }
  651. return 0;
  652. }
  653. void JsonParser::runBeginCallback(int elementType)
  654. {
  655. // switch(elementType) {
  656. // case STRUCT_DICT_T:
  657. // std::cout << "object start" << std::endl;
  658. // break;
  659. // case STRUCT_DICT_KEY_T:
  660. // std::cout << "object key start" << std::endl;
  661. // break;
  662. // case STRUCT_DICT_DATA_T:
  663. // std::cout << "object data start" << std::endl;
  664. // break;
  665. // case STRUCT_ARRAY_T:
  666. // std::cout << "array start" << std::endl;
  667. // break;
  668. // case STRUCT_ARRAY_DATA_T:
  669. // std::cout << "array data start" << std::endl;
  670. // break;
  671. // case STRUCT_STRING_T:
  672. // std::cout << "string start" << std::endl;
  673. // break;
  674. // case STRUCT_NUMBER_T:
  675. // std::cout << "number start" << std::endl;
  676. // break;
  677. // case STRUCT_BOOL_T:
  678. // std::cout << "bool start" << std::endl;
  679. // break;
  680. // case STRUCT_NULL_T:
  681. // std::cout << "null start" << std::endl;
  682. // break;
  683. // default:
  684. // break;
  685. // };
  686. psm_->beginElement(elementType);
  687. }
  688. void JsonParser::runEndCallback(int elementType)
  689. {
  690. // switch(elementType) {
  691. // case STRUCT_DICT_T:
  692. // std::cout << "object end" << std::endl;
  693. // break;
  694. // case STRUCT_DICT_KEY_T:
  695. // std::cout << "object key end" << std::endl;
  696. // break;
  697. // case STRUCT_DICT_DATA_T:
  698. // std::cout << "object data end" << std::endl;
  699. // break;
  700. // case STRUCT_ARRAY_T:
  701. // std::cout << "array end" << std::endl;
  702. // break;
  703. // case STRUCT_ARRAY_DATA_T:
  704. // std::cout << "array data end" << std::endl;
  705. // break;
  706. // case STRUCT_STRING_T:
  707. // std::cout << "string end" << std::endl;
  708. // break;
  709. // case STRUCT_NUMBER_T:
  710. // std::cout << "number end" << std::endl;
  711. // break;
  712. // case STRUCT_BOOL_T:
  713. // std::cout << "bool end" << std::endl;
  714. // break;
  715. // case STRUCT_NULL_T:
  716. // std::cout << "null end" << std::endl;
  717. // break;
  718. // default:
  719. // break;
  720. // };
  721. psm_->endElement(elementType);
  722. }
  723. void JsonParser::runCharactersCallback(const char* data, size_t len)
  724. {
  725. psm_->charactersCallback(data, len);
  726. }
  727. void JsonParser::runNumberCallback(int64_t number, int frac, int exp)
  728. {
  729. psm_->numberCallback(number, frac, exp);
  730. }
  731. void JsonParser::runBoolCallback(bool bval) { psm_->boolCallback(bval); }
  732. } // namespace json
  733. } // namespace aria2