Json.cc 22 KB

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