JSONParser.cpp 11 KB

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