JSONParser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. return json::parse_bool(m_at);
  116. }
  117. //--------------------------------------------------------------------------
  118. int32_t JSONElement::to_int() const
  119. {
  120. return json::parse_int(m_at);
  121. }
  122. //--------------------------------------------------------------------------
  123. float JSONElement::to_float() const
  124. {
  125. return json::parse_float(m_at);
  126. }
  127. //--------------------------------------------------------------------------
  128. void JSONElement::to_string(DynamicString& str) const
  129. {
  130. json::parse_string(m_at, str);
  131. }
  132. //--------------------------------------------------------------------------
  133. Vector3 JSONElement::to_vector3() const
  134. {
  135. TempAllocator64 alloc;
  136. Array<const char*> array(alloc);
  137. json::parse_array(m_at, array);
  138. return Vector3(json::parse_float(array[0]),
  139. json::parse_float(array[1]),
  140. json::parse_float(array[2]));
  141. }
  142. //--------------------------------------------------------------------------
  143. Quaternion JSONElement::to_quaternion() const
  144. {
  145. TempAllocator64 alloc;
  146. Array<const char*> array(alloc);
  147. json::parse_array(m_at, array);
  148. return Quaternion(json::parse_float(array[0]),
  149. json::parse_float(array[1]),
  150. json::parse_float(array[2]),
  151. json::parse_float(array[3]));
  152. }
  153. //--------------------------------------------------------------------------
  154. StringId32 JSONElement::to_string_id() const
  155. {
  156. TempAllocator1024 alloc;
  157. DynamicString str(alloc);
  158. json::parse_string(m_at, str);
  159. return str.to_string_id();
  160. }
  161. //--------------------------------------------------------------------------
  162. void JSONElement::to_array(Array<bool>& array) const
  163. {
  164. Array<const char*> temp(default_allocator());
  165. json::parse_array(m_at, temp);
  166. for (uint32_t i = 0; i < array::size(temp); i++)
  167. {
  168. array::push_back(array, json::parse_bool(temp[i]));
  169. }
  170. }
  171. //--------------------------------------------------------------------------
  172. void JSONElement::to_array(Array<int16_t>& array) const
  173. {
  174. Array<const char*> temp(default_allocator());
  175. json::parse_array(m_at, temp);
  176. for (uint32_t i = 0; i < array::size(temp); i++)
  177. {
  178. array::push_back(array, (int16_t)json::parse_int(temp[i]));
  179. }
  180. }
  181. //--------------------------------------------------------------------------
  182. void JSONElement::to_array(Array<uint16_t>& array) const
  183. {
  184. Array<const char*> temp(default_allocator());
  185. json::parse_array(m_at, temp);
  186. for (uint32_t i = 0; i < array::size(temp); i++)
  187. {
  188. array::push_back(array, (uint16_t)json::parse_int(temp[i]));
  189. }
  190. }
  191. //--------------------------------------------------------------------------
  192. void JSONElement::to_array(Array<int32_t>& array) const
  193. {
  194. Array<const char*> temp(default_allocator());
  195. json::parse_array(m_at, temp);
  196. for (uint32_t i = 0; i < array::size(temp); i++)
  197. {
  198. array::push_back(array, (int32_t)json::parse_int(temp[i]));
  199. }
  200. }
  201. //--------------------------------------------------------------------------
  202. void JSONElement::to_array(Array<uint32_t>& array) const
  203. {
  204. Array<const char*> temp(default_allocator());
  205. json::parse_array(m_at, temp);
  206. for (uint32_t i = 0; i < array::size(temp); i++)
  207. {
  208. array::push_back(array, (uint32_t)json::parse_int(temp[i]));
  209. }
  210. }
  211. //--------------------------------------------------------------------------
  212. void JSONElement::to_array(Array<float>& array) const
  213. {
  214. Array<const char*> temp(default_allocator());
  215. json::parse_array(m_at, temp);
  216. for (uint32_t i = 0; i < array::size(temp); i++)
  217. {
  218. array::push_back(array, json::parse_float(temp[i]));
  219. }
  220. }
  221. //--------------------------------------------------------------------------
  222. void JSONElement::to_array(Vector<DynamicString>& array) const
  223. {
  224. Array<const char*> temp(default_allocator());
  225. json::parse_array(m_at, temp);
  226. for (uint32_t i = 0; i < array::size(temp); i++)
  227. {
  228. DynamicString str;
  229. json::parse_string(temp[i], str);
  230. vector::push_back(array, str);
  231. }
  232. }
  233. //--------------------------------------------------------------------------
  234. void JSONElement::to_keys(Vector<DynamicString>& keys) const
  235. {
  236. Map<DynamicString, const char*> object(default_allocator());
  237. json::parse_object(m_at, object);
  238. const Map<DynamicString, const char*>::Node* it = map::begin(object);
  239. while (it != map::end(object))
  240. {
  241. vector::push_back(keys, (*it).key);
  242. it++;
  243. }
  244. }
  245. //--------------------------------------------------------------------------
  246. bool JSONElement::is_nil() const
  247. {
  248. if (m_at != NULL)
  249. {
  250. return json::type(m_at) == JSONType::NIL;
  251. }
  252. return true;
  253. }
  254. //--------------------------------------------------------------------------
  255. bool JSONElement::is_bool() const
  256. {
  257. if (m_at != NULL)
  258. {
  259. return json::type(m_at) == JSONType::BOOL;
  260. }
  261. return false;
  262. }
  263. //--------------------------------------------------------------------------
  264. bool JSONElement::is_number() const
  265. {
  266. if (m_at != NULL)
  267. {
  268. return json::type(m_at) == JSONType::NUMBER;
  269. }
  270. return false;
  271. }
  272. //--------------------------------------------------------------------------
  273. bool JSONElement::is_string() const
  274. {
  275. if (m_at != NULL)
  276. {
  277. return json::type(m_at) == JSONType::STRING;
  278. }
  279. return false;
  280. }
  281. //--------------------------------------------------------------------------
  282. bool JSONElement::is_array() const
  283. {
  284. if (m_at != NULL)
  285. {
  286. return json::type(m_at) == JSONType::ARRAY;
  287. }
  288. return false;
  289. }
  290. //--------------------------------------------------------------------------
  291. bool JSONElement::is_object() const
  292. {
  293. if (m_at != NULL)
  294. {
  295. return json::type(m_at) == JSONType::OBJECT;
  296. }
  297. return false;
  298. }
  299. //--------------------------------------------------------------------------
  300. uint32_t JSONElement::size() const
  301. {
  302. if (m_at == NULL)
  303. {
  304. return 0;
  305. }
  306. switch(json::type(m_at))
  307. {
  308. case JSONType::NIL:
  309. {
  310. return 1;
  311. }
  312. case JSONType::OBJECT:
  313. {
  314. Map<DynamicString, const char*> object(default_allocator());
  315. json::parse_object(m_at, object);
  316. return map::size(object);
  317. }
  318. case JSONType::ARRAY:
  319. {
  320. Array<const char*> array(default_allocator());
  321. json::parse_array(m_at, array);
  322. return array::size(array);
  323. }
  324. case JSONType::STRING:
  325. {
  326. DynamicString string;
  327. json::parse_string(m_at, string);
  328. return string.length();
  329. }
  330. case JSONType::NUMBER:
  331. {
  332. return 1;
  333. }
  334. case JSONType::BOOL:
  335. {
  336. return 1;
  337. }
  338. default:
  339. {
  340. CE_FATAL("Oops, unknown value type");
  341. return 0;
  342. }
  343. }
  344. }
  345. //--------------------------------------------------------------------------
  346. JSONParser::JSONParser(const char* s)
  347. : m_document(s)
  348. {
  349. CE_ASSERT_NOT_NULL(s);
  350. }
  351. //--------------------------------------------------------------------------
  352. JSONElement JSONParser::root()
  353. {
  354. const char* ch = m_document;
  355. while ((*ch) && (*ch) <= ' ') ch++;
  356. return JSONElement(ch);
  357. }
  358. } //namespace crown