Json.cc 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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(bool val)
  65. : node_(val ? json_value_create(JSON_VALUE_TRUE)
  66. : json_value_create(JSON_VALUE_FALSE)),
  67. parent_(nullptr), allocated_(true)
  68. {
  69. }
  70. Json::Json(const std::vector<std::string>& val)
  71. : node_(json_value_create(JSON_VALUE_ARRAY)),
  72. parent_(nullptr), allocated_(true)
  73. {
  74. json_array_t* arr = json_value_array(node_);
  75. for (const auto& str : val)
  76. {
  77. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  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 (is_placeholder())
  341. {
  342. *this = Json::Object{{key, val}};
  343. return;
  344. }
  345. if (!can_obj_push_back())
  346. {
  347. return;
  348. }
  349. json_object_t* obj = json_value_object(node_);
  350. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  351. json_object_append(obj, key.c_str(), type);
  352. }
  353. void Json::push_back(const std::string& key, std::nullptr_t val)
  354. {
  355. if (is_placeholder())
  356. {
  357. *this = Json::Object{{key, val}};
  358. return;
  359. }
  360. if (!can_obj_push_back())
  361. {
  362. return;
  363. }
  364. json_object_t* obj = json_value_object(node_);
  365. json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
  366. }
  367. void Json::push_back(const std::string& key, const std::string& val)
  368. {
  369. push_back(key, val.c_str());
  370. }
  371. void Json::push_back(const std::string& key, const char* val)
  372. {
  373. if (is_placeholder())
  374. {
  375. *this = Json::Object{{key, val}};
  376. return;
  377. }
  378. if (!can_obj_push_back())
  379. {
  380. return;
  381. }
  382. json_object_t* obj = json_value_object(node_);
  383. json_object_append(obj, key.c_str(), JSON_VALUE_STRING, val);
  384. }
  385. void Json::push_back(const std::string& key, const std::vector<std::string>& val)
  386. {
  387. if (is_placeholder())
  388. {
  389. *this = Json::Object{{key, val}};
  390. return;
  391. }
  392. if (!can_obj_push_back())
  393. {
  394. return;
  395. }
  396. json_object_t* obj = json_value_object(node_);
  397. const json_value_t* v = json_object_append(obj, key.c_str(),
  398. JSON_VALUE_ARRAY);
  399. json_array_t* arr = json_value_array(v);
  400. for (const auto& str : val)
  401. {
  402. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  403. }
  404. }
  405. void Json::push_back(const std::string& key, const Json& val)
  406. {
  407. if (is_placeholder())
  408. {
  409. *this = Json::Object{{key, val}};
  410. return;
  411. }
  412. if (!can_obj_push_back())
  413. {
  414. return;
  415. }
  416. json_object_t* obj = json_value_object(node_);
  417. Json copy_json = val;
  418. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  419. copy_json.reset();
  420. }
  421. void Json::placeholder_push_back(const std::string& key, bool val)
  422. {
  423. json_object_t* obj = json_value_object(parent_);
  424. destroy_node(&node_);
  425. if (val)
  426. {
  427. node_ = const_cast<json_value_t*>(
  428. json_object_append(obj, key.c_str(), JSON_VALUE_TRUE));
  429. }
  430. else
  431. {
  432. node_ = const_cast<json_value_t*>(
  433. json_object_append(obj, key.c_str(), JSON_VALUE_FALSE));
  434. }
  435. }
  436. void Json::placeholder_push_back(const std::string& key, std::nullptr_t val)
  437. {
  438. json_object_t* obj = json_value_object(parent_);
  439. destroy_node(&node_);
  440. node_ = const_cast<json_value_t*>(
  441. json_object_append(obj, key.c_str(), JSON_VALUE_NULL));
  442. }
  443. void Json::placeholder_push_back(const std::string& key, const std::string& val)
  444. {
  445. placeholder_push_back(key, val.c_str());
  446. }
  447. void Json::placeholder_push_back(const std::string& key, const char* val)
  448. {
  449. json_object_t* obj = json_value_object(parent_);
  450. destroy_node(&node_);
  451. node_ = const_cast<json_value_t*>(
  452. json_object_append(obj, key.c_str(), JSON_VALUE_STRING, val));
  453. }
  454. void Json::placeholder_push_back(const std::string& key,
  455. const std::vector<std::string>& val)
  456. {
  457. json_object_t* obj = json_value_object(parent_);
  458. destroy_node(&node_);
  459. node_ = const_cast<json_value_t*>(
  460. json_object_append(obj, key.c_str(), JSON_VALUE_ARRAY));
  461. json_array_t* arr = json_value_array(node_);
  462. for (const auto& str : val)
  463. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  464. }
  465. void Json::placeholder_push_back(const std::string& key, const Json& val)
  466. {
  467. json_object_t* obj = json_value_object(parent_);
  468. destroy_node(&node_);
  469. Json copy_json = val;
  470. node_ = const_cast<json_value_t*>(
  471. json_object_append(obj, key.c_str(), 0, copy_json.node_));
  472. copy_json.reset();
  473. }
  474. void Json::normal_push_back(const std::string& key, bool val)
  475. {
  476. json_object_t* obj = json_value_object(parent_);
  477. const json_value_t* find = json_object_find(key.c_str(), obj);
  478. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  479. if (find == nullptr)
  480. {
  481. json_object_append(obj, key.c_str(), type);
  482. return;
  483. }
  484. json_object_insert_before(find, obj, key.c_str(), type);
  485. json_value_t* remove_val = json_object_remove(find, obj);
  486. json_value_destroy(remove_val);
  487. }
  488. void Json::normal_push_back(const std::string& key, std::nullptr_t val)
  489. {
  490. json_object_t* obj = json_value_object(parent_);
  491. const json_value_t* find = json_object_find(key.c_str(), obj);
  492. if (find == nullptr)
  493. {
  494. json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
  495. return;
  496. }
  497. json_object_insert_before(find, obj, key.c_str(), JSON_VALUE_NULL);
  498. json_value_t* remove_val = json_object_remove(find, obj);
  499. json_value_destroy(remove_val);
  500. }
  501. void Json::normal_push_back(const std::string& key, const std::string& val)
  502. {
  503. normal_push_back(key, val.c_str());
  504. }
  505. void Json::normal_push_back(const std::string& key, const char* val)
  506. {
  507. json_object_t* obj = json_value_object(parent_);
  508. const json_value_t* find = json_object_find(key.c_str(), obj);
  509. if (find == nullptr)
  510. {
  511. json_object_append(obj, key.c_str(), JSON_VALUE_STRING, val);
  512. return;
  513. }
  514. json_object_insert_before(find, obj, key.c_str(), JSON_VALUE_STRING, val);
  515. json_value_t* remove_val = json_object_remove(find, obj);
  516. json_value_destroy(remove_val);
  517. }
  518. void Json::normal_push_back(const std::string& key,
  519. const std::vector<std::string>& val)
  520. {
  521. json_object_t* obj = json_value_object(parent_);
  522. const json_value_t* find = json_object_find(key.c_str(), obj);
  523. const json_value_t* v;
  524. if (find == nullptr)
  525. {
  526. v = json_object_append(obj, key.c_str(), JSON_VALUE_ARRAY);
  527. }
  528. else
  529. {
  530. v = json_object_insert_before(find, obj, key.c_str(),
  531. JSON_VALUE_ARRAY);
  532. json_value_t* remove_val = json_object_remove(find, obj);
  533. json_value_destroy(remove_val);
  534. }
  535. json_array_t* arr = json_value_array(v);
  536. for (const auto& str : val)
  537. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  538. }
  539. void Json::normal_push_back(const std::string& key, const Json& val)
  540. {
  541. json_object_t* obj = json_value_object(parent_);
  542. const json_value_t* find = json_object_find(key.c_str(), obj);
  543. Json copy_json = val;
  544. if (find == nullptr)
  545. {
  546. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  547. copy_json.node_ = nullptr;
  548. return;
  549. }
  550. json_object_insert_before(find, obj, key.c_str(), 0, copy_json.node_);
  551. copy_json.reset();
  552. json_value_t* remove_val = json_object_remove(find, obj);
  553. json_value_destroy(remove_val);
  554. }
  555. bool Json::can_arr_push_back()
  556. {
  557. if (is_root() && is_null())
  558. {
  559. to_array();
  560. }
  561. return is_array();
  562. }
  563. Json Json::copy() const
  564. {
  565. return *this;
  566. }
  567. void Json::push_back(bool val)
  568. {
  569. if (is_placeholder())
  570. {
  571. *this = Json::Array{{val}};
  572. return;
  573. }
  574. if (!can_arr_push_back())
  575. {
  576. return;
  577. }
  578. json_array_t* arr = json_value_array(node_);
  579. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  580. json_array_append(arr, type);
  581. }
  582. void Json::push_back(std::nullptr_t val)
  583. {
  584. if (is_placeholder())
  585. {
  586. *this = Json::Array{{val}};
  587. return;
  588. }
  589. if (!can_arr_push_back())
  590. {
  591. return;
  592. }
  593. json_array_t* arr = json_value_array(node_);
  594. json_array_append(arr, JSON_VALUE_NULL);
  595. }
  596. void Json::push_back(const std::string& val)
  597. {
  598. push_back(val.c_str());
  599. }
  600. void Json::push_back(const std::vector<std::string>& val)
  601. {
  602. for (const auto& str : val)
  603. {
  604. push_back(str.c_str());
  605. }
  606. }
  607. void Json::push_back(const char* val)
  608. {
  609. if (is_placeholder())
  610. {
  611. *this = Json::Array{{val}};
  612. return;
  613. }
  614. if (!can_arr_push_back())
  615. {
  616. return;
  617. }
  618. json_array_t* arr = json_value_array(node_);
  619. json_array_append(arr, JSON_VALUE_STRING, val);
  620. }
  621. void Json::push_back(const Json& val)
  622. {
  623. if (is_placeholder())
  624. {
  625. *this = Json::Array{{val}};
  626. return;
  627. }
  628. if (!can_arr_push_back())
  629. {
  630. return;
  631. }
  632. json_array_t* arr = json_value_array(node_);
  633. Json copy_json = val;
  634. json_array_append(arr, 0, copy_json.node_);
  635. copy_json.reset();
  636. }
  637. void Json::update_arr(bool val)
  638. {
  639. json_array_t* arr = json_value_array(parent_);
  640. int type = val ? JSON_VALUE_TRUE : JSON_VALUE_FALSE;
  641. json_array_insert_before(node_, arr, type);
  642. json_value_t* remove_val = json_array_remove(node_, arr);
  643. json_value_destroy(remove_val);
  644. }
  645. void Json::update_arr(std::nullptr_t val)
  646. {
  647. json_array_t* arr = json_value_array(parent_);
  648. json_array_insert_before(node_, arr, JSON_VALUE_NULL);
  649. json_value_t* remove_val = json_array_remove(node_, arr);
  650. json_value_destroy(remove_val);
  651. }
  652. void Json::update_arr(const std::string& val)
  653. {
  654. update_arr(val.c_str());
  655. }
  656. void Json::update_arr(const char* val)
  657. {
  658. json_array_t* arr = json_value_array(parent_);
  659. json_array_insert_before(node_, arr, JSON_VALUE_STRING, val);
  660. json_value_t* remove_val = json_array_remove(node_, arr);
  661. json_value_destroy(remove_val);
  662. }
  663. void Json::update_arr(const std::vector<std::string>& val)
  664. {
  665. json_array_t* arr = json_value_array(parent_);
  666. const json_value_t* v = json_array_insert_before(node_, arr, JSON_VALUE_ARRAY);
  667. json_value_t* remove_val = json_array_remove(node_, arr);
  668. json_value_destroy(remove_val);
  669. arr = json_value_array(v);
  670. for (const auto& str : val)
  671. {
  672. json_array_append(arr, JSON_VALUE_STRING, str.c_str());
  673. }
  674. }
  675. void Json::update_arr(const Json& val)
  676. {
  677. json_array_t* arr = json_value_array(parent_);
  678. Json copy_json = val;
  679. json_array_insert_before(node_, arr, 0, copy_json.node_);
  680. copy_json.reset();
  681. json_value_t* remove_val = json_array_remove(node_, arr);
  682. json_value_destroy(remove_val);
  683. }
  684. std::string Json::type_str() const
  685. {
  686. switch (type())
  687. {
  688. case JSON_VALUE_STRING:
  689. return "string";
  690. case JSON_VALUE_NUMBER:
  691. return "number";
  692. case JSON_VALUE_OBJECT:
  693. return "object";
  694. case JSON_VALUE_ARRAY:
  695. return "array";
  696. case JSON_VALUE_TRUE:
  697. return "true";
  698. case JSON_VALUE_FALSE:
  699. return "false";
  700. case JSON_VALUE_NULL:
  701. return "null";
  702. }
  703. return "unknown";
  704. }
  705. int Json::size() const
  706. {
  707. if (type() == JSON_VALUE_ARRAY)
  708. {
  709. json_array_t* array = json_value_array(node_);
  710. return json_array_size(array);
  711. }
  712. else if (type() == JSON_VALUE_OBJECT)
  713. {
  714. json_object_t* obj = json_value_object(node_);
  715. return json_object_size(obj);
  716. }
  717. return 1;
  718. }
  719. bool Json::empty() const
  720. {
  721. switch (type())
  722. {
  723. case JSON_VALUE_NULL:
  724. {
  725. // null values are empty
  726. return true;
  727. }
  728. case JSON_VALUE_ARRAY:
  729. case JSON_VALUE_OBJECT:
  730. {
  731. return size() == 0;
  732. }
  733. default:
  734. // all other types are nonempty
  735. return false;
  736. }
  737. }
  738. void Json::clear()
  739. {
  740. int type = json_value_type(node_);
  741. destroy_node(&node_);
  742. parent_ = nullptr;
  743. allocated_ = true;
  744. parent_key_.clear();
  745. if (type == JSON_VALUE_STRING)
  746. {
  747. node_ = json_value_create(JSON_VALUE_STRING, "");
  748. }
  749. else if (type == JSON_VALUE_NUMBER)
  750. {
  751. node_ = json_value_create(JSON_VALUE_NUMBER, 0.0);
  752. }
  753. else
  754. {
  755. node_ = json_value_create(type);
  756. }
  757. }
  758. bool Json::to_object()
  759. {
  760. if (!allocated_ || !is_null())
  761. {
  762. // watcher and non-null type can't change type
  763. return false;
  764. }
  765. destroy_node(&node_);
  766. node_ = json_value_create(JSON_VALUE_OBJECT);
  767. allocated_ = true;
  768. return true;
  769. }
  770. bool Json::to_array()
  771. {
  772. if (!allocated_ || !is_null())
  773. {
  774. // watcher and non-null type can't change type
  775. return false;
  776. }
  777. destroy_node(&node_);
  778. node_ = json_value_create(JSON_VALUE_ARRAY);
  779. allocated_ = true;
  780. return true;
  781. }
  782. void Json::value_convert(const json_value_t* val, int spaces, int depth,
  783. std::string* out_str)
  784. {
  785. if (val == nullptr || out_str == nullptr)
  786. return;
  787. switch (json_value_type(val))
  788. {
  789. case JSON_VALUE_STRING:
  790. string_convert(json_value_string(val), out_str);
  791. break;
  792. case JSON_VALUE_NUMBER:
  793. number_convert(json_value_number(val), out_str);
  794. break;
  795. case JSON_VALUE_OBJECT:
  796. object_convert(json_value_object(val), spaces, depth, out_str);
  797. break;
  798. case JSON_VALUE_ARRAY:
  799. array_convert(json_value_array(val), spaces, depth, out_str);
  800. break;
  801. case JSON_VALUE_TRUE:
  802. out_str->append("true");
  803. break;
  804. case JSON_VALUE_FALSE:
  805. out_str->append("false");
  806. break;
  807. case JSON_VALUE_NULL:
  808. out_str->append("null");
  809. break;
  810. }
  811. }
  812. void Json::string_convert(const char* str, std::string* out_str)
  813. {
  814. out_str->append("\"");
  815. while (*str)
  816. {
  817. switch (*str)
  818. {
  819. case '\r':
  820. out_str->append("\\r");
  821. break;
  822. case '\n':
  823. out_str->append("\\n");
  824. break;
  825. case '\f':
  826. out_str->append("\\f");
  827. break;
  828. case '\b':
  829. out_str->append("\\b");
  830. break;
  831. case '\"':
  832. out_str->append("\\\"");
  833. break;
  834. case '\t':
  835. out_str->append("\\t");
  836. break;
  837. case '\\':
  838. out_str->append("\\\\");
  839. break;
  840. default:
  841. out_str->push_back(*str);
  842. break;
  843. }
  844. str++;
  845. }
  846. out_str->append("\"");
  847. }
  848. void Json::number_convert(double number, std::string* out_str)
  849. {
  850. std::ostringstream oss;
  851. long long integer = number;
  852. if (integer == number)
  853. oss << integer;
  854. else
  855. oss << number;
  856. out_str->append(oss.str());
  857. }
  858. void Json::array_convert_not_format(const json_array_t* arr,
  859. std::string* out_str)
  860. {
  861. const json_value_t* val;
  862. int n = 0;
  863. out_str->append("[");
  864. json_array_for_each(val, arr)
  865. {
  866. if (n != 0)
  867. {
  868. out_str->append(",");
  869. }
  870. n++;
  871. value_convert(val, 0, 0, out_str);
  872. }
  873. out_str->append("]");
  874. }
  875. void Json::array_convert(const json_array_t* arr, int spaces, int depth,
  876. std::string* out_str)
  877. {
  878. if (spaces == 0)
  879. {
  880. return array_convert_not_format(arr, out_str);
  881. }
  882. const json_value_t* val;
  883. int n = 0;
  884. int i;
  885. std::string padding(spaces, ' ');
  886. out_str->append("[\n");
  887. json_array_for_each(val, arr)
  888. {
  889. if (n != 0)
  890. {
  891. out_str->append(",\n");
  892. }
  893. n++;
  894. for (i = 0; i < depth + 1; i++)
  895. {
  896. out_str->append(padding);
  897. }
  898. value_convert(val, spaces, depth + 1, out_str);
  899. }
  900. out_str->append("\n");
  901. for (i = 0; i < depth; i++)
  902. {
  903. out_str->append(padding);
  904. }
  905. out_str->append("]");
  906. }
  907. void Json::object_convert_not_format(const json_object_t* obj,
  908. std::string* out_str)
  909. {
  910. const char* name;
  911. const json_value_t* val;
  912. int n = 0;
  913. out_str->append("{");
  914. json_object_for_each(name, val, obj)
  915. {
  916. if (n != 0)
  917. {
  918. out_str->append(",");
  919. }
  920. n++;
  921. out_str->append("\"");
  922. out_str->append(name);
  923. out_str->append("\":");
  924. value_convert(val, 0, 0, out_str);
  925. }
  926. out_str->append("}");
  927. }
  928. void Json::object_convert(const json_object_t* obj, int spaces, int depth,
  929. std::string* out_str)
  930. {
  931. if (spaces == 0)
  932. {
  933. return object_convert_not_format(obj, out_str);
  934. }
  935. const char* name;
  936. const json_value_t* val;
  937. int n = 0;
  938. int i;
  939. std::string padding(spaces, ' ');
  940. out_str->append("{\n");
  941. json_object_for_each(name, val, obj)
  942. {
  943. if (n != 0)
  944. {
  945. out_str->append(",\n");
  946. }
  947. n++;
  948. for (i = 0; i < depth + 1; i++)
  949. {
  950. out_str->append(padding);
  951. }
  952. out_str->append("\"");
  953. out_str->append(name);
  954. out_str->append("\": ");
  955. value_convert(val, spaces, depth + 1, out_str);
  956. }
  957. out_str->append("\n");
  958. for (i = 0; i < depth; i++)
  959. {
  960. out_str->append(padding);
  961. }
  962. out_str->append("}");
  963. }
  964. } // namespace wfrest