json_parser.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "json_parser.h"
  6. #include "njson.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. njson::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. njson::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. njson::parse(_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. njson::parse(_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. njson::parse(_at, object);
  86. return map::has(object, DynamicString(k));
  87. }
  88. bool JSONElement::to_bool(bool def) const
  89. {
  90. return is_nil() ? def : njson::parse_bool(_at);
  91. }
  92. int32_t JSONElement::to_int(int32_t def) const
  93. {
  94. return is_nil() ? def : njson::parse_int(_at);
  95. }
  96. float JSONElement::to_float(float def) const
  97. {
  98. return is_nil() ? def : njson::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. njson::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. njson::parse_array(_at, array);
  114. return vector2(njson::parse_float(array[0]),
  115. njson::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. njson::parse_array(_at, array);
  124. return vector3(njson::parse_float(array[0]),
  125. njson::parse_float(array[1]),
  126. njson::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. njson::parse_array(_at, array);
  135. return vector4(njson::parse_float(array[0]),
  136. njson::parse_float(array[1]),
  137. njson::parse_float(array[2]),
  138. njson::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. njson::parse_array(_at, array);
  147. return quaternion(njson::parse_float(array[0]),
  148. njson::parse_float(array[1]),
  149. njson::parse_float(array[2]),
  150. njson::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. njson::parse_string(_at, str);
  168. return str.to_string_id();
  169. }
  170. ResourceId JSONElement::to_resource_id() const
  171. {
  172. DynamicString str(default_allocator());
  173. njson::parse_string(_at, str);
  174. return ResourceId(str.c_str());
  175. }
  176. void JSONElement::to_array(Array<bool>& array) const
  177. {
  178. Array<const char*> temp(default_allocator());
  179. njson::parse_array(_at, temp);
  180. for (uint32_t i = 0; i < array::size(temp); i++)
  181. {
  182. array::push_back(array, njson::parse_bool(temp[i]));
  183. }
  184. }
  185. void JSONElement::to_array(Array<int16_t>& array) const
  186. {
  187. Array<const char*> temp(default_allocator());
  188. njson::parse_array(_at, temp);
  189. for (uint32_t i = 0; i < array::size(temp); i++)
  190. {
  191. array::push_back(array, (int16_t)njson::parse_int(temp[i]));
  192. }
  193. }
  194. void JSONElement::to_array(Array<uint16_t>& array) const
  195. {
  196. Array<const char*> temp(default_allocator());
  197. njson::parse_array(_at, temp);
  198. for (uint32_t i = 0; i < array::size(temp); i++)
  199. {
  200. array::push_back(array, (uint16_t)njson::parse_int(temp[i]));
  201. }
  202. }
  203. void JSONElement::to_array(Array<int32_t>& array) const
  204. {
  205. Array<const char*> temp(default_allocator());
  206. njson::parse_array(_at, temp);
  207. for (uint32_t i = 0; i < array::size(temp); i++)
  208. {
  209. array::push_back(array, (int32_t)njson::parse_int(temp[i]));
  210. }
  211. }
  212. void JSONElement::to_array(Array<uint32_t>& array) const
  213. {
  214. Array<const char*> temp(default_allocator());
  215. njson::parse_array(_at, temp);
  216. for (uint32_t i = 0; i < array::size(temp); i++)
  217. {
  218. array::push_back(array, (uint32_t)njson::parse_int(temp[i]));
  219. }
  220. }
  221. void JSONElement::to_array(Array<float>& array) const
  222. {
  223. Array<const char*> temp(default_allocator());
  224. njson::parse_array(_at, temp);
  225. for (uint32_t i = 0; i < array::size(temp); i++)
  226. {
  227. array::push_back(array, njson::parse_float(temp[i]));
  228. }
  229. }
  230. void JSONElement::to_array(Vector<DynamicString>& array) const
  231. {
  232. Array<const char*> temp(default_allocator());
  233. njson::parse_array(_at, temp);
  234. for (uint32_t i = 0; i < array::size(temp); i++)
  235. {
  236. DynamicString str;
  237. njson::parse_string(temp[i], str);
  238. vector::push_back(array, str);
  239. }
  240. }
  241. void JSONElement::to_keys(Vector<DynamicString>& keys) const
  242. {
  243. Map<DynamicString, const char*> object(default_allocator());
  244. njson::parse(_at, object);
  245. const Map<DynamicString, const char*>::Node* it = map::begin(object);
  246. while (it != map::end(object))
  247. {
  248. vector::push_back(keys, (*it).key);
  249. it++;
  250. }
  251. }
  252. bool JSONElement::is_nil() const
  253. {
  254. if (_at != NULL)
  255. {
  256. return njson::type(_at) == NJSONValueType::NIL;
  257. }
  258. return true;
  259. }
  260. bool JSONElement::is_bool() const
  261. {
  262. if (_at != NULL)
  263. {
  264. return njson::type(_at) == NJSONValueType::BOOL;
  265. }
  266. return false;
  267. }
  268. bool JSONElement::is_number() const
  269. {
  270. if (_at != NULL)
  271. {
  272. return njson::type(_at) == NJSONValueType::NUMBER;
  273. }
  274. return false;
  275. }
  276. bool JSONElement::is_string() const
  277. {
  278. if (_at != NULL)
  279. {
  280. return njson::type(_at) == NJSONValueType::STRING;
  281. }
  282. return false;
  283. }
  284. bool JSONElement::is_array() const
  285. {
  286. if (_at != NULL)
  287. {
  288. return njson::type(_at) == NJSONValueType::ARRAY;
  289. }
  290. return false;
  291. }
  292. bool JSONElement::is_object() const
  293. {
  294. if (_at != NULL)
  295. {
  296. return njson::type(_at) == NJSONValueType::OBJECT;
  297. }
  298. return false;
  299. }
  300. uint32_t JSONElement::size() const
  301. {
  302. if (_at == NULL)
  303. {
  304. return 0;
  305. }
  306. switch(njson::type(_at))
  307. {
  308. case NJSONValueType::NIL:
  309. {
  310. return 1;
  311. }
  312. case NJSONValueType::OBJECT:
  313. {
  314. Map<DynamicString, const char*> object(default_allocator());
  315. njson::parse(_at, object);
  316. return map::size(object);
  317. }
  318. case NJSONValueType::ARRAY:
  319. {
  320. Array<const char*> array(default_allocator());
  321. njson::parse_array(_at, array);
  322. return array::size(array);
  323. }
  324. case NJSONValueType::STRING:
  325. {
  326. DynamicString string;
  327. njson::parse_string(_at, string);
  328. return string.length();
  329. }
  330. case NJSONValueType::NUMBER:
  331. {
  332. return 1;
  333. }
  334. case NJSONValueType::BOOL:
  335. {
  336. return 1;
  337. }
  338. default:
  339. {
  340. CE_FATAL("Oops, unknown value type");
  341. return 0;
  342. }
  343. }
  344. }
  345. JSONParser::JSONParser(const char* s)
  346. : _file(false)
  347. , _document(s)
  348. {
  349. CE_ASSERT_NOT_NULL(s);
  350. }
  351. JSONParser::JSONParser(File& f)
  352. : _file(true)
  353. , _document(NULL)
  354. {
  355. const size_t size = f.size();
  356. char* doc = (char*) default_allocator().allocate(size + 1);
  357. f.read(doc, size);
  358. doc[size] = '\0';
  359. _document = doc;
  360. }
  361. JSONParser::JSONParser(Buffer& b)
  362. : _file(false)
  363. , _document(NULL)
  364. {
  365. array::push_back(b, '\0');
  366. _document = array::begin(b);
  367. }
  368. JSONParser::~JSONParser()
  369. {
  370. if (_file)
  371. {
  372. default_allocator().deallocate((void*) _document);
  373. }
  374. }
  375. JSONElement JSONParser::root()
  376. {
  377. const char* ch = _document;
  378. while ((*ch) && (*ch) <= ' ') ch++;
  379. return JSONElement(ch);
  380. }
  381. } //namespace crown