1 /* 2 * Copyright (c) 2023 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 <memory> 20 #include <set> 21 #include <string> 22 #include <typeinfo> 23 #include <vector> 24 25 #include "cJSON.h" 26 27 namespace DocumentDB { 28 class ValueObject { 29 public: 30 enum class ValueType { 31 VALUE_NULL = 0, 32 VALUE_BOOL, 33 VALUE_NUMBER, 34 VALUE_STRING, 35 }; 36 ValueObject() = default; 37 explicit ValueObject(bool val); 38 explicit ValueObject(double val); 39 explicit ValueObject(const char *val); 40 41 ValueType GetValueType() const; 42 bool GetBoolValue() const; 43 int64_t GetIntValue() const; 44 double GetDoubleValue() const; 45 std::string GetStringValue() const; 46 47 private: 48 ValueType valueType = ValueType::VALUE_NULL; 49 union { 50 bool boolValue; 51 double doubleValue; 52 }; 53 std::string stringValue; 54 }; 55 using JsonFieldPath = std::vector<std::string>; 56 57 using ResultValue = ValueObject; 58 using JsonFieldPath = std::vector<std::string>; 59 60 class JsonObject { 61 public: 62 static JsonObject Parse(const std::string &jsonStr, int &errCode, bool caseSensitive = false, 63 bool isFilter = false); 64 bool operator==(const JsonObject &other) const; // If the two nodes exist with a different fieldName, then return 0. 65 ~JsonObject(); 66 67 std::string Print() const; 68 69 JsonObject GetObjectItem(const std::string &field, int &errCode); 70 71 JsonObject GetNext() const; 72 JsonObject GetChild() const; 73 74 int DeleteItemFromObject(const std::string &field); 75 int AddItemToObject(const std::string &fieldName, const JsonObject &item); 76 int AddItemToObject(const std::string &fieldName); 77 78 ValueObject GetItemValue() const; 79 void ReplaceItemInArray(const int &index, const JsonObject &newItem, int &errCode); 80 void ReplaceItemInObject(const std::string &fieldName, const JsonObject &newItem, int &errCode); 81 int InsertItemObject(int which, const JsonObject &newItem); 82 83 std::string GetItemField() const; 84 std::string GetItemField(int &errCode) const; 85 86 bool IsFieldExists(const JsonFieldPath &jsonPath) const; 87 bool IsFieldExistsPowerMode(const JsonFieldPath &jsonPath) const; 88 JsonObject FindItem(const JsonFieldPath &jsonPath, int &errCode) const; 89 JsonObject FindItemPowerMode(const JsonFieldPath &jsonPath, int &errCode) const; 90 ValueObject GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const; 91 int DeleteItemDeeplyOnTarget(const JsonFieldPath &path); 92 bool IsNull() const; 93 int GetDeep(); 94 enum class Type { 95 JSON_LEAF, // Corresponds to nodes of type null, number, string, true false in CJSON 96 JSON_OBJECT, 97 JSON_ARRAY 98 }; 99 Type GetType() const; 100 101 private: 102 JsonObject(); 103 int Init(const std::string &str, bool isFilter = false); 104 int CheckJsonRepeatField(cJSON *object, bool isFirstFloor); 105 int CheckSubObj(std::set<std::string> &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor); 106 int GetDeep(cJSON *cjson); 107 int CheckNumber(cJSON *cJSON); 108 cJSON *cjson_ = nullptr; 109 int jsonDeep_ = 0; 110 bool isOwner_ = false; 111 bool caseSensitive_ = false; 112 }; 113 } // namespace DocumentDB 114 #endif // JSON_OBJECT_H 115