json_parser.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "json_parser.h"
  6. #include "sjson.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. sjson::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. sjson::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. sjson::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. sjson::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. sjson::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 : sjson::parse_bool(_at);
  91. }
  92. int32_t JSONElement::to_int(int32_t def) const
  93. {
  94. return is_nil() ? def : sjson::parse_int(_at);
  95. }
  96. float JSONElement::to_float(float def) const
  97. {
  98. return is_nil() ? def : sjson::parse_float(_at);
  99. }
  100. DynamicString JSONElement::to_string(const char* def) const
  101. {
  102. DynamicString str(default_allocator());
  103. if (is_nil())
  104. str = def;
  105. else
  106. sjson::parse_string(_at, str);
  107. return str;
  108. }
  109. Vector2 JSONElement::to_vector2(const Vector2& def) const
  110. {
  111. if (is_nil())
  112. return def;
  113. TempAllocator64 alloc;
  114. Array<const char*> array(alloc);
  115. sjson::parse_array(_at, array);
  116. return vector2(sjson::parse_float(array[0]),
  117. sjson::parse_float(array[1]));
  118. }
  119. Vector3 JSONElement::to_vector3(const Vector3& def) const
  120. {
  121. if (is_nil())
  122. return def;
  123. TempAllocator64 alloc;
  124. Array<const char*> array(alloc);
  125. sjson::parse_array(_at, array);
  126. return vector3(sjson::parse_float(array[0]),
  127. sjson::parse_float(array[1]),
  128. sjson::parse_float(array[2]));
  129. }
  130. Vector4 JSONElement::to_vector4(const Vector4& def) const
  131. {
  132. if (is_nil())
  133. return def;
  134. TempAllocator64 alloc;
  135. Array<const char*> array(alloc);
  136. sjson::parse_array(_at, array);
  137. return vector4(sjson::parse_float(array[0]),
  138. sjson::parse_float(array[1]),
  139. sjson::parse_float(array[2]),
  140. sjson::parse_float(array[3]));
  141. }
  142. Quaternion JSONElement::to_quaternion(const Quaternion& def) const
  143. {
  144. if (is_nil())
  145. return def;
  146. TempAllocator64 alloc;
  147. Array<const char*> array(alloc);
  148. sjson::parse_array(_at, array);
  149. const Vector3 axis = vector3(sjson::parse_float(array[0])
  150. , sjson::parse_float(array[1])
  151. , sjson::parse_float(array[2])
  152. );
  153. const float angle = sjson::parse_float(array[3]);
  154. return quaternion(axis, angle);
  155. }
  156. Matrix4x4 JSONElement::to_matrix4x4(const Matrix4x4& def) const
  157. {
  158. if (is_nil())
  159. return def;
  160. TempAllocator128 alloc;
  161. Array<float> array(alloc);
  162. to_array(array);
  163. return matrix4x4(array::begin(array));
  164. }
  165. StringId32 JSONElement::to_string_id(const StringId32 def) const
  166. {
  167. if (is_nil())
  168. return def;
  169. TempAllocator1024 alloc;
  170. DynamicString str(alloc);
  171. sjson::parse_string(_at, str);
  172. return str.to_string_id();
  173. }
  174. ResourceId JSONElement::to_resource_id() const
  175. {
  176. DynamicString str(default_allocator());
  177. sjson::parse_string(_at, str);
  178. return ResourceId(str.c_str());
  179. }
  180. void JSONElement::to_array(Array<bool>& array) const
  181. {
  182. Array<const char*> temp(default_allocator());
  183. sjson::parse_array(_at, temp);
  184. for (uint32_t i = 0; i < array::size(temp); i++)
  185. {
  186. array::push_back(array, sjson::parse_bool(temp[i]));
  187. }
  188. }
  189. void JSONElement::to_array(Array<int16_t>& array) const
  190. {
  191. Array<const char*> temp(default_allocator());
  192. sjson::parse_array(_at, temp);
  193. for (uint32_t i = 0; i < array::size(temp); i++)
  194. {
  195. array::push_back(array, (int16_t)sjson::parse_int(temp[i]));
  196. }
  197. }
  198. void JSONElement::to_array(Array<uint16_t>& array) const
  199. {
  200. Array<const char*> temp(default_allocator());
  201. sjson::parse_array(_at, temp);
  202. for (uint32_t i = 0; i < array::size(temp); i++)
  203. {
  204. array::push_back(array, (uint16_t)sjson::parse_int(temp[i]));
  205. }
  206. }
  207. void JSONElement::to_array(Array<int32_t>& array) const
  208. {
  209. Array<const char*> temp(default_allocator());
  210. sjson::parse_array(_at, temp);
  211. for (uint32_t i = 0; i < array::size(temp); i++)
  212. {
  213. array::push_back(array, (int32_t)sjson::parse_int(temp[i]));
  214. }
  215. }
  216. void JSONElement::to_array(Array<uint32_t>& array) const
  217. {
  218. Array<const char*> temp(default_allocator());
  219. sjson::parse_array(_at, temp);
  220. for (uint32_t i = 0; i < array::size(temp); i++)
  221. {
  222. array::push_back(array, (uint32_t)sjson::parse_int(temp[i]));
  223. }
  224. }
  225. void JSONElement::to_array(Array<float>& array) const
  226. {
  227. Array<const char*> temp(default_allocator());
  228. sjson::parse_array(_at, temp);
  229. for (uint32_t i = 0; i < array::size(temp); i++)
  230. {
  231. array::push_back(array, sjson::parse_float(temp[i]));
  232. }
  233. }
  234. void JSONElement::to_array(Vector<DynamicString>& array) const
  235. {
  236. Array<const char*> temp(default_allocator());
  237. sjson::parse_array(_at, temp);
  238. for (uint32_t i = 0; i < array::size(temp); i++)
  239. {
  240. TempAllocator256 ta;
  241. DynamicString str(ta);
  242. sjson::parse_string(temp[i], str);
  243. vector::push_back(array, str);
  244. }
  245. }
  246. void JSONElement::to_keys(Vector<DynamicString>& keys) const
  247. {
  248. Map<DynamicString, const char*> object(default_allocator());
  249. sjson::parse(_at, object);
  250. const Map<DynamicString, const char*>::Node* it = map::begin(object);
  251. while (it != map::end(object))
  252. {
  253. vector::push_back(keys, (*it).pair.first);
  254. it++;
  255. }
  256. }
  257. bool JSONElement::is_nil() const
  258. {
  259. if (_at != NULL)
  260. {
  261. return sjson::type(_at) == JsonValueType::NIL;
  262. }
  263. return true;
  264. }
  265. bool JSONElement::is_bool() const
  266. {
  267. if (_at != NULL)
  268. {
  269. return sjson::type(_at) == JsonValueType::BOOL;
  270. }
  271. return false;
  272. }
  273. bool JSONElement::is_number() const
  274. {
  275. if (_at != NULL)
  276. {
  277. return sjson::type(_at) == JsonValueType::NUMBER;
  278. }
  279. return false;
  280. }
  281. bool JSONElement::is_string() const
  282. {
  283. if (_at != NULL)
  284. {
  285. return sjson::type(_at) == JsonValueType::STRING;
  286. }
  287. return false;
  288. }
  289. bool JSONElement::is_array() const
  290. {
  291. if (_at != NULL)
  292. {
  293. return sjson::type(_at) == JsonValueType::ARRAY;
  294. }
  295. return false;
  296. }
  297. bool JSONElement::is_object() const
  298. {
  299. if (_at != NULL)
  300. {
  301. return sjson::type(_at) == JsonValueType::OBJECT;
  302. }
  303. return false;
  304. }
  305. uint32_t JSONElement::size() const
  306. {
  307. if (_at == NULL)
  308. {
  309. return 0;
  310. }
  311. switch(sjson::type(_at))
  312. {
  313. case JsonValueType::NIL:
  314. {
  315. return 1;
  316. }
  317. case JsonValueType::OBJECT:
  318. {
  319. Map<DynamicString, const char*> object(default_allocator());
  320. sjson::parse(_at, object);
  321. return map::size(object);
  322. }
  323. case JsonValueType::ARRAY:
  324. {
  325. Array<const char*> array(default_allocator());
  326. sjson::parse_array(_at, array);
  327. return array::size(array);
  328. }
  329. case JsonValueType::STRING:
  330. {
  331. TempAllocator256 ta;
  332. DynamicString string(ta);
  333. sjson::parse_string(_at, string);
  334. return string.length();
  335. }
  336. case JsonValueType::NUMBER:
  337. {
  338. return 1;
  339. }
  340. case JsonValueType::BOOL:
  341. {
  342. return 1;
  343. }
  344. default:
  345. {
  346. CE_FATAL("Oops, unknown value type");
  347. return 0;
  348. }
  349. }
  350. }
  351. JSONParser::JSONParser(const char* s)
  352. : _file(false)
  353. , _document(s)
  354. {
  355. CE_ASSERT_NOT_NULL(s);
  356. }
  357. JSONParser::JSONParser(File& f)
  358. : _file(true)
  359. , _document(NULL)
  360. {
  361. const uint32_t size = f.size();
  362. char* doc = (char*) default_allocator().allocate(size + 1);
  363. f.read(doc, size);
  364. doc[size] = '\0';
  365. _document = doc;
  366. }
  367. JSONParser::JSONParser(Buffer& b)
  368. : _file(false)
  369. , _document(NULL)
  370. {
  371. array::push_back(b, '\0');
  372. _document = array::begin(b);
  373. }
  374. JSONParser::~JSONParser()
  375. {
  376. if (_file)
  377. {
  378. default_allocator().deallocate((void*) _document);
  379. }
  380. }
  381. JSONElement JSONParser::root()
  382. {
  383. const char* ch = _document;
  384. while ((*ch) && (*ch) <= ' ') ch++;
  385. return JSONElement(ch);
  386. }
  387. } //namespace crown