Json.h 24 KB

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