Json.cc 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. #include "Json.h"
  2. namespace wfrest
  3. {
  4. namespace
  5. {
  6. json_value_t* json_value_copy(const json_value_t* val);
  7. json_value_t* json_value_copy_object(const json_value_t* val)
  8. {
  9. json_value_t* dest_val = json_value_create(JSON_VALUE_OBJECT);
  10. json_object_t* dest_obj = json_value_object(dest_val);
  11. json_object_t* obj = json_value_object(val);
  12. const char* name;
  13. json_object_for_each(name, val, obj)
  14. json_object_append(dest_obj, name, 0, json_value_copy(val));
  15. return dest_val;
  16. }
  17. json_value_t* json_value_copy_array(const json_value_t* val)
  18. {
  19. json_value_t* dest_val = json_value_create(JSON_VALUE_ARRAY);
  20. json_array_t* dest_arr = json_value_array(dest_val);
  21. json_array_t* arr = json_value_array(val);
  22. json_array_for_each(val, arr)
  23. json_array_append(dest_arr, 0, json_value_copy(val));
  24. return dest_val;
  25. }
  26. json_value_t* json_value_copy(const json_value_t* val)
  27. {
  28. switch (json_value_type(val))
  29. {
  30. case JSON_VALUE_STRING:
  31. return json_value_create(JSON_VALUE_STRING, json_value_string(val));
  32. case JSON_VALUE_NUMBER:
  33. return json_value_create(JSON_VALUE_NUMBER, json_value_number(val));
  34. case JSON_VALUE_OBJECT:
  35. return json_value_copy_object(val);
  36. case JSON_VALUE_ARRAY:
  37. return json_value_copy_array(val);
  38. default:
  39. return json_value_create(json_value_type(val));
  40. }
  41. }
  42. } // namespace
  43. // ------------------------ Constructor -------------------------
  44. Json::Json()
  45. : node_(json_value_create(JSON_VALUE_NULL)), parent_(nullptr),
  46. allocated_(true)
  47. {
  48. }
  49. Json::Json(const std::string& str)
  50. : node_(json_value_create(JSON_VALUE_STRING, str.c_str())),
  51. parent_(nullptr), allocated_(true)
  52. {
  53. }
  54. Json::Json(const char* str)
  55. : node_(json_value_create(JSON_VALUE_STRING, str)), parent_(nullptr),
  56. allocated_(true)
  57. {
  58. }
  59. Json::Json(std::nullptr_t null)
  60. : node_(json_value_create(JSON_VALUE_NULL)), parent_(nullptr),
  61. allocated_(true)
  62. {
  63. }
  64. Json::Json(double val)
  65. : node_(json_value_create(JSON_VALUE_NUMBER, val)), parent_(nullptr),
  66. allocated_(true)
  67. {
  68. }
  69. Json::Json(int val)
  70. : node_(json_value_create(JSON_VALUE_NUMBER, static_cast<double>(val))),
  71. parent_(nullptr), allocated_(true)
  72. {
  73. }
  74. Json::Json(bool val)
  75. : node_(val ? json_value_create(JSON_VALUE_TRUE)
  76. : json_value_create(JSON_VALUE_FALSE)),
  77. parent_(nullptr), allocated_(true)
  78. {
  79. }
  80. // for parse
  81. Json::Json(const std::string& str, bool parse_flag) : parent_(nullptr)
  82. {
  83. node_ = json_value_parse(str.c_str());
  84. allocated_ = node_ == nullptr ? false : true;
  85. }
  86. Json::~Json()
  87. {
  88. destroy_node(&node_);
  89. }
  90. // watcher constructor
  91. Json::Json(const json_value_t* node, const json_value_t* parent,
  92. std::string&& key)
  93. : node_(const_cast<json_value_t*>(node)), parent_(parent),
  94. allocated_(false), parent_key_(std::move(key))
  95. {
  96. }
  97. Json::Json(const json_value_t* node, const json_value_t* parent,
  98. const std::string& key)
  99. : node_(const_cast<json_value_t*>(node)), parent_(parent),
  100. allocated_(false), parent_key_(key)
  101. {
  102. }
  103. Json::Json(JsonType type) : allocated_(true)
  104. {
  105. if (type == JsonType::Object)
  106. {
  107. node_ = json_value_create(JSON_VALUE_OBJECT);
  108. }
  109. else
  110. {
  111. node_ = json_value_create(JSON_VALUE_ARRAY);
  112. }
  113. }
  114. Json::Json(const Json& other)
  115. {
  116. node_ = json_value_copy(other.node_);
  117. allocated_ = true;
  118. }
  119. Json& Json::operator=(const Json& other)
  120. {
  121. if (this == &other)
  122. {
  123. return *this;
  124. }
  125. destroy_node(&node_);
  126. node_ = json_value_copy(other.node_);
  127. parent_ = nullptr;
  128. allocated_ = true;
  129. parent_key_.clear();
  130. return *this;
  131. }
  132. Json::Json(Json&& other)
  133. : node_(other.node_), parent_(other.parent_), allocated_(other.allocated_),
  134. parent_key_(std::move(other.parent_key_))
  135. {
  136. other.node_ = nullptr;
  137. other.parent_ = nullptr;
  138. other.allocated_ = false;
  139. }
  140. Json& Json::operator=(Json&& other)
  141. {
  142. if (this == &other)
  143. {
  144. return *this;
  145. }
  146. destroy_node(&node_);
  147. node_ = other.node_;
  148. other.node_ = nullptr;
  149. parent_ = other.parent_;
  150. other.parent_ = nullptr;
  151. allocated_ = other.allocated_;
  152. other.allocated_ = false;
  153. parent_key_ = std::move(other.parent_key_);
  154. return *this;
  155. }
  156. Json Json::parse(const std::string& str)
  157. {
  158. return Json(str, true);
  159. }
  160. Json Json::parse(const std::ifstream& stream)
  161. {
  162. std::stringstream buffer;
  163. buffer << stream.rdbuf();
  164. return Json(buffer.str(), true);
  165. }
  166. Json Json::parse(FILE* fp)
  167. {
  168. if (fp == nullptr)
  169. {
  170. return Json();
  171. }
  172. fseek(fp, 0, SEEK_END);
  173. long length = ftell(fp);
  174. fseek(fp, 0, SEEK_SET);
  175. char* buffer = (char*)malloc(length + 1);
  176. buffer[length] = '\0';
  177. long ret = fread(buffer, 1, length, fp);
  178. Json js;
  179. if (ret != length)
  180. {
  181. js = Json();
  182. }
  183. else
  184. {
  185. js = Json(buffer, true);
  186. }
  187. free(buffer);
  188. return js;
  189. }
  190. std::string Json::dump() const
  191. {
  192. return dump(0);
  193. }
  194. std::string Json::dump(int spaces) const
  195. {
  196. std::string str;
  197. str.reserve(64);
  198. value_convert(node_, spaces, 0, &str);
  199. return str;
  200. }
  201. Json Json::operator[](const char* key)
  202. {
  203. if (is_null() && is_root())
  204. {
  205. // todo : need is_root here?
  206. to_object();
  207. }
  208. else if (is_object())
  209. {
  210. // if exists
  211. json_object_t* obj = json_value_object(node_);
  212. const json_value_t* res = json_object_find(key, obj);
  213. if (res != nullptr)
  214. {
  215. return Json(res, node_, std::string(key));
  216. }
  217. }
  218. if (is_placeholder())
  219. {
  220. destroy_node(&node_);
  221. json_object_t* parent_obj = json_value_object(parent_);
  222. node_ = const_cast<json_value_t*>(json_object_append(
  223. parent_obj, parent_key_.c_str(), JSON_VALUE_OBJECT));
  224. }
  225. if (!is_object())
  226. {
  227. return Json();
  228. }
  229. // (null, parent(node_), key)
  230. Json js = Json();
  231. js.set_parent(node_, std::string(key));
  232. return js;
  233. }
  234. Json Json::operator[](const char* key) const
  235. {
  236. if (!is_object())
  237. {
  238. return Json();
  239. }
  240. const json_value_t* val = node_;
  241. json_object_t* obj = json_value_object(val);
  242. const json_value_t* res = json_object_find(key, obj);
  243. if (res != nullptr)
  244. {
  245. return Json(res, node_, "");
  246. }
  247. return Json();
  248. }
  249. Json Json::operator[](const std::string& key)
  250. {
  251. return this->operator[](key.c_str());
  252. }
  253. Json Json::operator[](const std::string& key) const
  254. {
  255. return this->operator[](key.c_str());
  256. }
  257. bool Json::has(const std::string& key) const
  258. {
  259. json_object_t* obj = json_value_object(node_);
  260. const json_value_t* find = json_object_find(key.c_str(), obj);
  261. return find != nullptr;
  262. }
  263. void Json::erase(const std::string& key)
  264. {
  265. if (!is_object())
  266. return;
  267. json_object_t* obj = json_value_object(node_);
  268. const json_value_t* find = json_object_find(key.c_str(), obj);
  269. if (find == nullptr)
  270. return;
  271. json_value_t* remove_val = json_object_remove(find, obj);
  272. json_value_destroy(remove_val);
  273. }
  274. Json Json::operator[](int index)
  275. {
  276. if (!is_array() || index < 0 || index > this->size())
  277. {
  278. return Json();
  279. }
  280. const json_value_t* val;
  281. json_array_t* arr = json_value_array(node_);
  282. json_array_for_each(val, arr)
  283. {
  284. if (index == 0)
  285. {
  286. return Json(val, node_, "");
  287. }
  288. index--;
  289. }
  290. return Json();
  291. }
  292. void Json::erase(int index)
  293. {
  294. if (!is_array())
  295. return;
  296. int cnt = 0;
  297. json_array_t* arr = json_value_array(node_);
  298. const json_value_t* arr_cursor = nullptr;
  299. json_array_for_each(arr_cursor, arr)
  300. {
  301. if (cnt++ == index)
  302. break;
  303. }
  304. json_value_t* remove_val = json_array_remove(arr_cursor, arr);
  305. json_value_destroy(remove_val);
  306. }
  307. Json Json::operator[](int index) const
  308. {
  309. if (!is_array() || index < 0 || index > this->size())
  310. {
  311. return Json();
  312. }
  313. const json_value_t* val;
  314. json_array_t* arr = json_value_array(node_);
  315. json_array_for_each(val, arr)
  316. {
  317. if (index == 0)
  318. {
  319. return Json(val, node_, "");
  320. }
  321. index--;
  322. }
  323. return Json();
  324. }
  325. bool Json::can_obj_push_back()
  326. {
  327. if (is_placeholder() ||
  328. (parent_ != nullptr && json_value_type(parent_) == JSON_VALUE_OBJECT))
  329. {
  330. return true;
  331. }
  332. if (is_root() && is_null())
  333. {
  334. to_object();
  335. }
  336. return is_object();
  337. }
  338. void Json::push_back(const std::string& key, bool val)
  339. {
  340. if (!can_obj_push_back())
  341. {
  342. return;
  343. }
  344. json_object_t* obj = json_value_object(node_);
  345. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  346. json_object_append(obj, key.c_str(), type);
  347. }
  348. void Json::push_back(const std::string& key, std::nullptr_t val)
  349. {
  350. if (!can_obj_push_back())
  351. {
  352. return;
  353. }
  354. json_object_t* obj = json_value_object(node_);
  355. json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
  356. }
  357. void Json::push_back(const std::string& key, const std::string& val)
  358. {
  359. push_back(key, val.c_str());
  360. }
  361. void Json::push_back(const std::string& key, const char* val)
  362. {
  363. if (!can_obj_push_back())
  364. {
  365. return;
  366. }
  367. json_object_t* obj = json_value_object(node_);
  368. json_object_append(obj, key.c_str(), JSON_VALUE_STRING, val);
  369. }
  370. void Json::push_back(const std::string& key, const std::vector<std::string>& val)
  371. {
  372. if (!can_obj_push_back())
  373. {
  374. return;
  375. }
  376. json_object_t* obj = json_value_object(node_);
  377. const json_value_t* v = json_object_append(obj, key.c_str(),
  378. JSON_VALUE_ARRAY);
  379. json_array_t* arr = json_value_array(v);
  380. for (const auto& str : val)
  381. {
  382. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  383. }
  384. }
  385. void Json::push_back(const std::string& key, const Json& val)
  386. {
  387. if (!can_obj_push_back())
  388. {
  389. return;
  390. }
  391. json_object_t* obj = json_value_object(node_);
  392. Json copy_json = val;
  393. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  394. copy_json.reset();
  395. }
  396. void Json::placeholder_push_back(const std::string& key, bool val)
  397. {
  398. json_object_t* obj = json_value_object(parent_);
  399. destroy_node(&node_);
  400. if (val)
  401. {
  402. node_ = const_cast<json_value_t*>(
  403. json_object_append(obj, key.c_str(), JSON_VALUE_TRUE));
  404. }
  405. else
  406. {
  407. node_ = const_cast<json_value_t*>(
  408. json_object_append(obj, key.c_str(), JSON_VALUE_FALSE));
  409. }
  410. }
  411. void Json::placeholder_push_back(const std::string& key, std::nullptr_t val)
  412. {
  413. json_object_t* obj = json_value_object(parent_);
  414. destroy_node(&node_);
  415. node_ = const_cast<json_value_t*>(
  416. json_object_append(obj, key.c_str(), JSON_VALUE_NULL));
  417. }
  418. void Json::placeholder_push_back(const std::string& key, const std::string& val)
  419. {
  420. placeholder_push_back(key, val.c_str());
  421. }
  422. void Json::placeholder_push_back(const std::string& key, const char* val)
  423. {
  424. json_object_t* obj = json_value_object(parent_);
  425. destroy_node(&node_);
  426. node_ = const_cast<json_value_t*>(
  427. json_object_append(obj, key.c_str(), JSON_VALUE_STRING, val));
  428. }
  429. void Json::placeholder_push_back(const std::string& key,
  430. const std::vector<std::string>& val)
  431. {
  432. json_object_t* obj = json_value_object(parent_);
  433. destroy_node(&node_);
  434. node_ = const_cast<json_value_t*>(
  435. json_object_append(obj, key.c_str(), JSON_VALUE_ARRAY));
  436. json_array_t* arr = json_value_array(node_);
  437. for (const auto& str : val)
  438. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  439. }
  440. void Json::placeholder_push_back(const std::string& key, const Json& val)
  441. {
  442. json_object_t* obj = json_value_object(parent_);
  443. destroy_node(&node_);
  444. Json copy_json = val;
  445. node_ = const_cast<json_value_t*>(
  446. json_object_append(obj, key.c_str(), 0, copy_json.node_));
  447. copy_json.reset();
  448. }
  449. void Json::normal_push_back(const std::string& key, bool val)
  450. {
  451. json_object_t* obj = json_value_object(parent_);
  452. const json_value_t* find = json_object_find(key.c_str(), obj);
  453. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  454. if (find == nullptr)
  455. {
  456. json_object_append(obj, key.c_str(), type);
  457. return;
  458. }
  459. json_object_insert_before(find, obj, key.c_str(), type);
  460. json_value_t* remove_val = json_object_remove(find, obj);
  461. json_value_destroy(remove_val);
  462. }
  463. void Json::normal_push_back(const std::string& key, std::nullptr_t val)
  464. {
  465. json_object_t* obj = json_value_object(parent_);
  466. const json_value_t* find = json_object_find(key.c_str(), obj);
  467. if (find == nullptr)
  468. {
  469. json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
  470. return;
  471. }
  472. json_object_insert_before(find, obj, key.c_str(), JSON_VALUE_NULL);
  473. json_value_t* remove_val = json_object_remove(find, obj);
  474. json_value_destroy(remove_val);
  475. }
  476. void Json::normal_push_back(const std::string& key, const std::string& val)
  477. {
  478. normal_push_back(key, val.c_str());
  479. }
  480. void Json::normal_push_back(const std::string& key, const char* val)
  481. {
  482. json_object_t* obj = json_value_object(parent_);
  483. const json_value_t* find = json_object_find(key.c_str(), obj);
  484. if (find == nullptr)
  485. {
  486. json_object_append(obj, key.c_str(), JSON_VALUE_STRING, val);
  487. return;
  488. }
  489. json_object_insert_before(find, obj, key.c_str(), JSON_VALUE_STRING, val);
  490. json_value_t* remove_val = json_object_remove(find, obj);
  491. json_value_destroy(remove_val);
  492. }
  493. void Json::normal_push_back(const std::string& key,
  494. const std::vector<std::string>& val)
  495. {
  496. json_object_t* obj = json_value_object(parent_);
  497. const json_value_t* find = json_object_find(key.c_str(), obj);
  498. if (find == nullptr)
  499. {
  500. json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
  501. return;
  502. }
  503. const json_value_t *v = json_object_insert_before(find, obj, key.c_str(),
  504. JSON_VALUE_ARRAY);
  505. json_value_t* remove_val = json_object_remove(find, obj);
  506. json_value_destroy(remove_val);
  507. json_array_t* arr = json_value_array(v);
  508. for (const auto& str : val)
  509. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  510. }
  511. void Json::normal_push_back(const std::string& key, const Json& val)
  512. {
  513. json_object_t* obj = json_value_object(parent_);
  514. const json_value_t* find = json_object_find(key.c_str(), obj);
  515. Json copy_json = val;
  516. if (find == nullptr)
  517. {
  518. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  519. copy_json.node_ = nullptr;
  520. return;
  521. }
  522. json_object_insert_before(find, obj, key.c_str(), 0, copy_json.node_);
  523. copy_json.reset();
  524. json_value_t* remove_val = json_object_remove(find, obj);
  525. json_value_destroy(remove_val);
  526. }
  527. bool Json::can_arr_push_back()
  528. {
  529. if (is_root() && is_null())
  530. {
  531. to_array();
  532. }
  533. return is_array();
  534. }
  535. Json Json::copy() const
  536. {
  537. return *this;
  538. }
  539. void Json::push_back(bool val)
  540. {
  541. if (!can_arr_push_back())
  542. {
  543. return;
  544. }
  545. json_array_t* arr = json_value_array(node_);
  546. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  547. json_array_append(arr, type);
  548. }
  549. void Json::push_back(std::nullptr_t val)
  550. {
  551. if (!can_arr_push_back())
  552. {
  553. return;
  554. }
  555. json_array_t* arr = json_value_array(node_);
  556. json_array_append(arr, JSON_VALUE_NULL);
  557. }
  558. void Json::push_back(const std::string& val)
  559. {
  560. push_back(val.c_str());
  561. }
  562. void Json::push_back(const std::vector<std::string>& val)
  563. {
  564. for (const auto& str : val)
  565. {
  566. push_back(str.c_str());
  567. }
  568. }
  569. void Json::push_back(const char* val)
  570. {
  571. if (!can_arr_push_back())
  572. {
  573. return;
  574. }
  575. json_array_t* arr = json_value_array(node_);
  576. json_array_append(arr, JSON_VALUE_STRING, val);
  577. }
  578. void Json::push_back(const Json& val)
  579. {
  580. if (!can_arr_push_back())
  581. {
  582. return;
  583. }
  584. json_array_t* arr = json_value_array(node_);
  585. Json copy_json = val;
  586. json_array_append(arr, 0, copy_json.node_);
  587. copy_json.reset();
  588. }
  589. void Json::update_arr(bool val)
  590. {
  591. json_array_t* arr = json_value_array(parent_);
  592. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  593. json_array_insert_before(node_, arr, type);
  594. json_value_t* remove_val = json_array_remove(node_, arr);
  595. json_value_destroy(remove_val);
  596. }
  597. void Json::update_arr(std::nullptr_t val)
  598. {
  599. json_array_t* arr = json_value_array(parent_);
  600. json_array_insert_before(node_, arr, JSON_VALUE_NULL);
  601. json_value_t* remove_val = json_array_remove(node_, arr);
  602. json_value_destroy(remove_val);
  603. }
  604. void Json::update_arr(const std::string& val)
  605. {
  606. update_arr(val.c_str());
  607. }
  608. void Json::update_arr(const char* val)
  609. {
  610. json_array_t* arr = json_value_array(parent_);
  611. json_array_insert_before(node_, arr, JSON_VALUE_STRING, val);
  612. json_value_t* remove_val = json_array_remove(node_, arr);
  613. json_value_destroy(remove_val);
  614. }
  615. void Json::update_arr(const std::vector<std::string>& val)
  616. {
  617. json_array_t* arr = json_value_array(parent_);
  618. const json_value_t* v = json_array_insert_before(node_, arr, JSON_VALUE_ARRAY);
  619. json_value_t* remove_val = json_array_remove(node_, arr);
  620. json_value_destroy(remove_val);
  621. arr = json_value_array(v);
  622. for (const auto& str : val)
  623. {
  624. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  625. }
  626. }
  627. void Json::update_arr(const Json& val)
  628. {
  629. json_array_t* arr = json_value_array(parent_);
  630. Json copy_json = val;
  631. json_array_insert_before(node_, arr, 0, copy_json.node_);
  632. copy_json.reset();
  633. json_value_t* remove_val = json_array_remove(node_, arr);
  634. json_value_destroy(remove_val);
  635. }
  636. std::string Json::type_str() const
  637. {
  638. switch (type())
  639. {
  640. case JSON_VALUE_STRING:
  641. return "string";
  642. case JSON_VALUE_NUMBER:
  643. return "number";
  644. case JSON_VALUE_OBJECT:
  645. return "object";
  646. case JSON_VALUE_ARRAY:
  647. return "array";
  648. case JSON_VALUE_TRUE:
  649. return "true";
  650. case JSON_VALUE_FALSE:
  651. return "false";
  652. case JSON_VALUE_NULL:
  653. return "null";
  654. }
  655. return "unknown";
  656. }
  657. int Json::size() const
  658. {
  659. if (type() == JSON_VALUE_ARRAY)
  660. {
  661. json_array_t* array = json_value_array(node_);
  662. return json_array_size(array);
  663. }
  664. else if (type() == JSON_VALUE_OBJECT)
  665. {
  666. json_object_t* obj = json_value_object(node_);
  667. return json_object_size(obj);
  668. }
  669. return 1;
  670. }
  671. bool Json::empty() const
  672. {
  673. switch (type())
  674. {
  675. case JSON_VALUE_NULL:
  676. {
  677. // null values are empty
  678. return true;
  679. }
  680. case JSON_VALUE_ARRAY:
  681. case JSON_VALUE_OBJECT:
  682. {
  683. return size() == 0;
  684. }
  685. default:
  686. // all other types are nonempty
  687. return false;
  688. }
  689. }
  690. void Json::clear()
  691. {
  692. int type = json_value_type(node_);
  693. destroy_node(&node_);
  694. parent_ = nullptr;
  695. allocated_ = true;
  696. parent_key_.clear();
  697. if (type == JSON_VALUE_STRING)
  698. {
  699. node_ = json_value_create(JSON_VALUE_STRING, "");
  700. }
  701. else if (type == JSON_VALUE_NUMBER)
  702. {
  703. node_ = json_value_create(JSON_VALUE_NUMBER, 0.0);
  704. }
  705. else
  706. {
  707. node_ = json_value_create(type);
  708. }
  709. }
  710. bool Json::to_object()
  711. {
  712. if (!allocated_ || !is_null())
  713. {
  714. // watcher and non-null type can't change type
  715. return false;
  716. }
  717. destroy_node(&node_);
  718. node_ = json_value_create(JSON_VALUE_OBJECT);
  719. allocated_ = true;
  720. return true;
  721. }
  722. bool Json::to_array()
  723. {
  724. if (!allocated_ || !is_null())
  725. {
  726. // watcher and non-null type can't change type
  727. return false;
  728. }
  729. destroy_node(&node_);
  730. node_ = json_value_create(JSON_VALUE_ARRAY);
  731. allocated_ = true;
  732. return true;
  733. }
  734. void Json::value_convert(const json_value_t* val, int spaces, int depth,
  735. std::string* out_str)
  736. {
  737. if (val == nullptr || out_str == nullptr)
  738. return;
  739. switch (json_value_type(val))
  740. {
  741. case JSON_VALUE_STRING:
  742. string_convert(json_value_string(val), out_str);
  743. break;
  744. case JSON_VALUE_NUMBER:
  745. number_convert(json_value_number(val), out_str);
  746. break;
  747. case JSON_VALUE_OBJECT:
  748. object_convert(json_value_object(val), spaces, depth, out_str);
  749. break;
  750. case JSON_VALUE_ARRAY:
  751. array_convert(json_value_array(val), spaces, depth, out_str);
  752. break;
  753. case JSON_VALUE_TRUE:
  754. out_str->append("true");
  755. break;
  756. case JSON_VALUE_FALSE:
  757. out_str->append("false");
  758. break;
  759. case JSON_VALUE_NULL:
  760. out_str->append("null");
  761. break;
  762. }
  763. }
  764. void Json::string_convert(const char* str, std::string* out_str)
  765. {
  766. out_str->append("\"");
  767. while (*str)
  768. {
  769. switch (*str)
  770. {
  771. case '\r':
  772. out_str->append("\\r");
  773. break;
  774. case '\n':
  775. out_str->append("\\n");
  776. break;
  777. case '\f':
  778. out_str->append("\\f");
  779. break;
  780. case '\b':
  781. out_str->append("\\b");
  782. break;
  783. case '\"':
  784. out_str->append("\\\"");
  785. break;
  786. case '\t':
  787. out_str->append("\\t");
  788. break;
  789. case '\\':
  790. out_str->append("\\\\");
  791. break;
  792. default:
  793. out_str->push_back(*str);
  794. break;
  795. }
  796. str++;
  797. }
  798. out_str->append("\"");
  799. }
  800. void Json::number_convert(double number, std::string* out_str)
  801. {
  802. std::ostringstream oss;
  803. long long integer = number;
  804. if (integer == number)
  805. oss << integer;
  806. else
  807. oss << number;
  808. out_str->append(oss.str());
  809. }
  810. void Json::array_convert_not_format(const json_array_t* arr,
  811. std::string* out_str)
  812. {
  813. const json_value_t* val;
  814. int n = 0;
  815. out_str->append("[");
  816. json_array_for_each(val, arr)
  817. {
  818. if (n != 0)
  819. {
  820. out_str->append(",");
  821. }
  822. n++;
  823. value_convert(val, 0, 0, out_str);
  824. }
  825. out_str->append("]");
  826. }
  827. void Json::array_convert(const json_array_t* arr, int spaces, int depth,
  828. std::string* out_str)
  829. {
  830. if (spaces == 0)
  831. {
  832. return array_convert_not_format(arr, out_str);
  833. }
  834. const json_value_t* val;
  835. int n = 0;
  836. int i;
  837. std::string padding(spaces, ' ');
  838. out_str->append("[\n");
  839. json_array_for_each(val, arr)
  840. {
  841. if (n != 0)
  842. {
  843. out_str->append(",\n");
  844. }
  845. n++;
  846. for (i = 0; i < depth + 1; i++)
  847. {
  848. out_str->append(padding);
  849. }
  850. value_convert(val, spaces, depth + 1, out_str);
  851. }
  852. out_str->append("\n");
  853. for (i = 0; i < depth; i++)
  854. {
  855. out_str->append(padding);
  856. }
  857. out_str->append("]");
  858. }
  859. void Json::object_convert_not_format(const json_object_t* obj,
  860. std::string* out_str)
  861. {
  862. const char* name;
  863. const json_value_t* val;
  864. int n = 0;
  865. out_str->append("{");
  866. json_object_for_each(name, val, obj)
  867. {
  868. if (n != 0)
  869. {
  870. out_str->append(",");
  871. }
  872. n++;
  873. out_str->append("\"");
  874. out_str->append(name);
  875. out_str->append("\":");
  876. value_convert(val, 0, 0, out_str);
  877. }
  878. out_str->append("}");
  879. }
  880. void Json::object_convert(const json_object_t* obj, int spaces, int depth,
  881. std::string* out_str)
  882. {
  883. if (spaces == 0)
  884. {
  885. return object_convert_not_format(obj, out_str);
  886. }
  887. const char* name;
  888. const json_value_t* val;
  889. int n = 0;
  890. int i;
  891. std::string padding(spaces, ' ');
  892. out_str->append("{\n");
  893. json_object_for_each(name, val, obj)
  894. {
  895. if (n != 0)
  896. {
  897. out_str->append(",\n");
  898. }
  899. n++;
  900. for (i = 0; i < depth + 1; i++)
  901. {
  902. out_str->append(padding);
  903. }
  904. out_str->append("\"");
  905. out_str->append(name);
  906. out_str->append("\": ");
  907. value_convert(val, spaces, depth + 1, out_str);
  908. }
  909. out_str->append("\n");
  910. for (i = 0; i < depth; i++)
  911. {
  912. out_str->append(padding);
  913. }
  914. out_str->append("}");
  915. }
  916. } // namespace wfrest