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 namespace OHOS { 23 namespace DistributedHardware { 24 class JsonObject; 25 class JsonItemObject { 26 friend void ToJson(JsonItemObject &itemObject, const std::string &value); 27 friend void ToJson(JsonItemObject &itemObject, const char *value); 28 friend void ToJson(JsonItemObject &itemObject, const double &value); 29 friend void ToJson(JsonItemObject &itemObject, const bool &value); 30 friend void ToJson(JsonItemObject &itemObject, const uint8_t &value); 31 friend void ToJson(JsonItemObject &itemObject, const int16_t &value); 32 friend void ToJson(JsonItemObject &itemObject, const uint16_t &value); 33 friend void ToJson(JsonItemObject &itemObject, const int32_t &value); 34 friend void ToJson(JsonItemObject &itemObject, const uint32_t &value); 35 friend void ToJson(JsonItemObject &itemObject, const int64_t &value); 36 friend void ToJson(JsonItemObject &itemObject, const uint64_t &value); 37 public: 38 JsonItemObject(const JsonItemObject &object); 39 JsonItemObject(const JsonObject &object) = delete; 40 virtual ~JsonItemObject(); 41 bool IsString() const; 42 bool IsNumber() const; 43 bool IsNumberInteger() const; 44 bool IsArray() const; 45 bool IsBoolean() const; 46 bool IsObject() const; 47 JsonItemObject& operator=(const JsonItemObject &object); 48 JsonItemObject& operator=(const JsonObject &object) = delete; 49 JsonItemObject operator[](const std::string &key); 50 const JsonItemObject operator[](const std::string &key) const; 51 std::string DumpFormated() const; 52 std::string Dump() const; 53 bool Contains(const std::string &key) const; 54 bool IsDiscarded() const; 55 bool PushBack(const std::string &strValue); 56 bool PushBack(const double &value); 57 bool PushBack(const int64_t &value); 58 bool PushBack(const JsonItemObject &item); 59 std::string Key() const; 60 void Insert(const std::string &key, const JsonItemObject &object); 61 JsonItemObject At(const std::string &key) const; 62 void GetTo(std::string &strValue) const; 63 void GetTo(double &value) const; 64 void GetTo(int32_t &value) const; 65 void GetTo(uint32_t &value) const; 66 void GetTo(int64_t &value) const; 67 void GetTo(bool &value) const; 68 std::vector<JsonItemObject> Items() const; 69 void Erase(const std::string &key); 70 71 template<typename T> Get()72 T Get() const 73 { 74 T value; 75 FromJson(*this, value); 76 return value; 77 } 78 79 template<typename T> Get(std::vector<T> & dataList)80 void Get(std::vector<T> &dataList) const 81 { 82 dataList.clear(); 83 if (!IsArray()) { 84 return; 85 } 86 for (auto& item : this->Items()) { 87 T element; 88 FromJson(item, element); 89 dataList.push_back(element); 90 } 91 } 92 93 template<typename T> 94 JsonItemObject& operator=(const T &value) 95 { 96 JsonItemObject newItem; 97 if (!InitItem(newItem)) { 98 return *this; 99 } 100 ToJson(newItem, value); 101 if (!ReplaceItem(newItem.item_)) { 102 newItem.needDeleteItem_ = true; 103 return *this; 104 } 105 return *this; 106 } 107 108 template<typename T> 109 JsonItemObject& operator=(const std::vector<T> &values) 110 { 111 if (!InitArray()) { 112 return *this; 113 } 114 for (const auto& value : values) { 115 JsonItemObject operationItem; 116 operationItem.parent_ = item_; 117 operationItem.beValid_ = true; 118 operationItem = value; 119 AddItemToArray(operationItem); 120 } 121 return *this; 122 } 123 protected: 124 JsonItemObject(); 125 void Delete(); 126 std::string Dump(bool formatFlag, bool isIgnoreError) const; 127 void AddItemToArray(JsonItemObject &item); 128 bool InitItem(JsonItemObject &item); 129 bool InitArray(); 130 bool ReplaceItem(void *newItem); 131 protected: 132 void *item_ = nullptr; 133 void *parent_ = nullptr; 134 int32_t itemIndex_ = -1; 135 bool needDeleteItem_ = false; 136 bool beValid_ = false; 137 std::string itemName_ = ""; 138 }; 139 140 enum class JsonCreateType { 141 JSON_CREATE_TYPE_OBJECT = 0, 142 JSON_CREATE_TYPE_ARRAY 143 }; 144 145 class JsonObject : public JsonItemObject { 146 public: 147 using JsonItemObject::operator=; 148 JsonObject(JsonCreateType type = JsonCreateType::JSON_CREATE_TYPE_OBJECT); 149 JsonObject(const std::string &strJson); 150 JsonObject(const JsonObject &object) = delete; 151 JsonObject& operator=(const JsonObject &object) = delete; 152 JsonObject& operator=(const JsonItemObject &object) = delete; 153 ~JsonObject(); 154 bool Parse(const std::string &strJson); 155 void Duplicate(const JsonObject &object); 156 }; 157 158 void ToJson(JsonItemObject &itemObject, const std::string &value); 159 void ToJson(JsonItemObject &itemObject, const char *value); 160 void ToJson(JsonItemObject &itemObject, const double &value); 161 void ToJson(JsonItemObject &itemObject, const bool &value); 162 void ToJson(JsonItemObject &itemObject, const uint8_t &value); 163 void ToJson(JsonItemObject &itemObject, const int16_t &value); 164 void ToJson(JsonItemObject &itemObject, const uint16_t &value); 165 void ToJson(JsonItemObject &itemObject, const int32_t &value); 166 void ToJson(JsonItemObject &itemObject, const uint32_t &value); 167 void ToJson(JsonItemObject &itemObject, const int64_t &value); 168 void ToJson(JsonItemObject &itemObject, const uint64_t &value); 169 void FromJson(const JsonItemObject &itemObject, std::string &value); 170 void FromJson(const JsonItemObject &itemObject, double &value); 171 void FromJson(const JsonItemObject &itemObject, bool &value); 172 void FromJson(const JsonItemObject &itemObject, uint8_t &value); 173 void FromJson(const JsonItemObject &itemObject, int16_t &value); 174 void FromJson(const JsonItemObject &itemObject, uint16_t &value); 175 void FromJson(const JsonItemObject &itemObject, int32_t &value); 176 void FromJson(const JsonItemObject &itemObject, uint32_t &value); 177 void FromJson(const JsonItemObject &itemObject, int64_t &value); 178 void FromJson(const JsonItemObject &itemObject, uint64_t &value); 179 std::string ToString(const JsonItemObject &jsonItem); 180 } // namespace DistributedHardware 181 } // namespace OHOS 182 #endif // JSON_OBJECT_H