json_object.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef _JSON_OBJECT_H_
  2. #define _JSON_OBJECT_H_
  3. #include "json_array.h"
  4. namespace base {
  5. std::string rapidjson_to_string(const rapidjson::Document& doc);
  6. std::string rapidjson_to_pretty_string(const rapidjson::Document& doc);
  7. class JSONArray;
  8. class JSONObject {
  9. public:
  10. JSONObject();
  11. virtual ~JSONObject();
  12. JSONObject(const rapidjson::Value& value);
  13. JSONObject(const rapidjson::Value::Object& obj);
  14. JSONObject(const rapidjson::Value::ConstObject& obj);
  15. JSONObject(JSONObject&& obj);
  16. JSONObject(const JSONObject& obj);
  17. JSONObject& Put(const std::string& name, int32_t v);
  18. JSONObject& Put(const std::string& name, uint32_t v);
  19. JSONObject& Put(const std::string& name, int64_t v);
  20. JSONObject& Put(const std::string& name, uint64_t v);
  21. JSONObject& Put(const std::string& name, const std::string& v);
  22. JSONObject& Put(const std::string& name, const JSONObject& v);
  23. JSONObject& Put(const std::string& name, const JSONArray& v);
  24. bool ParseObject(const std::string& json_str);
  25. bool ParseObjectFromFile(const std::string& path);
  26. std::string ToString() const;
  27. std::string ToPrettyString() const;
  28. /**
  29. * if not found, throw std::string
  30. */
  31. int GetInt(const std::string& name);
  32. /**
  33. * if not found, throw std::string
  34. */
  35. uint32_t GetUint(const std::string& name);
  36. uint32_t GetUint(const std::string& name, uint32_t default_value);
  37. int GetInt(const std::string& name, int default_value);
  38. int64_t GetInt64(const std::string& name, int64_t default_value);
  39. /**
  40. * if not found, throw std::string
  41. */
  42. std::string GetString(const std::string& name);
  43. std::string GetString(const std::string& name,
  44. const std::string& default_value);
  45. bool GetBool(const std::string& name, bool default_value);
  46. /**
  47. * if not found, return empty array
  48. */
  49. JSONArray GetArray(const std::string& name) const;
  50. /**
  51. * if not found, return empty object
  52. */
  53. JSONObject GetObject(const std::string& name);
  54. private:
  55. friend class JSONArray;
  56. rapidjson::Document doc_;
  57. };
  58. }
  59. /* namespace json */
  60. #endif