Json.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. #ifndef WFREST_JSON_H_
  2. #define WFREST_JSON_H_
  3. #include "workflow/json_parser.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <fstream>
  8. #include <functional>
  9. #include <iostream>
  10. #include <map>
  11. #include <sstream>
  12. #include <string>
  13. #include <vector>
  14. namespace wfrest
  15. {
  16. namespace detail
  17. {
  18. template <typename T>
  19. struct is_string
  20. {
  21. static constexpr bool value = false;
  22. };
  23. template <class T, class Traits, class Alloc>
  24. struct is_string<std::basic_string<T, Traits, Alloc>>
  25. {
  26. static constexpr bool value = true;
  27. };
  28. template <typename C>
  29. struct is_char
  30. : std::integral_constant<bool, std::is_same<C, char>::value ||
  31. std::is_same<C, char16_t>::value ||
  32. std::is_same<C, char32_t>::value ||
  33. std::is_same<C, wchar_t>::value>
  34. {
  35. };
  36. template <typename T>
  37. struct is_number
  38. : std::integral_constant<bool, std::is_arithmetic<T>::value &&
  39. !std::is_same<T, bool>::value &&
  40. !detail::is_char<T>::value>
  41. {
  42. };
  43. } // namespace detail
  44. class Object_S;
  45. class Array_S;
  46. class Json
  47. {
  48. public:
  49. using Object = Object_S;
  50. using Array = Array_S;
  51. public:
  52. static Json parse(const std::string& str);
  53. static Json parse(const std::ifstream& stream);
  54. static Json parse(FILE* fp);
  55. std::string dump() const;
  56. std::string dump(int spaces) const;
  57. // for correctness in clang
  58. Json operator[](const char* key);
  59. Json operator[](const char* key) const;
  60. Json operator[](const std::string& key);
  61. Json operator[](const std::string& key) const;
  62. Json operator[](int index);
  63. Json operator[](int index) const;
  64. template <typename T>
  65. void operator=(const T& val)
  66. {
  67. if (parent_ == nullptr)
  68. return;
  69. if (json_value_type(parent_) == JSON_VALUE_ARRAY)
  70. {
  71. update_arr(val);
  72. }
  73. else if (json_value_type(parent_) == JSON_VALUE_OBJECT)
  74. {
  75. push_back_obj(parent_key_, val);
  76. }
  77. }
  78. bool has(const std::string& key) const;
  79. template <typename T>
  80. typename std::enable_if<std::is_same<T, bool>::value, T>::type get() const
  81. {
  82. return json_value_type(node_) == JSON_VALUE_TRUE ? true : false;
  83. }
  84. template <typename T>
  85. typename std::enable_if<detail::is_number<T>::value, T>::type get() const
  86. {
  87. return static_cast<T>(json_value_number(node_));
  88. }
  89. template <typename T>
  90. typename std::enable_if<detail::is_string<T>::value, T>::type get() const
  91. {
  92. return std::string(json_value_string(node_));
  93. }
  94. template <typename T>
  95. typename std::enable_if<std::is_same<T, Object>::value, T>::type
  96. get() const;
  97. template <typename T>
  98. typename std::enable_if<std::is_same<T, Array>::value, T>::type get() const;
  99. template <typename T>
  100. typename std::enable_if<std::is_same<T, std::nullptr_t>::value, T>::type
  101. get() const
  102. {
  103. return nullptr;
  104. }
  105. template <typename T>
  106. operator T()
  107. {
  108. return get<T>();
  109. }
  110. public:
  111. int type() const
  112. {
  113. return json_value_type(node_);
  114. }
  115. std::string type_str() const;
  116. bool is_null() const
  117. {
  118. return type() == JSON_VALUE_NULL;
  119. }
  120. bool is_number() const
  121. {
  122. return type() == JSON_VALUE_NUMBER;
  123. }
  124. bool is_boolean() const
  125. {
  126. int type = this->type();
  127. return type == JSON_VALUE_TRUE || type == JSON_VALUE_FALSE;
  128. }
  129. bool is_object() const
  130. {
  131. return type() == JSON_VALUE_OBJECT;
  132. }
  133. bool is_array() const
  134. {
  135. return type() == JSON_VALUE_ARRAY;
  136. }
  137. bool is_string() const
  138. {
  139. return type() == JSON_VALUE_STRING;
  140. }
  141. bool is_valid() const
  142. {
  143. return node_ != nullptr;
  144. }
  145. int size() const;
  146. bool empty() const;
  147. void clear();
  148. Json copy() const;
  149. std::string key() const
  150. {
  151. return parent_key_;
  152. }
  153. const Json& value() const
  154. {
  155. return *this;
  156. }
  157. public:
  158. // for object
  159. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  160. bool>::type = true>
  161. void push_back(const std::string& key, const T& val)
  162. {
  163. if (!can_obj_push_back())
  164. {
  165. return;
  166. }
  167. json_object_t* obj = json_value_object(node_);
  168. json_object_append(obj, key.c_str(), JSON_VALUE_NUMBER,
  169. static_cast<double>(val));
  170. }
  171. template <typename T,
  172. typename std::enable_if<std::is_same<T, Object>::value ||
  173. std::is_same<T, Array>::value,
  174. bool>::type = true>
  175. void push_back(const std::string& key, const T& val);
  176. void push_back(const std::string& key, bool val);
  177. void push_back(const std::string& key, std::nullptr_t val);
  178. void push_back(const std::string& key, const std::string& val);
  179. void push_back(const std::string& key, const char* val);
  180. void push_back(const std::string& key, const Json& val);
  181. private:
  182. // for object
  183. template <typename T>
  184. void push_back_obj(const std::string& key, const T& val)
  185. {
  186. if (!can_obj_push_back())
  187. {
  188. return;
  189. }
  190. if (is_placeholder())
  191. {
  192. placeholder_push_back(key, val);
  193. }
  194. else
  195. {
  196. normal_push_back(key, val);
  197. }
  198. }
  199. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  200. bool>::type = true>
  201. void placeholder_push_back(const std::string& key, const T& val)
  202. {
  203. json_object_t* obj = json_value_object(parent_);
  204. destroy_node(&node_);
  205. node_ = const_cast<json_value_t*>(json_object_append(
  206. obj, key.c_str(), JSON_VALUE_NUMBER, static_cast<double>(val)));
  207. }
  208. template <typename T,
  209. typename std::enable_if<std::is_same<T, Object>::value ||
  210. std::is_same<T, Array>::value,
  211. bool>::type = true>
  212. void placeholder_push_back(const std::string& key, const T& val);
  213. void placeholder_push_back(const std::string& key, bool val);
  214. void placeholder_push_back(const std::string& key, std::nullptr_t val);
  215. void placeholder_push_back(const std::string& key, const std::string& val);
  216. void placeholder_push_back(const std::string& key, const char* val);
  217. void placeholder_push_back(const std::string& key, const Json& val);
  218. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  219. bool>::type = true>
  220. void normal_push_back(const std::string& key, const T& val)
  221. {
  222. json_object_t* obj = json_value_object(parent_);
  223. const json_value_t* find = json_object_find(key.c_str(), obj);
  224. if (find == nullptr)
  225. {
  226. json_object_append(obj, key.c_str(), JSON_VALUE_NUMBER,
  227. static_cast<double>(val));
  228. return;
  229. }
  230. json_object_insert_before(find, obj, key.c_str(), JSON_VALUE_NUMBER,
  231. static_cast<double>(val));
  232. json_value_t* remove_val = json_object_remove(find, obj);
  233. json_value_destroy(remove_val);
  234. }
  235. template <typename T,
  236. typename std::enable_if<std::is_same<T, Object>::value ||
  237. std::is_same<T, Array>::value,
  238. bool>::type = true>
  239. void normal_push_back(const std::string& key, const T& val);
  240. void normal_push_back(const std::string& key, bool val);
  241. void normal_push_back(const std::string& key, std::nullptr_t val);
  242. void normal_push_back(const std::string& key, const std::string& val);
  243. void normal_push_back(const std::string& key, const char* val);
  244. void normal_push_back(const std::string& key, const Json& val);
  245. public:
  246. void erase(const std::string& key);
  247. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  248. bool>::type = true>
  249. void push_back(T val)
  250. {
  251. if (!can_arr_push_back())
  252. {
  253. return;
  254. }
  255. json_array_t* arr = json_value_array(node_);
  256. json_array_append(arr, JSON_VALUE_NUMBER, static_cast<double>(val));
  257. }
  258. template <typename T,
  259. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  260. std::is_same<T, Json::Array>::value,
  261. bool>::type = true>
  262. void push_back(const T& val);
  263. void push_back(bool val);
  264. void push_back(const std::string& val);
  265. void push_back(const char* val);
  266. void push_back(std::nullptr_t val);
  267. void push_back(const Json& val);
  268. void erase(int index);
  269. private:
  270. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  271. bool>::type = true>
  272. void update_arr(T val)
  273. {
  274. json_array_t* arr = json_value_array(parent_);
  275. json_array_insert_before(node_, arr, JSON_VALUE_NUMBER,
  276. static_cast<double>(val));
  277. json_value_t* remove_val = json_array_remove(node_, arr);
  278. json_value_destroy(remove_val);
  279. }
  280. template <typename T,
  281. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  282. std::is_same<T, Json::Array>::value,
  283. bool>::type = true>
  284. void update_arr(const T& val);
  285. void update_arr(bool val);
  286. void update_arr(const std::string& val);
  287. void update_arr(const char* val);
  288. void update_arr(std::nullptr_t val);
  289. void update_arr(const Json& val);
  290. public:
  291. class IteratorBase
  292. {
  293. public:
  294. friend class Json;
  295. explicit IteratorBase(const json_value_t* val)
  296. : val_(val), json_(new Json(nullptr, nullptr, ""))
  297. {
  298. }
  299. ~IteratorBase()
  300. {
  301. if (json_ != nullptr)
  302. {
  303. delete json_;
  304. }
  305. }
  306. IteratorBase(const IteratorBase& iter)
  307. {
  308. val_ = iter.val_;
  309. key_ = iter.key_;
  310. json_ = new Json(iter.json_->node_, iter.json_->parent_,
  311. iter.json_->parent_key_);
  312. }
  313. IteratorBase& operator=(const IteratorBase& iter)
  314. {
  315. if (this == &iter)
  316. {
  317. return *this;
  318. }
  319. val_ = iter.val_;
  320. key_ = iter.key_;
  321. json_->reset(iter.json_->node_, iter.json_->parent_, false,
  322. iter.json_->parent_key_);
  323. return *this;
  324. }
  325. IteratorBase(IteratorBase&& iter)
  326. : val_(iter.val_), key_(iter.key_), json_(iter.json_)
  327. {
  328. iter.val_ = nullptr;
  329. iter.key_ = nullptr;
  330. iter.json_ = nullptr;
  331. }
  332. IteratorBase& operator=(IteratorBase&& iter)
  333. {
  334. if (this == &iter)
  335. {
  336. return *this;
  337. }
  338. val_ = iter.val_;
  339. iter.val_ = nullptr;
  340. key_ = iter.key_;
  341. iter.key_ = nullptr;
  342. delete json_;
  343. json_ = iter.json_;
  344. iter.json_ = nullptr;
  345. return *this;
  346. }
  347. Json& operator*() const
  348. {
  349. if (key_ != nullptr)
  350. {
  351. json_->parent_key_ = std::string(key_);
  352. }
  353. else
  354. {
  355. json_->parent_key_ = "";
  356. }
  357. return *json_;
  358. }
  359. Json* operator->() const
  360. {
  361. if (key_ != nullptr)
  362. {
  363. json_->parent_key_ = std::string(key_);
  364. }
  365. else
  366. {
  367. json_->parent_key_ = "";
  368. }
  369. return json_;
  370. }
  371. bool operator==(const IteratorBase& other) const
  372. {
  373. return json_->node_ == other.json_->node_;
  374. }
  375. bool operator!=(IteratorBase const& other) const
  376. {
  377. return !(*this == other);
  378. }
  379. private:
  380. const json_value_t* val_ = nullptr;
  381. const char* key_ = nullptr;
  382. Json* json_; // cursor
  383. };
  384. class iterator : public IteratorBase
  385. {
  386. public:
  387. friend class Json;
  388. explicit iterator(const json_value_t* val) : IteratorBase(val)
  389. {
  390. }
  391. iterator& operator++()
  392. {
  393. if (is_end())
  394. {
  395. return *this;
  396. }
  397. forward();
  398. return *this;
  399. }
  400. iterator operator++(int)
  401. {
  402. iterator old = (*this);
  403. if (!is_end())
  404. {
  405. ++(*this);
  406. }
  407. return old;
  408. }
  409. private:
  410. void set_begin()
  411. {
  412. json_->node_ = nullptr;
  413. key_ = nullptr;
  414. forward();
  415. }
  416. void set_end()
  417. {
  418. json_->node_ = nullptr;
  419. key_ = nullptr;
  420. }
  421. bool is_end()
  422. {
  423. return json_->node_ == nullptr && key_ == nullptr;
  424. }
  425. void forward()
  426. {
  427. if (json_value_type(val_) == JSON_VALUE_OBJECT)
  428. {
  429. json_object_t* obj = json_value_object(val_);
  430. key_ = json_object_next_name(key_, obj);
  431. json_->node_ = const_cast<json_value_t*>(
  432. json_object_next_value(json_->node_, obj));
  433. }
  434. else if (json_value_type(val_) == JSON_VALUE_ARRAY)
  435. {
  436. json_array_t* arr = json_value_array(val_);
  437. json_->node_ = const_cast<json_value_t*>(
  438. json_array_next_value(json_->node_, arr));
  439. }
  440. }
  441. };
  442. class reverse_iterator : public IteratorBase
  443. {
  444. public:
  445. friend class Json;
  446. explicit reverse_iterator(const json_value_t* val) : IteratorBase(val)
  447. {
  448. }
  449. reverse_iterator& operator++()
  450. {
  451. if (is_rend())
  452. {
  453. return *this;
  454. }
  455. backward();
  456. return *this;
  457. }
  458. reverse_iterator operator++(int)
  459. {
  460. reverse_iterator old = (*this);
  461. if (!is_rend())
  462. {
  463. ++(*this);
  464. }
  465. return old;
  466. }
  467. private:
  468. void set_rbegin()
  469. {
  470. json_->node_ = nullptr;
  471. key_ = nullptr;
  472. backward();
  473. }
  474. void set_rend()
  475. {
  476. json_->node_ = nullptr;
  477. key_ = nullptr;
  478. }
  479. bool is_rend()
  480. {
  481. return json_->node_ == nullptr && key_ == nullptr;
  482. }
  483. void backward()
  484. {
  485. if (json_value_type(val_) == JSON_VALUE_OBJECT)
  486. {
  487. json_object_t* obj = json_value_object(val_);
  488. key_ = json_object_prev_name(key_, obj);
  489. json_->node_ = const_cast<json_value_t*>(
  490. json_object_prev_value(json_->node_, obj));
  491. }
  492. else if (json_value_type(val_) == JSON_VALUE_ARRAY)
  493. {
  494. json_array_t* arr = json_value_array(val_);
  495. json_->node_ = const_cast<json_value_t*>(
  496. json_array_prev_value(json_->node_, arr));
  497. }
  498. }
  499. };
  500. iterator begin() const
  501. {
  502. iterator iter(node_);
  503. iter.set_begin();
  504. return iter;
  505. }
  506. iterator end() const
  507. {
  508. iterator iter(node_);
  509. iter.set_end();
  510. return iter;
  511. }
  512. reverse_iterator rbegin() const
  513. {
  514. reverse_iterator iter(node_);
  515. iter.set_rbegin();
  516. return iter;
  517. }
  518. reverse_iterator rend() const
  519. {
  520. reverse_iterator iter(node_);
  521. iter.set_rend();
  522. return iter;
  523. }
  524. private:
  525. bool can_obj_push_back();
  526. bool can_arr_push_back();
  527. // can convert to any type, just like a placeholder
  528. bool is_placeholder() const
  529. {
  530. return is_null() && parent_ != nullptr;
  531. }
  532. void destroy_node(json_value_t** node)
  533. {
  534. if (allocated_)
  535. {
  536. json_value_destroy(*node);
  537. *node = nullptr;
  538. allocated_ = false;
  539. }
  540. }
  541. public:
  542. // Constructors for the various types of JSON value.
  543. // Basic json type Constructors
  544. Json();
  545. Json(const std::string& str);
  546. Json(const char* str);
  547. Json(std::nullptr_t null);
  548. Json(double val);
  549. Json(int val);
  550. Json(bool val);
  551. // For parse
  552. Json(const std::string& str, bool parse_flag);
  553. Json(const Json& json);
  554. Json& operator=(const Json& json);
  555. Json(Json&& other);
  556. Json& operator=(Json&& other);
  557. ~Json();
  558. protected:
  559. // watcher
  560. Json(const json_value_t* node, const json_value_t* parent,
  561. std::string&& key);
  562. Json(const json_value_t* node, const json_value_t* parent,
  563. const std::string& key);
  564. enum class JsonType
  565. {
  566. Object,
  567. Array,
  568. };
  569. // For Object && Array Construct
  570. Json(JsonType type);
  571. bool is_root() const
  572. {
  573. return parent_ == nullptr;
  574. }
  575. bool to_object();
  576. bool to_array();
  577. void reset()
  578. {
  579. node_ = nullptr;
  580. parent_ = nullptr;
  581. allocated_ = false;
  582. parent_key_.clear();
  583. }
  584. void reset(json_value_t* node, const json_value_t* parent, bool allocated,
  585. const std::string& parent_key)
  586. {
  587. node_ = node;
  588. parent_ = parent;
  589. allocated_ = allocated;
  590. parent_key_ = parent_key;
  591. }
  592. void reset(json_value_t* node, const json_value_t* parent, bool allocated,
  593. std::string&& parent_key)
  594. {
  595. node_ = node;
  596. parent_ = parent;
  597. allocated_ = allocated;
  598. parent_key_ = std::move(parent_key);
  599. }
  600. void set_parent(const json_value_t* parent, std::string&& parent_key)
  601. {
  602. parent_ = parent;
  603. parent_key_ = std::move(parent_key);
  604. }
  605. private:
  606. json_value_t* node_ = nullptr;
  607. const json_value_t* parent_ = nullptr;
  608. bool allocated_ = false;
  609. std::string parent_key_;
  610. private:
  611. // for parse
  612. static void value_convert(const json_value_t* val, int spaces, int depth,
  613. std::string* out_str);
  614. static void string_convert(const char* raw_str, std::string* out_str);
  615. static void number_convert(double number, std::string* out_str);
  616. static void array_convert(const json_array_t* arr, int spaces, int depth,
  617. std::string* out_str);
  618. static void array_convert_not_format(const json_array_t* arr,
  619. std::string* out_str);
  620. static void object_convert(const json_object_t* obj, int spaces, int depth,
  621. std::string* out_str);
  622. static void object_convert_not_format(const json_object_t* obj,
  623. std::string* out_str);
  624. friend inline std::ostream& operator<<(std::ostream& os, const Json& json)
  625. {
  626. return (os << json.dump());
  627. }
  628. };
  629. class Object_S : public Json
  630. {
  631. public:
  632. Object_S() : Json(JsonType::Object)
  633. {
  634. }
  635. Object_S(const json_value_t* node, const json_value_t* parent)
  636. : Json(node, parent, "")
  637. {
  638. }
  639. using string_type = typename std::basic_string<char, std::char_traits<char>,
  640. std::allocator<char>>;
  641. using pair_type = std::pair<string_type, Json>;
  642. Object_S(std::initializer_list<pair_type> list)
  643. {
  644. std::for_each(list.begin(), list.end(),
  645. [this](const pair_type& pair)
  646. { this->push_back(pair.first, pair.second); });
  647. }
  648. };
  649. class Array_S : public Json
  650. {
  651. public:
  652. Array_S() : Json(JsonType::Array)
  653. {
  654. }
  655. Array_S(const json_value_t* node, const json_value_t* parent)
  656. : Json(node, parent, "")
  657. {
  658. }
  659. Array_S(std::initializer_list<Json> list)
  660. {
  661. std::for_each(list.begin(), list.end(),
  662. [this](const Json& js) { this->push_back(js); });
  663. }
  664. };
  665. template <typename T>
  666. typename std::enable_if<std::is_same<T, Json::Object>::value, T>::type
  667. Json::get() const
  668. {
  669. return Json::Object(node_, parent_);
  670. }
  671. template <typename T>
  672. typename std::enable_if<std::is_same<T, Json::Array>::value, T>::type
  673. Json::get() const
  674. {
  675. return Json::Array(node_, parent_);
  676. }
  677. template <typename T,
  678. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  679. std::is_same<T, Json::Array>::value,
  680. bool>::type>
  681. void Json::push_back(const std::string& key, const T& val)
  682. {
  683. if (!can_obj_push_back())
  684. {
  685. return;
  686. }
  687. json_object_t* obj = json_value_object(node_);
  688. Json copy_json = val.copy();
  689. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  690. copy_json.reset();
  691. }
  692. template <typename T,
  693. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  694. std::is_same<T, Json::Array>::value,
  695. bool>::type>
  696. void Json::placeholder_push_back(const std::string& key, const T& val)
  697. {
  698. json_object_t* obj = json_value_object(parent_);
  699. destroy_node(&node_);
  700. Json copy_json = val.copy();
  701. node_ = const_cast<json_value_t*>(
  702. json_object_append(obj, key.c_str(), 0, copy_json.node_));
  703. copy_json.reset();
  704. }
  705. template <typename T,
  706. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  707. std::is_same<T, Json::Array>::value,
  708. bool>::type>
  709. void Json::normal_push_back(const std::string& key, const T& val)
  710. {
  711. json_object_t* obj = json_value_object(parent_);
  712. const json_value_t* find = json_object_find(key.c_str(), obj);
  713. Json copy_json = val.copy();
  714. if (find == nullptr)
  715. {
  716. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  717. copy_json.reset();
  718. return;
  719. }
  720. json_object_insert_before(find, obj, key.c_str(), 0, copy_json.node_);
  721. copy_json.reset();
  722. json_value_t* remove_val = json_object_remove(find, obj);
  723. json_value_destroy(remove_val);
  724. }
  725. template <typename T,
  726. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  727. ::std::is_same<T, Json::Array>::value,
  728. bool>::type>
  729. void Json::push_back(const T& val)
  730. {
  731. if (!can_arr_push_back())
  732. {
  733. return;
  734. }
  735. json_array_t* arr = json_value_array(node_);
  736. Json copy_json = val.copy();
  737. json_array_append(arr, 0, copy_json.node_);
  738. copy_json.reset();
  739. }
  740. template <typename T,
  741. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  742. ::std::is_same<T, Json::Array>::value,
  743. bool>::type>
  744. void Json::update_arr(const T& val)
  745. {
  746. json_array_t* arr = json_value_array(parent_);
  747. Json copy_json = val.copy();
  748. json_array_insert_before(node_, arr, 0, copy_json.node_);
  749. copy_json.reset();
  750. json_value_t* remove_val = json_array_remove(node_, arr);
  751. json_value_destroy(remove_val);
  752. }
  753. } // namespace wfrest
  754. #endif // WFREST_JSON_H_