json_object.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "json_object.h"
  2. #include <fstream>
  3. #include <sstream>
  4. #include "rapidjson/stringbuffer.h"
  5. #include "rapidjson/allocators.h"
  6. #include "rapidjson/writer.h"
  7. #include "rapidjson/prettywriter.h"
  8. #include "utils/Log.h"
  9. namespace base {
  10. std::string rapidjson_to_string(const rapidjson::Document& doc) {
  11. using namespace rapidjson;
  12. StringBuffer buffer;
  13. Writer<StringBuffer> writer(buffer);
  14. doc.Accept(writer);
  15. return buffer.GetString();
  16. }
  17. std::string rapidjson_to_pretty_string(const rapidjson::Document& doc) {
  18. using namespace rapidjson;
  19. StringBuffer buffer;
  20. PrettyWriter<StringBuffer> writer(buffer);
  21. doc.Accept(writer);
  22. return buffer.GetString();
  23. }
  24. JSONObject::JSONObject() {
  25. doc_.SetObject();
  26. }
  27. JSONObject::~JSONObject() {
  28. }
  29. JSONObject::JSONObject(const rapidjson::Value& value) {
  30. doc_.SetObject();
  31. doc_.CopyFrom(value, doc_.GetAllocator());
  32. }
  33. JSONObject::JSONObject(const rapidjson::Value::Object& obj) {
  34. using namespace rapidjson;
  35. doc_.SetObject();
  36. for (Value::MemberIterator it = obj.MemberBegin(); it != obj.MemberEnd();
  37. ++it) {
  38. doc_.AddMember(Value(it->name, doc_.GetAllocator()).Move(),
  39. Value(it->value, doc_.GetAllocator()).Move(), doc_.GetAllocator());
  40. }
  41. }
  42. JSONObject::JSONObject(const rapidjson::Value::ConstObject& obj) {
  43. using namespace rapidjson;
  44. doc_.SetObject();
  45. for (Value::ConstMemberIterator it = obj.MemberBegin(); it != obj.MemberEnd();
  46. ++it) {
  47. doc_.AddMember(Value(it->name, doc_.GetAllocator()).Move(),
  48. Value(it->value, doc_.GetAllocator()).Move(), doc_.GetAllocator());
  49. }
  50. }
  51. JSONObject::JSONObject(JSONObject&& obj) {
  52. doc_.SetObject();
  53. doc_.Swap(obj.doc_);
  54. }
  55. JSONObject::JSONObject(const JSONObject& obj) {
  56. doc_.SetObject();
  57. doc_.CopyFrom(obj.doc_, doc_.GetAllocator());
  58. }
  59. JSONObject& JSONObject::Put(const std::string& name, int32_t v) {
  60. using namespace rapidjson;
  61. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  62. Value().SetInt(v), doc_.GetAllocator());
  63. return *this;
  64. }
  65. JSONObject& JSONObject::Put(const std::string& name, uint32_t v) {
  66. using namespace rapidjson;
  67. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  68. Value().SetUint(v), doc_.GetAllocator());
  69. return *this;
  70. }
  71. JSONObject& JSONObject::Put(const std::string& name, int64_t v) {
  72. using namespace rapidjson;
  73. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  74. Value().SetInt64(v), doc_.GetAllocator());
  75. return *this;
  76. }
  77. JSONObject& JSONObject::Put(const std::string& name, uint64_t v) {
  78. using namespace rapidjson;
  79. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  80. Value().SetUint64(v), doc_.GetAllocator());
  81. return *this;
  82. }
  83. JSONObject& JSONObject::Put(const std::string& name, const std::string& v) {
  84. using namespace rapidjson;
  85. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  86. Value().SetString(v.c_str(), doc_.GetAllocator()), doc_.GetAllocator());
  87. return *this;
  88. }
  89. JSONObject& JSONObject::Put(const std::string& name, const JSONObject& v) {
  90. using namespace rapidjson;
  91. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  92. Value(v.doc_, doc_.GetAllocator()).Move(), doc_.GetAllocator());
  93. return *this;
  94. }
  95. JSONObject& JSONObject::Put(const std::string& name, const JSONArray& v) {
  96. using namespace rapidjson;
  97. doc_.AddMember(Value(name.c_str(), doc_.GetAllocator()).Move(),
  98. Value(v.doc_, doc_.GetAllocator()).Move(), doc_.GetAllocator());
  99. return *this;
  100. }
  101. bool JSONObject::ParseObject(const std::string& json_str) {
  102. doc_.Parse(json_str.c_str());
  103. return !doc_.HasParseError() && doc_.IsObject();
  104. }
  105. bool JSONObject::ParseObjectFromFile(const std::string& path) {
  106. std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
  107. if (!ifs.is_open()) {
  108. return false;
  109. }
  110. std::stringstream ss;
  111. ss << ifs.rdbuf();
  112. ifs.close();
  113. return ParseObject(ss.str());
  114. }
  115. std::string JSONObject::ToString() const {
  116. return rapidjson_to_string(doc_);
  117. }
  118. std::string JSONObject::ToPrettyString() const {
  119. return rapidjson_to_pretty_string(doc_);
  120. }
  121. /**
  122. * if not found, throw std::string
  123. */
  124. int JSONObject::GetInt(const std::string& name) {
  125. if (doc_.HasMember(name.c_str())) {
  126. return doc_[name.c_str()].GetInt();
  127. }
  128. throw name;
  129. }
  130. /**
  131. * if not found, throw std::string
  132. */
  133. uint32_t JSONObject::GetUint(const std::string& name) {
  134. if (doc_.HasMember(name.c_str())) {
  135. return doc_[name.c_str()].GetUint();
  136. }
  137. throw name;
  138. }
  139. uint32_t JSONObject::GetUint(const std::string& name, uint32_t default_value) {
  140. if (doc_.HasMember(name.c_str())) {
  141. return doc_[name.c_str()].GetUint();
  142. }
  143. return default_value;
  144. }
  145. int JSONObject::GetInt(const std::string& name, int default_value) {
  146. if (doc_.HasMember(name.c_str()) && doc_[name].IsInt()) {
  147. return doc_[name.c_str()].GetInt();
  148. }
  149. return default_value;
  150. }
  151. int64_t JSONObject::GetInt64(const std::string& name, int64_t default_value) {
  152. if (doc_.HasMember(name.c_str())) {
  153. return doc_[name.c_str()].GetInt64();
  154. }
  155. return default_value;
  156. }
  157. /**
  158. * if not found, throw std::string
  159. */
  160. std::string JSONObject::GetString(const std::string& name) {
  161. if (doc_.HasMember(name.c_str())) {
  162. return doc_[name.c_str()].GetString();
  163. }
  164. throw name;
  165. }
  166. std::string JSONObject::GetString(const std::string& name,
  167. const std::string& default_value) {
  168. if (doc_.HasMember(name.c_str()) && doc_[name].IsString()) {
  169. return doc_[name.c_str()].GetString();
  170. }
  171. return default_value;
  172. }
  173. bool JSONObject::GetBool(const std::string& name, bool default_value) {
  174. if (doc_.HasMember(name) && doc_[name].IsBool()) {
  175. return doc_[name].GetBool();
  176. }
  177. return default_value;
  178. }
  179. /**
  180. * if not found, return empty array
  181. */
  182. JSONArray JSONObject::GetArray(const std::string& name) const {
  183. if (doc_.HasMember(name.c_str()) && doc_[name.c_str()].IsArray()
  184. && !doc_[name].IsNull()) {
  185. return (doc_[name.c_str()].GetArray());
  186. }
  187. JSONArray arr;
  188. return arr;
  189. }
  190. /**
  191. * if not found, return empty object
  192. */
  193. JSONObject JSONObject::GetObject(const std::string& name) {
  194. if (doc_.HasMember(name) && doc_[name].IsObject() && !doc_[name].IsNull()) {
  195. return doc_[name];
  196. }
  197. JSONObject obj;
  198. return obj;
  199. }
  200. }