1 /* 2 * Copyright (c) 2022-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 OHOS_DISTRIBUTED_SERIALIZABLE_SERIALIZABLE_H 17 #define OHOS_DISTRIBUTED_SERIALIZABLE_SERIALIZABLE_H 18 #include <string> 19 20 #include "api/visibility.h" 21 #ifndef JSON_NOEXCEPTION 22 #define JSON_NOEXCEPTION 23 #endif 24 #include "cJSON.h" 25 26 namespace OHOS { 27 namespace DistributedData { 28 #ifndef GET_NAME 29 #define GET_NAME(value) #value 30 #endif 31 struct Serializable { 32 public: 33 using json = cJSON *; 34 API_EXPORT bool Unmarshall(const std::string &jsonStr); 35 API_EXPORT std::string Marshall() const; 36 virtual bool Marshal(json &node) const = 0; 37 virtual bool Unmarshal(const json &node) = 0; 38 API_EXPORT static bool GetValue(const json node, const std::string &name, std::string &value); 39 API_EXPORT static bool GetValue(const json node, const std::string &name, uint8_t &value); 40 API_EXPORT static bool GetValue(const json node, const std::string &name, uint16_t &value); 41 API_EXPORT static bool GetValue(const json node, const std::string &name, uint32_t &value); 42 API_EXPORT static bool GetValue(const json node, const std::string &name, uint64_t &value); 43 API_EXPORT static bool GetValue(const json node, const std::string &name, int32_t &value); 44 API_EXPORT static bool GetValue(const json node, const std::string &name, int64_t &value); 45 API_EXPORT static bool GetValue(const json node, const std::string &name, bool &value); 46 API_EXPORT static bool GetValue(const json node, const std::string &name, std::vector<uint8_t> &value); 47 API_EXPORT static bool GetValue(const json node, const std::string &name, Serializable &value); 48 API_EXPORT static bool SetValue(json &node, const std::string &value, const std::string &name = ""); 49 API_EXPORT static bool SetValue(json &node, const uint8_t &value, const std::string &name = ""); 50 API_EXPORT static bool SetValue(json &node, const uint16_t &value, const std::string &name = ""); 51 API_EXPORT static bool SetValue(json &node, const uint32_t &value, const std::string &name = ""); 52 API_EXPORT static bool SetValue(json &node, const uint64_t &value, const std::string &name = ""); 53 API_EXPORT static bool SetValue(json &node, const int32_t &value, const std::string &name = ""); 54 API_EXPORT static bool SetValue(json &node, const int64_t &value, const std::string &name = ""); 55 API_EXPORT static bool SetValue(json &node, const bool &value, const std::string &name = ""); 56 API_EXPORT static bool SetValue(json &node, const std::vector<uint8_t> &value, const std::string &name = ""); 57 API_EXPORT static bool SetValue(json &node, const Serializable &value, const std::string &name = ""); 58 59 template<typename T> 60 static bool SetValue(json &node, const std::vector<T> &values, const std::string &name = "") 61 { 62 bool result = true; 63 auto subNode = cJSON_CreateArray(); 64 for (const auto &value : values) { 65 json item = nullptr; 66 result = SetValue(item, value, "") && result; 67 cJSON_AddItemToArray(subNode, item); 68 } 69 70 if (name.empty()) { 71 node = subNode; 72 return result; 73 } 74 SetValueToObject(node, subNode, name); 75 return result; 76 } 77 78 protected: 79 API_EXPORT ~Serializable() = default; 80 81 template<typename T> GetValueSerializable82 static bool GetValue(const json node, const std::string &name, std::vector<T> &values) 83 { 84 auto subNode = GetSubNode(node, name); 85 if (!cJSON_IsArray(subNode)) { 86 return false; 87 } 88 bool result = true; 89 auto arraySize = cJSON_GetArraySize(subNode); 90 values.resize(arraySize); 91 for (int i = 0; i < arraySize; ++i) { 92 result = GetValue(cJSON_GetArrayItem(subNode, i), "", values[i]) && result; 93 } 94 return result; 95 } 96 97 template<typename T> SetNumberSerializable98 static bool SetNumber(json &node, const T &value, const std::string &name) 99 { 100 json subNode = cJSON_CreateNumber(value); 101 if (name.empty()) { 102 node = subNode; 103 return true; 104 } 105 SetValueToObject(node, subNode, name); 106 return true; 107 } 108 109 template<typename T> GetNumberSerializable110 static bool GetNumber(const json node, const std::string &name, T &value) 111 { 112 auto subNode = GetSubNode(node, name); 113 if (!cJSON_IsNumber(subNode)) { 114 return false; 115 } 116 value = cJSON_GetNumberValue(subNode); 117 return true; 118 } 119 120 API_EXPORT static json GetSubNode(const json node, const std::string &name); SetValueToObjectSerializable121 static void SetValueToObject(json &node, json subNode, const std::string &name) 122 { 123 if (!cJSON_IsObject(node)) { 124 node = cJSON_CreateObject(); 125 } 126 if (cJSON_HasObjectItem(node, name.c_str())) { 127 cJSON_ReplaceItemInObject(node, name.c_str(), subNode); 128 } else { 129 cJSON_AddItemToObject(node, name.c_str(), subNode); 130 } 131 } 132 }; 133 } // namespace DistributedData 134 } // namespace OHOS 135 #endif // OHOS_DISTRIBUTED_SERIALIZABLE_SERIALIZABLE_H 136