JSONParser.cpp 12 KB

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