json_array.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef _JSON_ARRAY_H_
  2. #define _JSON_ARRAY_H_
  3. #include "rapidjson/document.h"
  4. #include "json_object.h"
  5. namespace base {
  6. class JSONObject;
  7. class JSONArray {
  8. public:
  9. JSONArray();
  10. virtual ~JSONArray();
  11. JSONArray(const rapidjson::Value::Array& array);
  12. JSONArray(const rapidjson::Value::ConstArray& array);
  13. JSONArray(JSONArray&& array);
  14. int GetInt(int index);
  15. uint32_t GetUint(int index);
  16. std::string GetString(int index);
  17. int64_t GetInt64(int index);
  18. uint64_t GetUint64(int index);
  19. JSONArray GetArray(int index);
  20. JSONObject GetObject(int index) const ;
  21. JSONArray& Add(int32_t value);
  22. JSONArray& Add(uint32_t value);
  23. JSONArray& Add(int64_t value);
  24. JSONArray& Add(uint64_t value);
  25. JSONArray& Add(bool value);
  26. JSONArray& Add(const JSONArray& array);
  27. JSONArray& Add(const JSONObject& object);
  28. template <typename T>
  29. JSONArray& Add(const T value) {
  30. rapidjson::Value tmp(value, doc_.GetAllocator());
  31. doc_.PushBack(tmp,
  32. doc_.GetAllocator());
  33. return *this;
  34. }
  35. int Size() const;
  36. bool ParseArray(const std::string& json_str);
  37. bool ParseArrayFromFile(const std::string& file_path);
  38. std::string ToString() const;
  39. std::string ToPrettyString() const;
  40. private:
  41. friend class JSONObject;
  42. rapidjson::Document doc_;
  43. };
  44. } /* namespace rapidjson */
  45. #endif