JSONParser.cpp 10 KB

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