JSONParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "JSON.h"
  25. #include "TempAllocator.h"
  26. #include "StringUtils.h"
  27. #include "Log.h"
  28. namespace crown
  29. {
  30. //--------------------------------------------------------------------------
  31. JSONElement::JSONElement()
  32. : m_at(NULL)
  33. {
  34. }
  35. //--------------------------------------------------------------------------
  36. JSONElement::JSONElement(const char* at)
  37. : m_at(at)
  38. {
  39. }
  40. //--------------------------------------------------------------------------
  41. JSONElement::JSONElement(const JSONElement& other)
  42. : m_at(other.m_at)
  43. {
  44. }
  45. //--------------------------------------------------------------------------
  46. JSONElement& JSONElement::operator=(const JSONElement& other)
  47. {
  48. // Our begin is the other's at
  49. m_at = other.m_at;
  50. return *this;
  51. }
  52. //--------------------------------------------------------------------------
  53. JSONElement JSONElement::operator[](uint32_t i)
  54. {
  55. Array<const char*> array(default_allocator());
  56. json::parse_array(m_at, array);
  57. CE_ASSERT(i < array::size(array), "Index out of bounds");
  58. return JSONElement(array[i]);
  59. }
  60. //--------------------------------------------------------------------------
  61. JSONElement JSONElement::index(uint32_t i)
  62. {
  63. return this->operator[](i);
  64. }
  65. //--------------------------------------------------------------------------
  66. JSONElement JSONElement::index_or_nil(uint32_t i)
  67. {
  68. if (m_at != NULL)
  69. {
  70. Array<const char*> array(default_allocator());
  71. json::parse_array(m_at, array);
  72. if (i >= array::size(array))
  73. {
  74. return JSONElement();
  75. }
  76. return JSONElement(array[i]);
  77. }
  78. return JSONElement();
  79. }
  80. //--------------------------------------------------------------------------
  81. JSONElement JSONElement::key(const char* k)
  82. {
  83. Array<JSONPair> object(default_allocator());
  84. json::parse_object(m_at, object);
  85. bool found = false;
  86. const char* tmp_at = m_at;
  87. for (uint32_t i = 0; i < array::size(object); i++)
  88. {
  89. DynamicString key;
  90. json::parse_string(object[i].key, key);
  91. if (key == k)
  92. {
  93. tmp_at = object[i].val;
  94. found = true;
  95. }
  96. }
  97. CE_ASSERT(found, "Key not found: '%s'", k);
  98. return JSONElement(tmp_at);
  99. }
  100. //--------------------------------------------------------------------------
  101. JSONElement JSONElement::key_or_nil(const char* k)
  102. {
  103. if (m_at != NULL)
  104. {
  105. Array<JSONPair> object(default_allocator());
  106. json::parse_object(m_at, object);
  107. bool found = false;
  108. const char* tmp_at = m_at;
  109. for (uint32_t i = 0; i < array::size(object); i++)
  110. {
  111. DynamicString key;
  112. json::parse_string(object[i].key, key);
  113. if (key == k)
  114. {
  115. tmp_at = object[i].val;
  116. found = true;
  117. }
  118. }
  119. if (!found)
  120. {
  121. return JSONElement();
  122. }
  123. return JSONElement(tmp_at);
  124. }
  125. return JSONElement();
  126. }
  127. //--------------------------------------------------------------------------
  128. bool JSONElement::has_key(const char* k) const
  129. {
  130. Array<JSONPair> object(default_allocator());
  131. json::parse_object(m_at, object);
  132. for (uint32_t i = 0; i < array::size(object); i++)
  133. {
  134. DynamicString key;
  135. json::parse_string(object[i].key, key);
  136. if (key == k)
  137. {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. //--------------------------------------------------------------------------
  144. bool JSONElement::is_key_unique(const char* k) const
  145. {
  146. Array<JSONPair> object(default_allocator());
  147. json::parse_object(m_at, object);
  148. bool found = false;
  149. for (uint32_t i = 0; i < array::size(object); i++)
  150. {
  151. DynamicString key;
  152. json::parse_string(object[i].key, key);
  153. if (key == k)
  154. {
  155. if (found == true)
  156. {
  157. return false;
  158. }
  159. found = true;
  160. }
  161. }
  162. return found;
  163. }
  164. //--------------------------------------------------------------------------
  165. bool JSONElement::to_bool() const
  166. {
  167. const bool value = json::parse_bool(m_at);
  168. return value;
  169. }
  170. //--------------------------------------------------------------------------
  171. int32_t JSONElement::to_int() const
  172. {
  173. const int32_t value = json::parse_int(m_at);
  174. return value;
  175. }
  176. //--------------------------------------------------------------------------
  177. float JSONElement::to_float() const
  178. {
  179. const float value = json::parse_float(m_at);
  180. return value;
  181. }
  182. //--------------------------------------------------------------------------
  183. void JSONElement::to_string(DynamicString& str) const
  184. {
  185. json::parse_string(m_at, str);
  186. }
  187. //--------------------------------------------------------------------------
  188. StringId32 JSONElement::to_string_id() const
  189. {
  190. TempAllocator1024 alloc;
  191. DynamicString str(alloc);
  192. json::parse_string(m_at, str);
  193. return str.to_string_id();
  194. }
  195. //--------------------------------------------------------------------------
  196. void JSONElement::to_array(Array<bool>& array) const
  197. {
  198. Array<const char*> temp(default_allocator());
  199. json::parse_array(m_at, temp);
  200. for (uint32_t i = 0; i < array::size(temp); i++)
  201. {
  202. array::push_back(array, json::parse_bool(temp[i]));
  203. }
  204. }
  205. //--------------------------------------------------------------------------
  206. void JSONElement::to_array(Array<int16_t>& array) const
  207. {
  208. Array<const char*> temp(default_allocator());
  209. json::parse_array(m_at, temp);
  210. for (uint32_t i = 0; i < array::size(temp); i++)
  211. {
  212. array::push_back(array, (int16_t)json::parse_int(temp[i]));
  213. }
  214. }
  215. //--------------------------------------------------------------------------
  216. void JSONElement::to_array(Array<uint16_t>& array) const
  217. {
  218. Array<const char*> temp(default_allocator());
  219. json::parse_array(m_at, temp);
  220. for (uint32_t i = 0; i < array::size(temp); i++)
  221. {
  222. array::push_back(array, (uint16_t)json::parse_int(temp[i]));
  223. }
  224. }
  225. //--------------------------------------------------------------------------
  226. void JSONElement::to_array(Array<int32_t>& array) const
  227. {
  228. Array<const char*> temp(default_allocator());
  229. json::parse_array(m_at, temp);
  230. for (uint32_t i = 0; i < array::size(temp); i++)
  231. {
  232. array::push_back(array, (int32_t)json::parse_int(temp[i]));
  233. }
  234. }
  235. //--------------------------------------------------------------------------
  236. void JSONElement::to_array(Array<uint32_t>& array) const
  237. {
  238. Array<const char*> temp(default_allocator());
  239. json::parse_array(m_at, temp);
  240. for (uint32_t i = 0; i < array::size(temp); i++)
  241. {
  242. array::push_back(array, (uint32_t)json::parse_int(temp[i]));
  243. }
  244. }
  245. //--------------------------------------------------------------------------
  246. void JSONElement::to_array(Array<float>& array) const
  247. {
  248. Array<const char*> temp(default_allocator());
  249. json::parse_array(m_at, temp);
  250. for (uint32_t i = 0; i < array::size(temp); i++)
  251. {
  252. array::push_back(array, json::parse_float(temp[i]));
  253. }
  254. }
  255. //--------------------------------------------------------------------------
  256. void JSONElement::to_array(Vector<DynamicString>& array) const
  257. {
  258. Array<const char*> temp(default_allocator());
  259. json::parse_array(m_at, temp);
  260. for (uint32_t i = 0; i < array::size(temp); i++)
  261. {
  262. DynamicString str;
  263. json::parse_string(temp[i], str);
  264. vector::push_back(array, str);
  265. }
  266. }
  267. //--------------------------------------------------------------------------
  268. void JSONElement::to_keys(Vector<DynamicString>& keys) const
  269. {
  270. Array<JSONPair> object(default_allocator());
  271. json::parse_object(m_at, object);
  272. for (uint32_t i = 0; i < array::size(object); i++)
  273. {
  274. DynamicString key;
  275. json::parse_string(object[i].key, key);
  276. vector::push_back(keys, key);
  277. }
  278. }
  279. //--------------------------------------------------------------------------
  280. bool JSONElement::is_nil() const
  281. {
  282. if (m_at != NULL)
  283. {
  284. return json::type(m_at) == JSONType::NIL;
  285. }
  286. return true;
  287. }
  288. //--------------------------------------------------------------------------
  289. bool JSONElement::is_bool() const
  290. {
  291. if (m_at != NULL)
  292. {
  293. return json::type(m_at) == JSONType::BOOL;
  294. }
  295. return false;
  296. }
  297. //--------------------------------------------------------------------------
  298. bool JSONElement::is_number() const
  299. {
  300. if (m_at != NULL)
  301. {
  302. return json::type(m_at) == JSONType::NUMBER;
  303. }
  304. return false;
  305. }
  306. //--------------------------------------------------------------------------
  307. bool JSONElement::is_string() const
  308. {
  309. if (m_at != NULL)
  310. {
  311. return json::type(m_at) == JSONType::STRING;
  312. }
  313. return false;
  314. }
  315. //--------------------------------------------------------------------------
  316. bool JSONElement::is_array() const
  317. {
  318. if (m_at != NULL)
  319. {
  320. return json::type(m_at) == JSONType::ARRAY;
  321. }
  322. return false;
  323. }
  324. //--------------------------------------------------------------------------
  325. bool JSONElement::is_object() const
  326. {
  327. if (m_at != NULL)
  328. {
  329. return json::type(m_at) == JSONType::OBJECT;
  330. }
  331. return false;
  332. }
  333. //--------------------------------------------------------------------------
  334. uint32_t JSONElement::size() const
  335. {
  336. if (m_at == NULL)
  337. {
  338. return 0;
  339. }
  340. switch(json::type(m_at))
  341. {
  342. case JSONType::NIL:
  343. {
  344. return 1;
  345. }
  346. case JSONType::OBJECT:
  347. {
  348. Array<JSONPair> object(default_allocator());
  349. json::parse_object(m_at, object);
  350. return array::size(object);
  351. }
  352. case JSONType::ARRAY:
  353. {
  354. Array<const char*> array(default_allocator());
  355. json::parse_array(m_at, array);
  356. return array::size(array);
  357. }
  358. case JSONType::STRING:
  359. {
  360. DynamicString string;
  361. json::parse_string(m_at, string);
  362. return string.length();
  363. }
  364. case JSONType::NUMBER:
  365. {
  366. return 1;
  367. }
  368. case JSONType::BOOL:
  369. {
  370. return 1;
  371. }
  372. default:
  373. {
  374. CE_FATAL("Oops, unknown value type");
  375. return 0;
  376. }
  377. }
  378. }
  379. //--------------------------------------------------------------------------
  380. JSONParser::JSONParser(const char* s)
  381. : m_document(s)
  382. {
  383. CE_ASSERT_NOT_NULL(s);
  384. }
  385. //--------------------------------------------------------------------------
  386. JSONElement JSONParser::root()
  387. {
  388. const char* ch = m_document;
  389. while ((*ch) && (*ch) <= ' ') ch++;
  390. return JSONElement(ch);
  391. }
  392. } //namespace crown