json_parser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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(bool def) const
  118. {
  119. return is_nil() ? def : json::parse_bool(m_at);
  120. }
  121. //--------------------------------------------------------------------------
  122. int32_t JSONElement::to_int(int32_t def) const
  123. {
  124. return is_nil() ? def : json::parse_int(m_at);
  125. }
  126. //--------------------------------------------------------------------------
  127. float JSONElement::to_float(float def) const
  128. {
  129. return is_nil() ? def : json::parse_float(m_at);
  130. }
  131. //--------------------------------------------------------------------------
  132. void JSONElement::to_string(DynamicString& str, const char* def) const
  133. {
  134. if (is_nil())
  135. str = def;
  136. else
  137. json::parse_string(m_at, str);
  138. }
  139. //--------------------------------------------------------------------------
  140. Vector2 JSONElement::to_vector2(const Vector2& def) const
  141. {
  142. if (is_nil())
  143. return def;
  144. TempAllocator64 alloc;
  145. Array<const char*> array(alloc);
  146. json::parse_array(m_at, array);
  147. return Vector2(json::parse_float(array[0]),
  148. json::parse_float(array[1]));
  149. }
  150. //--------------------------------------------------------------------------
  151. Vector3 JSONElement::to_vector3(const Vector3& def) const
  152. {
  153. if (is_nil())
  154. return def;
  155. TempAllocator64 alloc;
  156. Array<const char*> array(alloc);
  157. json::parse_array(m_at, array);
  158. return Vector3(json::parse_float(array[0]),
  159. json::parse_float(array[1]),
  160. json::parse_float(array[2]));
  161. }
  162. //--------------------------------------------------------------------------
  163. Vector4 JSONElement::to_vector4(const Vector4& def) const
  164. {
  165. if (is_nil())
  166. return def;
  167. TempAllocator64 alloc;
  168. Array<const char*> array(alloc);
  169. json::parse_array(m_at, array);
  170. return Vector4(json::parse_float(array[0]),
  171. json::parse_float(array[1]),
  172. json::parse_float(array[2]),
  173. json::parse_float(array[3]));
  174. }
  175. //--------------------------------------------------------------------------
  176. Quaternion JSONElement::to_quaternion(const Quaternion& def) const
  177. {
  178. if (is_nil())
  179. return def;
  180. TempAllocator64 alloc;
  181. Array<const char*> array(alloc);
  182. json::parse_array(m_at, array);
  183. return Quaternion(json::parse_float(array[0]),
  184. json::parse_float(array[1]),
  185. json::parse_float(array[2]),
  186. json::parse_float(array[3]));
  187. }
  188. //--------------------------------------------------------------------------
  189. Matrix4x4 JSONElement::to_matrix4x4(const Matrix4x4& def) const
  190. {
  191. if (is_nil())
  192. return def;
  193. TempAllocator128 alloc;
  194. Array<float> array(alloc);
  195. to_array(array);
  196. return Matrix4x4(array::begin(array));
  197. }
  198. //--------------------------------------------------------------------------
  199. StringId32 JSONElement::to_string_id(const StringId32 def) const
  200. {
  201. if (is_nil())
  202. return def;
  203. TempAllocator1024 alloc;
  204. DynamicString str(alloc);
  205. json::parse_string(m_at, str);
  206. return str.to_string_id();
  207. }
  208. //--------------------------------------------------------------------------
  209. ResourceId JSONElement::to_resource_id(const char* type) const
  210. {
  211. CE_ASSERT_NOT_NULL(type);
  212. // TempAllocator1024 alloc;
  213. DynamicString str(default_allocator());
  214. json::parse_string(m_at, str);
  215. return ResourceId(type, str.c_str());
  216. }
  217. //--------------------------------------------------------------------------
  218. void JSONElement::to_array(Array<bool>& array) const
  219. {
  220. Array<const char*> temp(default_allocator());
  221. json::parse_array(m_at, temp);
  222. for (uint32_t i = 0; i < array::size(temp); i++)
  223. {
  224. array::push_back(array, json::parse_bool(temp[i]));
  225. }
  226. }
  227. //--------------------------------------------------------------------------
  228. void JSONElement::to_array(Array<int16_t>& array) const
  229. {
  230. Array<const char*> temp(default_allocator());
  231. json::parse_array(m_at, temp);
  232. for (uint32_t i = 0; i < array::size(temp); i++)
  233. {
  234. array::push_back(array, (int16_t)json::parse_int(temp[i]));
  235. }
  236. }
  237. //--------------------------------------------------------------------------
  238. void JSONElement::to_array(Array<uint16_t>& array) const
  239. {
  240. Array<const char*> temp(default_allocator());
  241. json::parse_array(m_at, temp);
  242. for (uint32_t i = 0; i < array::size(temp); i++)
  243. {
  244. array::push_back(array, (uint16_t)json::parse_int(temp[i]));
  245. }
  246. }
  247. //--------------------------------------------------------------------------
  248. void JSONElement::to_array(Array<int32_t>& array) const
  249. {
  250. Array<const char*> temp(default_allocator());
  251. json::parse_array(m_at, temp);
  252. for (uint32_t i = 0; i < array::size(temp); i++)
  253. {
  254. array::push_back(array, (int32_t)json::parse_int(temp[i]));
  255. }
  256. }
  257. //--------------------------------------------------------------------------
  258. void JSONElement::to_array(Array<uint32_t>& array) const
  259. {
  260. Array<const char*> temp(default_allocator());
  261. json::parse_array(m_at, temp);
  262. for (uint32_t i = 0; i < array::size(temp); i++)
  263. {
  264. array::push_back(array, (uint32_t)json::parse_int(temp[i]));
  265. }
  266. }
  267. //--------------------------------------------------------------------------
  268. void JSONElement::to_array(Array<float>& array) const
  269. {
  270. Array<const char*> temp(default_allocator());
  271. json::parse_array(m_at, temp);
  272. for (uint32_t i = 0; i < array::size(temp); i++)
  273. {
  274. array::push_back(array, json::parse_float(temp[i]));
  275. }
  276. }
  277. //--------------------------------------------------------------------------
  278. void JSONElement::to_array(Vector<DynamicString>& array) const
  279. {
  280. Array<const char*> temp(default_allocator());
  281. json::parse_array(m_at, temp);
  282. for (uint32_t i = 0; i < array::size(temp); i++)
  283. {
  284. DynamicString str;
  285. json::parse_string(temp[i], str);
  286. vector::push_back(array, str);
  287. }
  288. }
  289. //--------------------------------------------------------------------------
  290. void JSONElement::to_keys(Vector<DynamicString>& keys) const
  291. {
  292. Map<DynamicString, const char*> object(default_allocator());
  293. json::parse_object(m_at, object);
  294. const Map<DynamicString, const char*>::Node* it = map::begin(object);
  295. while (it != map::end(object))
  296. {
  297. vector::push_back(keys, (*it).key);
  298. it++;
  299. }
  300. }
  301. //--------------------------------------------------------------------------
  302. bool JSONElement::is_nil() const
  303. {
  304. if (m_at != NULL)
  305. {
  306. return json::type(m_at) == JSONType::NIL;
  307. }
  308. return true;
  309. }
  310. //--------------------------------------------------------------------------
  311. bool JSONElement::is_bool() const
  312. {
  313. if (m_at != NULL)
  314. {
  315. return json::type(m_at) == JSONType::BOOL;
  316. }
  317. return false;
  318. }
  319. //--------------------------------------------------------------------------
  320. bool JSONElement::is_number() const
  321. {
  322. if (m_at != NULL)
  323. {
  324. return json::type(m_at) == JSONType::NUMBER;
  325. }
  326. return false;
  327. }
  328. //--------------------------------------------------------------------------
  329. bool JSONElement::is_string() const
  330. {
  331. if (m_at != NULL)
  332. {
  333. return json::type(m_at) == JSONType::STRING;
  334. }
  335. return false;
  336. }
  337. //--------------------------------------------------------------------------
  338. bool JSONElement::is_array() const
  339. {
  340. if (m_at != NULL)
  341. {
  342. return json::type(m_at) == JSONType::ARRAY;
  343. }
  344. return false;
  345. }
  346. //--------------------------------------------------------------------------
  347. bool JSONElement::is_object() const
  348. {
  349. if (m_at != NULL)
  350. {
  351. return json::type(m_at) == JSONType::OBJECT;
  352. }
  353. return false;
  354. }
  355. //--------------------------------------------------------------------------
  356. uint32_t JSONElement::size() const
  357. {
  358. if (m_at == NULL)
  359. {
  360. return 0;
  361. }
  362. switch(json::type(m_at))
  363. {
  364. case JSONType::NIL:
  365. {
  366. return 1;
  367. }
  368. case JSONType::OBJECT:
  369. {
  370. Map<DynamicString, const char*> object(default_allocator());
  371. json::parse_object(m_at, object);
  372. return map::size(object);
  373. }
  374. case JSONType::ARRAY:
  375. {
  376. Array<const char*> array(default_allocator());
  377. json::parse_array(m_at, array);
  378. return array::size(array);
  379. }
  380. case JSONType::STRING:
  381. {
  382. DynamicString string;
  383. json::parse_string(m_at, string);
  384. return string.length();
  385. }
  386. case JSONType::NUMBER:
  387. {
  388. return 1;
  389. }
  390. case JSONType::BOOL:
  391. {
  392. return 1;
  393. }
  394. default:
  395. {
  396. CE_FATAL("Oops, unknown value type");
  397. return 0;
  398. }
  399. }
  400. }
  401. //--------------------------------------------------------------------------
  402. JSONParser::JSONParser(const char* s)
  403. : m_file(false)
  404. , m_document(s)
  405. {
  406. CE_ASSERT_NOT_NULL(s);
  407. }
  408. //--------------------------------------------------------------------------
  409. JSONParser::JSONParser(File& f)
  410. : m_file(true)
  411. , m_document(NULL)
  412. {
  413. const size_t size = f.size();
  414. char* doc = (char*) default_allocator().allocate(size);
  415. f.read(doc, size);
  416. m_document = doc;
  417. }
  418. //--------------------------------------------------------------------------
  419. JSONParser::~JSONParser()
  420. {
  421. if (m_file)
  422. {
  423. default_allocator().deallocate((void*) m_document);
  424. }
  425. }
  426. //--------------------------------------------------------------------------
  427. JSONElement JSONParser::root()
  428. {
  429. const char* ch = m_document;
  430. while ((*ch) && (*ch) <= ' ') ch++;
  431. return JSONElement(ch);
  432. }
  433. } //namespace crown