json_parser.cpp 8.5 KB

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