json_array.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "json_array.h"
  2. #include <fstream>
  3. #include <sstream>
  4. #include "rapidjson/stringbuffer.h"
  5. #include "rapidjson/allocators.h"
  6. #include "rapidjson/writer.h"
  7. #include "utils/Log.h"
  8. #define JSONARRAY_CHECK_RANGE \
  9. if (rapidjson::SizeType(index) >= doc_.Size()) { \
  10. LOGE("%s index out of range", __FUNCTION__); \
  11. RAPIDJSON_ASSERT(0); \
  12. } \
  13. namespace base {
  14. JSONArray::JSONArray() {
  15. doc_.SetArray();
  16. }
  17. JSONArray::~JSONArray() {
  18. }
  19. JSONArray::JSONArray(const rapidjson::Value::Array& array) {
  20. doc_.SetArray();
  21. for (rapidjson::SizeType i = 0; i < array.Size(); ++i) {
  22. doc_.PushBack(rapidjson::Value(array[i], doc_.GetAllocator()).Move(),
  23. doc_.GetAllocator());
  24. }
  25. }
  26. JSONArray::JSONArray(const rapidjson::Value::ConstArray& array) {
  27. doc_.SetArray();
  28. for (rapidjson::SizeType i = 0; i < array.Size(); ++i) {
  29. doc_.PushBack(rapidjson::Value(array[i], doc_.GetAllocator()).Move(),
  30. doc_.GetAllocator());
  31. }
  32. }
  33. JSONArray::JSONArray(JSONArray&& array) {
  34. doc_.SetArray();
  35. doc_.Swap(array.doc_);
  36. }
  37. int JSONArray::GetInt(int index) {
  38. JSONARRAY_CHECK_RANGE;
  39. return (doc_.Begin()[index]).GetInt();
  40. }
  41. uint32_t JSONArray::GetUint(int index) {
  42. JSONARRAY_CHECK_RANGE;
  43. return (doc_.Begin()[index]).GetUint();
  44. }
  45. std::string JSONArray::GetString(int index) {
  46. JSONARRAY_CHECK_RANGE;
  47. return (doc_.Begin()[index]).GetString();
  48. }
  49. int64_t JSONArray::GetInt64(int index) {
  50. JSONARRAY_CHECK_RANGE;
  51. return (doc_.Begin()[index]).GetInt64();
  52. }
  53. uint64_t JSONArray::GetUint64(int index) {
  54. JSONARRAY_CHECK_RANGE;
  55. return (doc_.Begin()[index]).GetUint64();
  56. }
  57. JSONArray JSONArray::GetArray(int index) {
  58. JSONARRAY_CHECK_RANGE;
  59. if (!doc_[index].IsArray()) {
  60. JSONArray arr;
  61. return arr;
  62. }
  63. return doc_[index].GetArray();
  64. }
  65. JSONObject JSONArray::GetObject(int index) const {
  66. JSONARRAY_CHECK_RANGE;
  67. if (!doc_.Begin()[index].IsObject()) {
  68. JSONObject obj;
  69. return obj;
  70. }
  71. return (doc_.Begin()[index]).GetObject();
  72. }
  73. JSONArray& JSONArray::Add(int32_t value) {
  74. doc_.PushBack(value, doc_.GetAllocator());
  75. return *this;
  76. }
  77. JSONArray& JSONArray::Add(uint32_t value) {
  78. doc_.PushBack(value, doc_.GetAllocator());
  79. return *this;
  80. }
  81. JSONArray& JSONArray::Add(int64_t value) {
  82. doc_.PushBack(value, doc_.GetAllocator());
  83. return *this;
  84. }
  85. JSONArray& JSONArray::Add(uint64_t value) {
  86. doc_.PushBack(value, doc_.GetAllocator());
  87. return *this;
  88. }
  89. JSONArray& JSONArray::Add(bool value) {
  90. doc_.PushBack(value, doc_.GetAllocator());
  91. return *this;
  92. }
  93. JSONArray& JSONArray::Add(const JSONArray& array) {
  94. rapidjson::Value v;
  95. v.CopyFrom(array.doc_, doc_.GetAllocator());
  96. doc_.PushBack(v, doc_.GetAllocator());
  97. return *this;
  98. }
  99. JSONArray& JSONArray::Add(const JSONObject& object) {
  100. rapidjson::Value v;
  101. v.CopyFrom(object.doc_, doc_.GetAllocator());
  102. doc_.PushBack(v, doc_.GetAllocator());
  103. return *this;
  104. }
  105. int JSONArray::Size() const {
  106. return doc_.Size();
  107. }
  108. bool JSONArray::ParseArray(const std::string& json_str) {
  109. doc_.Parse(json_str.c_str());
  110. return !doc_.HasParseError() && doc_.IsArray();
  111. }
  112. bool JSONArray::ParseArrayFromFile(const std::string& file_path) {
  113. std::ifstream ifs(file_path.c_str(), std::ios::in | std::ios::binary);
  114. if (!ifs.is_open()) {
  115. return false;
  116. }
  117. std::stringstream ss;
  118. ss << ifs.rdbuf();
  119. ifs.close();
  120. return ParseArray(ss.str());
  121. }
  122. std::string JSONArray::ToString() const {
  123. return rapidjson_to_string(doc_);
  124. }
  125. std::string JSONArray::ToPrettyString() const {
  126. return rapidjson_to_pretty_string(doc_);
  127. }
  128. } /* namespace rapidjson */