1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef JSON_OBJECT_H 17 #define JSON_OBJECT_H 18 19 #include <string> 20 #include <vector> 21 22 #include "cJSON.h" 23 #include "dm_log.h" 24 25 namespace OHOS { 26 namespace DistributedHardware { 27 class JsonObject; 28 class JsonItemObject { 29 friend void ToJson(JsonItemObject &itemObject, const std::string &value); 30 friend void ToJson(JsonItemObject &itemObject, const char *value); 31 friend void ToJson(JsonItemObject &itemObject, const double &value); 32 friend void ToJson(JsonItemObject &itemObject, const bool &value); 33 friend void ToJson(JsonItemObject &itemObject, const uint8_t &value); 34 friend void ToJson(JsonItemObject &itemObject, const int16_t &value); 35 friend void ToJson(JsonItemObject &itemObject, const uint16_t &value); 36 friend void ToJson(JsonItemObject &itemObject, const int32_t &value); 37 friend void ToJson(JsonItemObject &itemObject, const uint32_t &value); 38 friend void ToJson(JsonItemObject &itemObject, const int64_t &value); 39 friend void ToJson(JsonItemObject &itemObject, const uint64_t &value); 40 public: 41 JsonItemObject(const JsonItemObject &object); 42 JsonItemObject(const JsonObject &object) = delete; 43 virtual ~JsonItemObject(); 44 bool IsString() const; 45 bool IsNumber() const; 46 bool IsNumberInteger() const; 47 bool IsArray() const; 48 bool IsBoolean() const; 49 bool IsObject() const; 50 JsonItemObject& operator=(const JsonItemObject &object); 51 JsonItemObject& operator=(const JsonObject &object) = delete; 52 JsonItemObject operator[](const std::string &key); 53 const JsonItemObject operator[](const std::string &key) const; 54 std::string DumpFormated() const; 55 std::string Dump() const; 56 bool Contains(const std::string &key) const; 57 bool IsDiscarded() const; 58 bool PushBack(const std::string &strValue); 59 bool PushBack(const double &strValue); 60 bool PushBack(const JsonItemObject &item); 61 std::string Key() const; 62 void Insert(const std::string &key, const JsonItemObject &object); 63 JsonItemObject At(const std::string &key) const; 64 void GetTo(std::string &strValue) const; 65 void GetTo(double &value) const; 66 void GetTo(int32_t &value) const; 67 void GetTo(uint32_t &value) const; 68 void GetTo(int64_t &value) const; 69 void GetTo(bool &value) const; 70 std::vector<JsonItemObject> Items() const; 71 void Erase(const std::string &key); 72 73 template<typename T> Get()74 T Get() const 75 { 76 T value; 77 FromJson(*this, value); 78 return value; 79 } 80 81 template<typename T> Get(std::vector<T> & dataList)82 void Get(std::vector<T> &dataList) const 83 { 84 dataList.clear(); 85 if (!IsArray()) { 86 LOGE("item is not array"); 87 return; 88 } 89 for (auto& item : this->Items()) { 90 T element; 91 FromJson(item, element); 92 dataList.push_back(element); 93 } 94 } 95 96 template<typename T> 97 JsonItemObject& operator=(const T &value) 98 { 99 JsonItemObject newItem; 100 if (!InitItem(newItem)) { 101 return *this; 102 } 103 ToJson(newItem, value); 104 if (!ReplaceItem(newItem.item_)) { 105 newItem.needDeleteItem_ = true; 106 return *this; 107 } 108 return *this; 109 } 110 111 template<typename T> 112 JsonItemObject& operator=(const std::vector<T> &values) 113 { 114 if (!InitArray()) { 115 return *this; 116 } 117 for (const auto& value : values) { 118 JsonItemObject operationItem; 119 operationItem.parent_ = item_; 120 operationItem.beValid_ = true; 121 operationItem = value; 122 if (cJSON_AddItemToArray(item_, operationItem.item_)) { 123 operationItem.itemIndex_ = cJSON_GetArraySize(item_) - 1; 124 } else if (operationItem.item_ != nullptr) { 125 // item add fail, need delete 126 operationItem.needDeleteItem_ = true; 127 } 128 } 129 return *this; 130 } 131 protected: 132 JsonItemObject(); 133 void Delete(); 134 std::string Dump(bool formatFlag) const; 135 bool AddToArray(cJSON *newItem); 136 bool InitItem(JsonItemObject &item); 137 bool InitArray(); 138 bool ReplaceItem(cJSON *newItem); 139 protected: 140 cJSON *item_ = nullptr; 141 cJSON *parent_ = nullptr; 142 int32_t itemIndex_ = -1; 143 bool needDeleteItem_ = false; 144 bool beValid_ = false; 145 std::string itemName_ = ""; 146 }; 147 148 enum class JsonCreateType { 149 JSON_CREATE_TYPE_OBJECT = 0, 150 JSON_CREATE_TYPE_ARRAY 151 }; 152 153 class JsonObject : public JsonItemObject { 154 public: 155 using JsonItemObject::operator=; 156 JsonObject(JsonCreateType type = JsonCreateType::JSON_CREATE_TYPE_OBJECT); 157 JsonObject(const std::string &strJson); 158 JsonObject(const JsonObject &object) = delete; 159 JsonObject& operator=(const JsonObject &object) = delete; 160 JsonObject& operator=(const JsonItemObject &object) = delete; 161 ~JsonObject(); 162 bool Parse(const std::string &strJson); 163 void Duplicate(const JsonObject &object); 164 }; 165 166 void ToJson(JsonItemObject &itemObject, const std::string &value); 167 void ToJson(JsonItemObject &itemObject, const char *value); 168 void ToJson(JsonItemObject &itemObject, const double &value); 169 void ToJson(JsonItemObject &itemObject, const bool &value); 170 void ToJson(JsonItemObject &itemObject, const uint8_t &value); 171 void ToJson(JsonItemObject &itemObject, const int16_t &value); 172 void ToJson(JsonItemObject &itemObject, const uint16_t &value); 173 void ToJson(JsonItemObject &itemObject, const int32_t &value); 174 void ToJson(JsonItemObject &itemObject, const uint32_t &value); 175 void ToJson(JsonItemObject &itemObject, const int64_t &value); 176 void ToJson(JsonItemObject &itemObject, const uint64_t &value); 177 void FromJson(const JsonItemObject &itemObject, std::string &value); 178 void FromJson(const JsonItemObject &itemObject, double &value); 179 void FromJson(const JsonItemObject &itemObject, bool &value); 180 void FromJson(const JsonItemObject &itemObject, uint8_t &value); 181 void FromJson(const JsonItemObject &itemObject, int16_t &value); 182 void FromJson(const JsonItemObject &itemObject, uint16_t &value); 183 void FromJson(const JsonItemObject &itemObject, int32_t &value); 184 void FromJson(const JsonItemObject &itemObject, uint32_t &value); 185 void FromJson(const JsonItemObject &itemObject, int64_t &value); 186 void FromJson(const JsonItemObject &itemObject, uint64_t &value); 187 std::string ToString(const JsonItemObject &jsonItem); 188 } // namespace DistributedHardware 189 } // namespace OHOS 190 #endif // JSON_OBJECT_H