json_parser.cpp 9.5 KB

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