Json.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 (!can_obj_push_back())
  165. {
  166. return;
  167. }
  168. json_object_t* obj = json_value_object(node_);
  169. json_object_append(obj, key.c_str(), JSON_VALUE_NUMBER,
  170. static_cast<double>(val));
  171. }
  172. template <typename T,
  173. typename std::enable_if<std::is_same<T, Object>::value ||
  174. std::is_same<T, Array>::value,
  175. bool>::type = true>
  176. void push_back(const std::string& key, const T& val)
  177. {
  178. if (!can_obj_push_back())
  179. {
  180. return;
  181. }
  182. json_object_t* obj = json_value_object(node_);
  183. Json copy_json = val.copy();
  184. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  185. copy_json.reset();
  186. }
  187. void push_back(const std::string& key, bool val);
  188. void push_back(const std::string& key, std::nullptr_t val);
  189. void push_back(const std::string& key, const std::string& val);
  190. void push_back(const std::string& key, const char* val);
  191. void push_back(const std::string& key, const std::vector<std::string>& val);
  192. void push_back(const std::string& key, const std::initializer_list<std::string>& val)
  193. {
  194. push_back(key, std::vector<std::string>(val));
  195. }
  196. void push_back(const std::string& key, const Json& val);
  197. private:
  198. // for object
  199. template <typename T>
  200. void push_back_obj(const std::string& key, const T& val)
  201. {
  202. if (!can_obj_push_back())
  203. {
  204. return;
  205. }
  206. if (is_placeholder())
  207. {
  208. placeholder_push_back(key, val);
  209. }
  210. else
  211. {
  212. normal_push_back(key, val);
  213. }
  214. }
  215. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  216. bool>::type = true>
  217. void placeholder_push_back(const std::string& key, const T& val)
  218. {
  219. json_object_t* obj = json_value_object(parent_);
  220. destroy_node(&node_);
  221. node_ = const_cast<json_value_t*>(json_object_append(
  222. obj, key.c_str(), JSON_VALUE_NUMBER, static_cast<double>(val)));
  223. }
  224. template <typename T,
  225. typename std::enable_if<std::is_same<T, Object>::value ||
  226. std::is_same<T, Array>::value,
  227. bool>::type = true>
  228. void placeholder_push_back(const std::string& key, const T& val)
  229. {
  230. json_object_t* obj = json_value_object(parent_);
  231. destroy_node(&node_);
  232. Json copy_json = val.copy();
  233. node_ = const_cast<json_value_t*>(
  234. json_object_append(obj, key.c_str(), 0, copy_json.node_));
  235. copy_json.reset();
  236. }
  237. void placeholder_push_back(const std::string& key, bool val);
  238. void placeholder_push_back(const std::string& key, std::nullptr_t val);
  239. void placeholder_push_back(const std::string& key, const std::string& val);
  240. void placeholder_push_back(const std::string& key, const char* val);
  241. void placeholder_push_back(const std::string& key,
  242. const std::vector<std::string>& val);
  243. void placeholder_push_back(const std::string& key, const Json& val);
  244. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  245. bool>::type = true>
  246. void normal_push_back(const std::string& key, const T& val)
  247. {
  248. json_object_t* obj = json_value_object(parent_);
  249. const json_value_t* find = json_object_find(key.c_str(), obj);
  250. if (find == nullptr)
  251. {
  252. json_object_append(obj, key.c_str(), JSON_VALUE_NUMBER,
  253. static_cast<double>(val));
  254. return;
  255. }
  256. json_object_insert_before(find, obj, key.c_str(), JSON_VALUE_NUMBER,
  257. static_cast<double>(val));
  258. json_value_t* remove_val = json_object_remove(find, obj);
  259. json_value_destroy(remove_val);
  260. }
  261. template <typename T,
  262. typename std::enable_if<std::is_same<T, Object>::value ||
  263. std::is_same<T, Array>::value,
  264. bool>::type = true>
  265. void normal_push_back(const std::string& key, const T& val)
  266. {
  267. json_object_t* obj = json_value_object(parent_);
  268. const json_value_t* find = json_object_find(key.c_str(), obj);
  269. Json copy_json = val.copy();
  270. if (find == nullptr)
  271. {
  272. json_object_append(obj, key.c_str(), 0, copy_json.node_);
  273. copy_json.reset();
  274. return;
  275. }
  276. json_object_insert_before(find, obj, key.c_str(), 0, copy_json.node_);
  277. copy_json.reset();
  278. json_value_t* remove_val = json_object_remove(find, obj);
  279. json_value_destroy(remove_val);
  280. }
  281. void normal_push_back(const std::string& key, bool val);
  282. void normal_push_back(const std::string& key, std::nullptr_t val);
  283. void normal_push_back(const std::string& key, const std::string& val);
  284. void normal_push_back(const std::string& key, const char* val);
  285. void normal_push_back(const std::string& key,
  286. const std::vector<std::string>& val);
  287. void normal_push_back(const std::string& key, const Json& val);
  288. public:
  289. void erase(const std::string& key);
  290. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  291. bool>::type = true>
  292. void push_back(T val)
  293. {
  294. if (!can_arr_push_back())
  295. {
  296. return;
  297. }
  298. json_array_t* arr = json_value_array(node_);
  299. json_array_append(arr, JSON_VALUE_NUMBER, static_cast<double>(val));
  300. }
  301. template <typename T,
  302. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  303. std::is_same<T, Json::Array>::value,
  304. bool>::type = true>
  305. void push_back(const T& val)
  306. {
  307. if (!can_arr_push_back())
  308. {
  309. return;
  310. }
  311. json_array_t* arr = json_value_array(node_);
  312. Json copy_json = val.copy();
  313. json_array_append(arr, 0, copy_json.node_);
  314. copy_json.reset();
  315. }
  316. void push_back(bool val);
  317. void push_back(std::nullptr_t val);
  318. void push_back(const std::string& val);
  319. void push_back(const char* val);
  320. void push_back(const std::vector<std::string>& val);
  321. void push_back(const std::initializer_list<std::string>& val)
  322. {
  323. push_back(std::vector<std::string>(val));
  324. }
  325. void push_back(const Json& val);
  326. void erase(int index);
  327. private:
  328. template <typename T, typename std::enable_if<detail::is_number<T>::value,
  329. bool>::type = true>
  330. void update_arr(T val)
  331. {
  332. json_array_t* arr = json_value_array(parent_);
  333. json_array_insert_before(node_, arr, JSON_VALUE_NUMBER,
  334. static_cast<double>(val));
  335. json_value_t* remove_val = json_array_remove(node_, arr);
  336. json_value_destroy(remove_val);
  337. }
  338. template <typename T,
  339. typename std::enable_if<std::is_same<T, Json::Object>::value ||
  340. std::is_same<T, Json::Array>::value,
  341. bool>::type = true>
  342. void update_arr(const T& val)
  343. {
  344. json_array_t* arr = json_value_array(parent_);
  345. Json copy_json = val.copy();
  346. json_array_insert_before(node_, arr, 0, copy_json.node_);
  347. copy_json.reset();
  348. json_value_t* remove_val = json_array_remove(node_, arr);
  349. json_value_destroy(remove_val);
  350. }
  351. void update_arr(bool val);
  352. void update_arr(std::nullptr_t val);
  353. void update_arr(const std::string& val);
  354. void update_arr(const char* val);
  355. void update_arr(const std::vector<std::string>& val);
  356. void update_arr(const Json& val);
  357. public:
  358. class IteratorBase
  359. {
  360. public:
  361. friend class Json;
  362. explicit IteratorBase(const json_value_t* val)
  363. : val_(val), json_(new Json(nullptr, nullptr, ""))
  364. {
  365. }
  366. ~IteratorBase()
  367. {
  368. if (json_ != nullptr)
  369. {
  370. delete json_;
  371. }
  372. }
  373. IteratorBase(const IteratorBase& iter)
  374. {
  375. val_ = iter.val_;
  376. key_ = iter.key_;
  377. json_ = new Json(iter.json_->node_, iter.json_->parent_,
  378. iter.json_->parent_key_);
  379. }
  380. IteratorBase& operator=(const IteratorBase& iter)
  381. {
  382. if (this == &iter)
  383. {
  384. return *this;
  385. }
  386. val_ = iter.val_;
  387. key_ = iter.key_;
  388. json_->reset(iter.json_->node_, iter.json_->parent_, false,
  389. iter.json_->parent_key_);
  390. return *this;
  391. }
  392. IteratorBase(IteratorBase&& iter)
  393. : val_(iter.val_), key_(iter.key_), json_(iter.json_)
  394. {
  395. iter.val_ = nullptr;
  396. iter.key_ = nullptr;
  397. iter.json_ = nullptr;
  398. }
  399. IteratorBase& operator=(IteratorBase&& iter)
  400. {
  401. if (this == &iter)
  402. {
  403. return *this;
  404. }
  405. val_ = iter.val_;
  406. iter.val_ = nullptr;
  407. key_ = iter.key_;
  408. iter.key_ = nullptr;
  409. delete json_;
  410. json_ = iter.json_;
  411. iter.json_ = nullptr;
  412. return *this;
  413. }
  414. Json& operator*() const
  415. {
  416. if (key_ != nullptr)
  417. {
  418. json_->parent_key_ = std::string(key_);
  419. }
  420. else
  421. {
  422. json_->parent_key_ = "";
  423. }
  424. return *json_;
  425. }
  426. Json* operator->() const
  427. {
  428. if (key_ != nullptr)
  429. {
  430. json_->parent_key_ = std::string(key_);
  431. }
  432. else
  433. {
  434. json_->parent_key_ = "";
  435. }
  436. return json_;
  437. }
  438. bool operator==(const IteratorBase& other) const
  439. {
  440. return json_->node_ == other.json_->node_;
  441. }
  442. bool operator!=(IteratorBase const& other) const
  443. {
  444. return !(*this == other);
  445. }
  446. private:
  447. const json_value_t* val_ = nullptr;
  448. const char* key_ = nullptr;
  449. Json* json_; // cursor
  450. };
  451. class iterator : public IteratorBase
  452. {
  453. public:
  454. friend class Json;
  455. explicit iterator(const json_value_t* val) : IteratorBase(val)
  456. {
  457. }
  458. iterator& operator++()
  459. {
  460. if (is_end())
  461. {
  462. return *this;
  463. }
  464. forward();
  465. return *this;
  466. }
  467. iterator operator++(int)
  468. {
  469. iterator old = (*this);
  470. if (!is_end())
  471. {
  472. ++(*this);
  473. }
  474. return old;
  475. }
  476. private:
  477. void set_begin()
  478. {
  479. json_->node_ = nullptr;
  480. key_ = nullptr;
  481. forward();
  482. }
  483. void set_end()
  484. {
  485. json_->node_ = nullptr;
  486. key_ = nullptr;
  487. }
  488. bool is_end()
  489. {
  490. return json_->node_ == nullptr && key_ == nullptr;
  491. }
  492. void forward()
  493. {
  494. if (json_value_type(val_) == JSON_VALUE_OBJECT)
  495. {
  496. json_object_t* obj = json_value_object(val_);
  497. key_ = json_object_next_name(key_, obj);
  498. json_->node_ = const_cast<json_value_t*>(
  499. json_object_next_value(json_->node_, obj));
  500. }
  501. else if (json_value_type(val_) == JSON_VALUE_ARRAY)
  502. {
  503. json_array_t* arr = json_value_array(val_);
  504. json_->node_ = const_cast<json_value_t*>(
  505. json_array_next_value(json_->node_, arr));
  506. }
  507. }
  508. };
  509. class reverse_iterator : public IteratorBase
  510. {
  511. public:
  512. friend class Json;
  513. explicit reverse_iterator(const json_value_t* val) : IteratorBase(val)
  514. {
  515. }
  516. reverse_iterator& operator++()
  517. {
  518. if (is_rend())
  519. {
  520. return *this;
  521. }
  522. backward();
  523. return *this;
  524. }
  525. reverse_iterator operator++(int)
  526. {
  527. reverse_iterator old = (*this);
  528. if (!is_rend())
  529. {
  530. ++(*this);
  531. }
  532. return old;
  533. }
  534. private:
  535. void set_rbegin()
  536. {
  537. json_->node_ = nullptr;
  538. key_ = nullptr;
  539. backward();
  540. }
  541. void set_rend()
  542. {
  543. json_->node_ = nullptr;
  544. key_ = nullptr;
  545. }
  546. bool is_rend()
  547. {
  548. return json_->node_ == nullptr && key_ == nullptr;
  549. }
  550. void backward()
  551. {
  552. if (json_value_type(val_) == JSON_VALUE_OBJECT)
  553. {
  554. json_object_t* obj = json_value_object(val_);
  555. key_ = json_object_prev_name(key_, obj);
  556. json_->node_ = const_cast<json_value_t*>(
  557. json_object_prev_value(json_->node_, obj));
  558. }
  559. else if (json_value_type(val_) == JSON_VALUE_ARRAY)
  560. {
  561. json_array_t* arr = json_value_array(val_);
  562. json_->node_ = const_cast<json_value_t*>(
  563. json_array_prev_value(json_->node_, arr));
  564. }
  565. }
  566. };
  567. iterator begin() const
  568. {
  569. iterator iter(node_);
  570. iter.set_begin();
  571. return iter;
  572. }
  573. iterator end() const
  574. {
  575. iterator iter(node_);
  576. iter.set_end();
  577. return iter;
  578. }
  579. reverse_iterator rbegin() const
  580. {
  581. reverse_iterator iter(node_);
  582. iter.set_rbegin();
  583. return iter;
  584. }
  585. reverse_iterator rend() const
  586. {
  587. reverse_iterator iter(node_);
  588. iter.set_rend();
  589. return iter;
  590. }
  591. private:
  592. bool can_obj_push_back();
  593. bool can_arr_push_back();
  594. // can convert to any type, just like a placeholder
  595. bool is_placeholder() const
  596. {
  597. return is_null() && parent_ != nullptr;
  598. }
  599. void destroy_node(json_value_t** node)
  600. {
  601. if (allocated_)
  602. {
  603. json_value_destroy(*node);
  604. *node = nullptr;
  605. allocated_ = false;
  606. }
  607. }
  608. void copy(const Json& other);
  609. public:
  610. // Constructors for the various types of JSON value.
  611. // Basic json type Constructors
  612. Json();
  613. Json(const std::string& str);
  614. Json(const char* str);
  615. Json(std::nullptr_t null);
  616. Json(double val);
  617. Json(int val);
  618. Json(bool val);
  619. // For parse
  620. Json(const std::string& str, bool parse_flag);
  621. Json(const Json& json);
  622. Json& operator=(const Json& json);
  623. Json(Json&& other);
  624. Json& operator=(Json&& other);
  625. ~Json();
  626. protected:
  627. // watcher
  628. Json(const json_value_t* node, const json_value_t* parent,
  629. std::string&& key);
  630. Json(const json_value_t* node, const json_value_t* parent,
  631. const std::string& key);
  632. enum class JsonType
  633. {
  634. Object,
  635. Array,
  636. };
  637. // For Object && Array Construct
  638. Json(JsonType type);
  639. bool is_root() const
  640. {
  641. return parent_ == nullptr;
  642. }
  643. bool to_object();
  644. bool to_array();
  645. void reset()
  646. {
  647. node_ = nullptr;
  648. parent_ = nullptr;
  649. allocated_ = false;
  650. parent_key_.clear();
  651. }
  652. void reset(json_value_t* node, const json_value_t* parent, bool allocated,
  653. const std::string& parent_key)
  654. {
  655. node_ = node;
  656. parent_ = parent;
  657. allocated_ = allocated;
  658. parent_key_ = parent_key;
  659. }
  660. void reset(json_value_t* node, const json_value_t* parent, bool allocated,
  661. std::string&& parent_key)
  662. {
  663. node_ = node;
  664. parent_ = parent;
  665. allocated_ = allocated;
  666. parent_key_ = std::move(parent_key);
  667. }
  668. void set_parent(const json_value_t* parent, std::string&& parent_key)
  669. {
  670. parent_ = parent;
  671. parent_key_ = std::move(parent_key);
  672. }
  673. private:
  674. json_value_t* node_ = nullptr;
  675. const json_value_t* parent_ = nullptr;
  676. bool allocated_ = false;
  677. std::string parent_key_;
  678. private:
  679. // for parse
  680. static void value_convert(const json_value_t* val, int spaces, int depth,
  681. std::string* out_str);
  682. static void string_convert(const char* raw_str, std::string* out_str);
  683. static void number_convert(double number, std::string* out_str);
  684. static void array_convert(const json_array_t* arr, int spaces, int depth,
  685. std::string* out_str);
  686. static void array_convert_not_format(const json_array_t* arr,
  687. std::string* out_str);
  688. static void object_convert(const json_object_t* obj, int spaces, int depth,
  689. std::string* out_str);
  690. static void object_convert_not_format(const json_object_t* obj,
  691. std::string* out_str);
  692. friend inline std::ostream& operator<<(std::ostream& os, const Json& json)
  693. {
  694. return (os << json.dump());
  695. }
  696. };
  697. class Object_S : public Json
  698. {
  699. public:
  700. Object_S() : Json(JsonType::Object)
  701. {
  702. }
  703. Object_S(const json_value_t* node, const json_value_t* parent)
  704. : Json(node, parent, "")
  705. {
  706. }
  707. using string_type = std::basic_string<char, std::char_traits<char>,
  708. std::allocator<char>>;
  709. using pair_type = std::pair<string_type, Json>;
  710. Object_S(std::initializer_list<pair_type> list)
  711. {
  712. std::for_each(list.begin(), list.end(),
  713. [this](const pair_type& pair)
  714. { this->push_back(pair.first, pair.second); });
  715. }
  716. };
  717. class Array_S : public Json
  718. {
  719. public:
  720. Array_S() : Json(JsonType::Array)
  721. {
  722. }
  723. Array_S(const json_value_t* node, const json_value_t* parent)
  724. : Json(node, parent, "")
  725. {
  726. }
  727. Array_S(std::initializer_list<Json> list)
  728. {
  729. std::for_each(list.begin(), list.end(),
  730. [this](const Json& js) { this->push_back(js); });
  731. }
  732. };
  733. template <typename T>
  734. typename std::enable_if<std::is_same<T, Json::Object>::value, T>::type
  735. Json::get() const
  736. {
  737. return Json::Object(node_, parent_);
  738. }
  739. template <typename T>
  740. typename std::enable_if<std::is_same<T, Json::Array>::value, T>::type
  741. Json::get() const
  742. {
  743. return Json::Array(node_, parent_);
  744. }
  745. } // namespace wfrest
  746. #endif // WFREST_JSON_H_