2
0

json_parser.cpp 12 KB

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