JSONParser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "JSONParser.h"
  24. #include "TempAllocator.h"
  25. #include "StringUtils.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. static const char* next(const char* str, const char c = 0)
  30. {
  31. CE_ASSERT_NOT_NULL(str);
  32. if (c && c != (*str))
  33. {
  34. CE_ASSERT(false, "Expected '%c' got '%c'", c, (*str));
  35. }
  36. return str + 1;
  37. }
  38. //-----------------------------------------------------------------------------
  39. static const char* skip_whites(const char* s)
  40. {
  41. CE_ASSERT_NOT_NULL(s);
  42. const char* ch = s;
  43. while ((*ch) && (*ch) <= ' ') ch = next(ch);
  44. return ch;
  45. }
  46. //-----------------------------------------------------------------------------
  47. static const char* skip_string(const char* s)
  48. {
  49. CE_ASSERT_NOT_NULL(s);
  50. const char* ch = s;
  51. bool escaped = false;
  52. if ((*ch) == '"')
  53. {
  54. while ((*(ch = next(ch))) != 0)
  55. {
  56. if ((*ch) == '"' && !escaped)
  57. {
  58. ch = next(ch);
  59. return ch;
  60. }
  61. else if ((*ch) == '\\') escaped = true;
  62. else escaped = false;
  63. }
  64. }
  65. return ch;
  66. }
  67. //-----------------------------------------------------------------------------
  68. static const char* skip_number(const char* s)
  69. {
  70. CE_ASSERT_NOT_NULL(s);
  71. const char* ch = s;
  72. while ((*ch) && (((*ch) >= '0' && (*ch) <= '9') ||
  73. (*ch) == '-' || (*ch) == '.' || (*ch) == '+' ||
  74. (*ch) == 'e' || (*ch) == 'E'))
  75. {
  76. ch = next(ch);
  77. }
  78. return ch;
  79. }
  80. //-----------------------------------------------------------------------------
  81. static const char* skip_object(const char* s)
  82. {
  83. CE_ASSERT_NOT_NULL(s);
  84. const char* ch = s;
  85. uint32_t brackets = 1;
  86. if ((*ch) == '{')
  87. {
  88. brackets++;
  89. ch = next(ch, '{');
  90. while ((*ch) && brackets != 1)
  91. {
  92. if ((*ch) == '}') brackets--;
  93. else if ((*ch) == '{') brackets++;
  94. ch = next(ch);
  95. }
  96. }
  97. return ch;
  98. }
  99. //-----------------------------------------------------------------------------
  100. static const char* skip_array(const char* s)
  101. {
  102. CE_ASSERT_NOT_NULL(s);
  103. const char* ch = s;
  104. uint32_t brackets = 1;
  105. if ((*ch) == '[')
  106. {
  107. brackets++;
  108. ch = next(ch, '[');
  109. while ((*ch) && brackets != 1)
  110. {
  111. if ((*ch) == ']') brackets--;
  112. else if ((*ch) == '[') brackets++;
  113. ch = next(ch);
  114. }
  115. }
  116. return ch;
  117. }
  118. //-----------------------------------------------------------------------------
  119. static const char* skip_bool(const char* s)
  120. {
  121. CE_ASSERT_NOT_NULL(s);
  122. const char* ch = s;
  123. switch ((*ch))
  124. {
  125. case 't':
  126. {
  127. ch = next(ch, 't');
  128. ch = next(ch, 'r');
  129. ch = next(ch, 'u');
  130. ch = next(ch, 'e');
  131. break;
  132. }
  133. case 'f':
  134. {
  135. ch = next(ch, 'f');
  136. ch = next(ch, 'a');
  137. ch = next(ch, 'l');
  138. ch = next(ch, 's');
  139. ch = next(ch, 'e');
  140. break;
  141. }
  142. default:
  143. {
  144. break;
  145. }
  146. }
  147. return ch;
  148. }
  149. //-----------------------------------------------------------------------------
  150. static bool is_escapee(char c)
  151. {
  152. return c == '"' || c == '\\' || c == '/' || c == '\b' || c == '\f' || c == '\n' ||
  153. c == '\r' || c == '\t';
  154. }
  155. //--------------------------------------------------------------------------
  156. JSONElement::JSONElement() :
  157. m_parser(NULL),
  158. m_at(NULL)
  159. {
  160. }
  161. //--------------------------------------------------------------------------
  162. JSONElement::JSONElement(JSONParser& parser, const char* at) :
  163. m_parser(&parser),
  164. m_at(at)
  165. {
  166. }
  167. //--------------------------------------------------------------------------
  168. JSONElement& JSONElement::operator[](uint32_t i)
  169. {
  170. TempAllocator1024 alloc;
  171. List<const char*> array(alloc);
  172. JSONParser::parse_array(m_at, array);
  173. CE_ASSERT(i < array.size(), "Index out of bounds");
  174. m_at = array[i];
  175. return *this;
  176. }
  177. //--------------------------------------------------------------------------
  178. JSONElement& JSONElement::index(uint32_t i)
  179. {
  180. return this->operator[](i);
  181. }
  182. //--------------------------------------------------------------------------
  183. JSONElement& JSONElement::key(const char* k)
  184. {
  185. TempAllocator1024 alloc;
  186. List<JSONPair> object(alloc);
  187. JSONParser::parse_object(m_at, object);
  188. bool found = false;
  189. for (uint32_t i = 0; i < object.size(); i++)
  190. {
  191. TempAllocator256 key_alloc;
  192. List<char> key(key_alloc);
  193. JSONParser::parse_string(object[i].key, key);
  194. if (string::strcmp(k, key.begin()) == 0)
  195. {
  196. m_at = object[i].val;
  197. found = true;
  198. }
  199. }
  200. CE_ASSERT(found, "Key not found: '%s'", k);
  201. return *this;
  202. }
  203. //--------------------------------------------------------------------------
  204. bool JSONElement::has_key(const char* k) const
  205. {
  206. TempAllocator1024 alloc;
  207. List<JSONPair> object(alloc);
  208. JSONParser::parse_object(m_at, object);
  209. for (uint32_t i = 0; i < object.size(); i++)
  210. {
  211. if (string::strcmp(k, object[i].key) == 0)
  212. {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. //--------------------------------------------------------------------------
  219. bool JSONElement::is_key_unique(const char* k) const
  220. {
  221. TempAllocator1024 alloc;
  222. List<JSONPair> object(alloc);
  223. JSONParser::parse_object(m_at, object);
  224. bool found = false;
  225. for (uint32_t i = 0; i < object.size(); i++)
  226. {
  227. if (string::strcmp(k, object[i].key) == 0)
  228. {
  229. if (found == true)
  230. {
  231. return false;
  232. }
  233. found = true;
  234. }
  235. }
  236. return found;
  237. }
  238. //--------------------------------------------------------------------------
  239. bool JSONElement::bool_value() const
  240. {
  241. return JSONParser::parse_bool(m_at);
  242. }
  243. //--------------------------------------------------------------------------
  244. int32_t JSONElement::int_value() const
  245. {
  246. return JSONParser::parse_int(m_at);
  247. }
  248. //--------------------------------------------------------------------------
  249. float JSONElement::float_value() const
  250. {
  251. return JSONParser::parse_float(m_at);
  252. }
  253. //--------------------------------------------------------------------------
  254. const char* JSONElement::string_value() const
  255. {
  256. static TempAllocator1024 alloc;
  257. static List<char> string(alloc);
  258. string.clear();
  259. JSONParser::parse_string(m_at, string);
  260. return string.begin();
  261. }
  262. //--------------------------------------------------------------------------
  263. bool JSONElement::is_nil() const
  264. {
  265. return JSONParser::type(m_at) == JT_NIL;
  266. }
  267. //--------------------------------------------------------------------------
  268. bool JSONElement::is_bool() const
  269. {
  270. return JSONParser::type(m_at) == JT_BOOL;
  271. }
  272. //--------------------------------------------------------------------------
  273. bool JSONElement::is_number() const
  274. {
  275. return JSONParser::type(m_at) == JT_NUMBER;
  276. }
  277. //--------------------------------------------------------------------------
  278. bool JSONElement::is_string() const
  279. {
  280. return JSONParser::type(m_at) == JT_STRING;
  281. }
  282. //--------------------------------------------------------------------------
  283. bool JSONElement::is_array() const
  284. {
  285. return JSONParser::type(m_at) == JT_ARRAY;
  286. }
  287. //--------------------------------------------------------------------------
  288. bool JSONElement::is_object() const
  289. {
  290. return JSONParser::type(m_at) == JT_OBJECT;
  291. }
  292. //--------------------------------------------------------------------------
  293. uint32_t JSONElement::size() const
  294. {
  295. switch(JSONParser::type(m_at))
  296. {
  297. case JT_NIL:
  298. {
  299. return 1;
  300. }
  301. case JT_OBJECT:
  302. {
  303. TempAllocator1024 alloc;
  304. List<JSONPair> object(alloc);
  305. JSONParser::parse_object(m_at, object);
  306. return object.size();
  307. }
  308. case JT_ARRAY:
  309. {
  310. TempAllocator1024 alloc;
  311. List<const char*> array(alloc);
  312. JSONParser::parse_array(m_at, array);
  313. return array.size();
  314. }
  315. case JT_STRING:
  316. {
  317. TempAllocator1024 alloc;
  318. List<char> string(alloc);
  319. JSONParser::parse_string(m_at, string);
  320. return string::strlen(string.begin());
  321. }
  322. case JT_NUMBER:
  323. {
  324. return 1;
  325. }
  326. case JT_BOOL:
  327. {
  328. return 1;
  329. }
  330. default:
  331. {
  332. return 0;
  333. }
  334. }
  335. }
  336. //--------------------------------------------------------------------------
  337. JSONParser::JSONParser(const char* s) :
  338. m_document(s)
  339. {
  340. CE_ASSERT_NOT_NULL(s);
  341. }
  342. //--------------------------------------------------------------------------
  343. JSONElement JSONParser::root()
  344. {
  345. return JSONElement(*this, skip_whites(m_document));
  346. }
  347. //-----------------------------------------------------------------------------
  348. JSONType JSONParser::type(const char* s)
  349. {
  350. CE_ASSERT_NOT_NULL(s);
  351. switch (s[0])
  352. {
  353. case '{': return JT_OBJECT;
  354. case '[': return JT_ARRAY;
  355. case '"': return JT_STRING;
  356. case '-': return JT_NUMBER;
  357. default: return s[0] >= '0' && s[0] <= '9' ? JT_NUMBER : (s[0] == 'n' ? JT_NIL : JT_BOOL);
  358. }
  359. }
  360. //-----------------------------------------------------------------------------
  361. void JSONParser::parse_string(const char* s, List<char>& str)
  362. {
  363. CE_ASSERT_NOT_NULL(s);
  364. const char* ch = s;
  365. if ((*ch) == '"')
  366. {
  367. while ((*(ch = next(ch))))
  368. {
  369. // Empty string
  370. if ((*ch) == '"')
  371. {
  372. ch = next(ch);
  373. str.push_back('\0');
  374. return;
  375. }
  376. else if ((*ch) == '\\')
  377. {
  378. ch = next(ch);
  379. if ((*ch) == 'u')
  380. {
  381. CE_ASSERT(false, "Not supported at the moment");
  382. }
  383. else if (is_escapee(*ch))
  384. {
  385. str.push_back('\\');
  386. str.push_back(*ch);
  387. }
  388. else
  389. {
  390. // Go to invalid string
  391. break;
  392. }
  393. }
  394. else
  395. {
  396. str.push_back(*ch);
  397. }
  398. }
  399. }
  400. CE_ASSERT(false, "Bad string");
  401. }
  402. //-----------------------------------------------------------------------------
  403. double JSONParser::parse_number(const char* s)
  404. {
  405. CE_ASSERT_NOT_NULL(s);
  406. const char* ch = s;
  407. TempAllocator1024 allocator;
  408. List<char> str(allocator);
  409. if ((*ch) == '-')
  410. {
  411. str.push_back('-');
  412. ch = next(ch, '-');
  413. }
  414. while ((*ch) >= '0' && (*ch) <= '9')
  415. {
  416. str.push_back((*ch));
  417. ch = next(ch);
  418. }
  419. if ((*ch) == '.')
  420. {
  421. str.push_back('.');
  422. while ((*(ch = next(ch))) && (*ch) >= '0' && (*ch) <= '9')
  423. {
  424. str.push_back(*ch);
  425. }
  426. }
  427. if ((*ch) == 'e' || (*ch) == 'E')
  428. {
  429. str.push_back(*ch);
  430. ch = next(ch);
  431. if ((*ch) == '-' || (*ch) == '+')
  432. {
  433. str.push_back(*ch);
  434. ch = next(ch);
  435. }
  436. while ((*ch) >= '0' && (*ch) <= '9')
  437. {
  438. str.push_back(*ch);
  439. ch = next(ch);
  440. }
  441. }
  442. // Ensure null terminated
  443. str.push_back('\0');
  444. return string::parse_double(str.begin());
  445. }
  446. //-----------------------------------------------------------------------------
  447. bool JSONParser::parse_bool(const char* s)
  448. {
  449. CE_ASSERT_NOT_NULL(s);
  450. const char* ch = s;
  451. switch(*ch)
  452. {
  453. case 't':
  454. {
  455. ch = next(ch, 't');
  456. ch = next(ch, 'r');
  457. ch = next(ch, 'u');
  458. ch = next(ch, 'e');
  459. return true;
  460. }
  461. case 'f':
  462. {
  463. ch = next(ch, 'f');
  464. ch = next(ch, 'a');
  465. ch = next(ch, 'l');
  466. ch = next(ch, 's');
  467. ch = next(ch, 'e');
  468. return false;
  469. }
  470. default: break;
  471. }
  472. CE_ASSERT(false, "Bad boolean");
  473. }
  474. //-----------------------------------------------------------------------------
  475. int32_t JSONParser::parse_int(const char* s)
  476. {
  477. CE_ASSERT_NOT_NULL(s);
  478. return (int32_t) parse_number(s);
  479. }
  480. //-----------------------------------------------------------------------------
  481. float JSONParser::parse_float(const char* s)
  482. {
  483. CE_ASSERT_NOT_NULL(s);
  484. return (float) parse_number(s);
  485. }
  486. //-----------------------------------------------------------------------------
  487. void JSONParser::parse_array(const char* s, List<const char*>& array)
  488. {
  489. CE_ASSERT_NOT_NULL(s);
  490. const char* ch = s;
  491. if ((*ch) == '[')
  492. {
  493. ch = next(ch, '[');
  494. // Skip whitespaces
  495. while ((*ch) && (*ch) <= ' ')
  496. {
  497. ch = next(ch);
  498. }
  499. if ((*ch) == ']')
  500. {
  501. ch = next(ch, ']');
  502. return;
  503. }
  504. while (*ch)
  505. {
  506. array.push_back(ch);
  507. ch = skip_array(ch);
  508. ch = skip_object(ch);
  509. ch = skip_number(ch);
  510. ch = skip_string(ch);
  511. ch = skip_bool(ch);
  512. ch = skip_whites(ch);
  513. // Closing bracket (top-most array)
  514. if ((*ch) == ']')
  515. {
  516. ch = next(ch, ']');
  517. return;
  518. }
  519. // Skip until next ','
  520. ch = next(ch, ',');
  521. // Skip whites, eventually
  522. ch = skip_whites(ch);
  523. }
  524. }
  525. CE_ASSERT(false, "Bad array");
  526. }
  527. //-----------------------------------------------------------------------------
  528. void JSONParser::parse_object(const char* s, List<JSONPair>& object)
  529. {
  530. CE_ASSERT_NOT_NULL(s);
  531. const char* ch = s;
  532. if ((*ch) == '{')
  533. {
  534. ch = next(ch, '{');
  535. ch = skip_whites(ch);
  536. if ((*ch) == '}')
  537. {
  538. next(ch, '}');
  539. return;
  540. }
  541. while (*ch)
  542. {
  543. JSONPair pair;
  544. pair.key = ch;
  545. // Skip any value
  546. ch = skip_array(ch);
  547. ch = skip_object(ch);
  548. ch = skip_number(ch);
  549. ch = skip_string(ch);
  550. ch = skip_bool(ch);
  551. ch = skip_whites(ch);
  552. ch = next(ch, ':');
  553. ch = skip_whites(ch);
  554. pair.val = ch;
  555. object.push_back(pair);
  556. // Skip any value
  557. ch = skip_array(ch);
  558. ch = skip_object(ch);
  559. ch = skip_number(ch);
  560. ch = skip_string(ch);
  561. ch = skip_bool(ch);
  562. ch = skip_whites(ch);
  563. if ((*ch) == '}')
  564. {
  565. next(ch, '}');
  566. return;
  567. }
  568. ch = next(ch, ',');
  569. ch = skip_whites(ch);
  570. }
  571. }
  572. CE_ASSERT(false, "Bad object");
  573. }
  574. } //namespace crown