JSONParser.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. List<const char*> array(default_allocator());
  58. json::parse_array(m_at, array);
  59. CE_ASSERT(i < array.size(), "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. List<const char*> array(default_allocator());
  73. json::parse_array(m_at, array);
  74. if (i >= array.size())
  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. List<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 < object.size(); 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. List<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 < object.size(); 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. List<JSONPair> object(default_allocator());
  133. json::parse_object(m_at, object);
  134. for (uint32_t i = 0; i < object.size(); 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. List<JSONPair> object(default_allocator());
  149. json::parse_object(m_at, object);
  150. bool found = false;
  151. for (uint32_t i = 0; i < object.size(); 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::bool_value() const
  168. {
  169. const bool value = json::parse_bool(m_at);
  170. return value;
  171. }
  172. //--------------------------------------------------------------------------
  173. int32_t JSONElement::int_value() const
  174. {
  175. const int32_t value = json::parse_int(m_at);
  176. return value;
  177. }
  178. //--------------------------------------------------------------------------
  179. float JSONElement::float_value() const
  180. {
  181. const float value = json::parse_float(m_at);
  182. return value;
  183. }
  184. //--------------------------------------------------------------------------
  185. void JSONElement::string_value(DynamicString& str) const
  186. {
  187. json::parse_string(m_at, str);
  188. }
  189. //--------------------------------------------------------------------------
  190. void JSONElement::array_value(List<bool>& array) const
  191. {
  192. List<const char*> temp(default_allocator());
  193. json::parse_array(m_at, temp);
  194. for (uint32_t i = 0; i < temp.size(); i++)
  195. {
  196. array.push_back(json::parse_bool(temp[i]));
  197. }
  198. }
  199. //--------------------------------------------------------------------------
  200. void JSONElement::array_value(List<int16_t>& array) const
  201. {
  202. List<const char*> temp(default_allocator());
  203. json::parse_array(m_at, temp);
  204. for (uint32_t i = 0; i < temp.size(); i++)
  205. {
  206. array.push_back((int16_t)json::parse_int(temp[i]));
  207. }
  208. }
  209. //--------------------------------------------------------------------------
  210. void JSONElement::array_value(List<uint16_t>& array) const
  211. {
  212. List<const char*> temp(default_allocator());
  213. json::parse_array(m_at, temp);
  214. for (uint32_t i = 0; i < temp.size(); i++)
  215. {
  216. array.push_back((uint16_t)json::parse_int(temp[i]));
  217. }
  218. }
  219. //--------------------------------------------------------------------------
  220. void JSONElement::array_value(List<int32_t>& array) const
  221. {
  222. List<const char*> temp(default_allocator());
  223. json::parse_array(m_at, temp);
  224. for (uint32_t i = 0; i < temp.size(); i++)
  225. {
  226. array.push_back((int32_t)json::parse_int(temp[i]));
  227. }
  228. }
  229. //--------------------------------------------------------------------------
  230. void JSONElement::array_value(List<uint32_t>& array) const
  231. {
  232. List<const char*> temp(default_allocator());
  233. json::parse_array(m_at, temp);
  234. for (uint32_t i = 0; i < temp.size(); i++)
  235. {
  236. array.push_back((uint32_t)json::parse_int(temp[i]));
  237. }
  238. }
  239. //--------------------------------------------------------------------------
  240. void JSONElement::array_value(List<float>& array) const
  241. {
  242. List<const char*> temp(default_allocator());
  243. json::parse_array(m_at, temp);
  244. for (uint32_t i = 0; i < temp.size(); i++)
  245. {
  246. array.push_back(json::parse_float(temp[i]));
  247. }
  248. }
  249. //--------------------------------------------------------------------------
  250. void JSONElement::array_value(Vector<DynamicString>& array) const
  251. {
  252. List<const char*> temp(default_allocator());
  253. json::parse_array(m_at, temp);
  254. for (uint32_t i = 0; i < temp.size(); i++)
  255. {
  256. DynamicString str;
  257. json::parse_string(temp[i], str);
  258. array.push_back(str);
  259. }
  260. }
  261. //--------------------------------------------------------------------------
  262. bool JSONElement::is_nil() const
  263. {
  264. if (m_at != NULL)
  265. {
  266. return json::type(m_at) == JSONType::NIL;
  267. }
  268. return true;
  269. }
  270. //--------------------------------------------------------------------------
  271. bool JSONElement::is_bool() const
  272. {
  273. if (m_at != NULL)
  274. {
  275. return json::type(m_at) == JSONType::BOOL;
  276. }
  277. return false;
  278. }
  279. //--------------------------------------------------------------------------
  280. bool JSONElement::is_number() const
  281. {
  282. if (m_at != NULL)
  283. {
  284. return json::type(m_at) == JSONType::NUMBER;
  285. }
  286. return false;
  287. }
  288. //--------------------------------------------------------------------------
  289. bool JSONElement::is_string() const
  290. {
  291. if (m_at != NULL)
  292. {
  293. return json::type(m_at) == JSONType::STRING;
  294. }
  295. return false;
  296. }
  297. //--------------------------------------------------------------------------
  298. bool JSONElement::is_array() const
  299. {
  300. if (m_at != NULL)
  301. {
  302. return json::type(m_at) == JSONType::ARRAY;
  303. }
  304. return false;
  305. }
  306. //--------------------------------------------------------------------------
  307. bool JSONElement::is_object() const
  308. {
  309. if (m_at != NULL)
  310. {
  311. return json::type(m_at) == JSONType::OBJECT;
  312. }
  313. return false;
  314. }
  315. //--------------------------------------------------------------------------
  316. uint32_t JSONElement::size() const
  317. {
  318. if (m_at == NULL)
  319. {
  320. return 0;
  321. }
  322. switch(json::type(m_at))
  323. {
  324. case JSONType::NIL:
  325. {
  326. return 1;
  327. }
  328. case JSONType::OBJECT:
  329. {
  330. List<JSONPair> object(default_allocator());
  331. json::parse_object(m_at, object);
  332. return object.size();
  333. }
  334. case JSONType::ARRAY:
  335. {
  336. List<const char*> array(default_allocator());
  337. json::parse_array(m_at, array);
  338. return array.size();
  339. }
  340. case JSONType::STRING:
  341. {
  342. DynamicString string;
  343. json::parse_string(m_at, string);
  344. return string.length();
  345. }
  346. case JSONType::NUMBER:
  347. {
  348. return 1;
  349. }
  350. case JSONType::BOOL:
  351. {
  352. return 1;
  353. }
  354. default:
  355. {
  356. CE_FATAL("Oops, unknown value type");
  357. return 0;
  358. }
  359. }
  360. }
  361. //--------------------------------------------------------------------------
  362. JSONParser::JSONParser(const char* s)
  363. : m_document(s)
  364. {
  365. CE_ASSERT_NOT_NULL(s);
  366. }
  367. //--------------------------------------------------------------------------
  368. JSONElement JSONParser::root()
  369. {
  370. const char* ch = m_document;
  371. while ((*ch) && (*ch) <= ' ') ch++;
  372. return JSONElement(ch);
  373. }
  374. } //namespace crown