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 DISTRIBUTED_RDB_SERIALIZABLE_H 17 #define DISTRIBUTED_RDB_SERIALIZABLE_H 18 #include <string> 19 #include <vector> 20 21 #include "rdb_visibility.h" 22 #ifndef JSON_NOEXCEPTION 23 #define JSON_NOEXCEPTION 24 #endif 25 #include <nlohmann/json.hpp> 26 namespace OHOS { 27 #ifndef GET_NAME 28 #define GET_NAME(value) #value 29 #endif 30 struct Serializable { 31 public: 32 using json = nlohmann::json; 33 using size_type = nlohmann::json::size_type; 34 using error_handler_t = nlohmann::detail::error_handler_t; 35 API_EXPORT json Marshall() const; 36 template<typename T> MarshallSerializable37 static std::string Marshall(T &values) 38 { 39 json root; 40 SetValue(root, values); 41 return root.dump(-1, ' ', false, error_handler_t::replace); 42 } 43 44 API_EXPORT bool Unmarshall(const std::string &jsonStr); 45 template<typename T> UnmarshallSerializable46 static bool Unmarshall(const std::string &body, T &values) 47 { 48 return GetValue(ToJson(body), "", values); 49 } 50 API_EXPORT static json ToJson(const std::string &jsonStr); 51 API_EXPORT static bool IsJson(const std::string &jsonStr); 52 virtual bool Marshal(json &node) const = 0; 53 virtual bool Unmarshal(const json &node) = 0; 54 API_EXPORT static bool GetValue(const json &node, const std::string &name, std::string &value); 55 API_EXPORT static bool GetValue(const json &node, const std::string &name, uint32_t &value); 56 API_EXPORT static bool GetValue(const json &node, const std::string &name, int32_t &value); 57 API_EXPORT static bool GetValue(const json &node, const std::string &name, int64_t &value); 58 API_EXPORT static bool GetValue(const json &node, const std::string &name, uint64_t &value); 59 API_EXPORT static bool GetValue(const json &node, const std::string &name, bool &value); 60 API_EXPORT static bool GetValue(const json &node, const std::string &name, std::vector<uint8_t> &value); 61 API_EXPORT static bool GetValue(const json &node, const std::string &name, Serializable &value); 62 API_EXPORT static bool GetValue(const json &node, const std::string &name, double &value); 63 API_EXPORT static bool SetValue(json &node, const std::string &value); 64 API_EXPORT static bool SetValue(json &node, const uint32_t &value); 65 API_EXPORT static bool SetValue(json &node, const int32_t &value); 66 API_EXPORT static bool SetValue(json &node, const int64_t &value); 67 API_EXPORT static bool SetValue(json &node, const uint64_t &value); 68 API_EXPORT static bool SetValue(json &node, const bool &value); 69 API_EXPORT static bool SetValue(json &node, const std::vector<uint8_t> &value); 70 API_EXPORT static bool SetValue(json &node, const Serializable &value); 71 API_EXPORT static bool SetValue(json &node, const double &value); 72 73 protected: 74 API_EXPORT ~Serializable() = default; 75 76 template<typename T> GetValueSerializable77 static bool GetValue(const json &node, const std::string &name, T *&value) 78 { 79 auto &subNode = GetSubNode(node, name); 80 if (subNode.is_null()) { 81 return false; 82 } 83 value = new (std::nothrow) T(); 84 if (value == nullptr) { 85 return false; 86 } 87 bool result = GetValue(subNode, "", *value); 88 if (!result) { 89 delete value; 90 value = nullptr; 91 } 92 return result; 93 } 94 template<typename T> GetValueSerializable95 static bool GetValue(const json &node, const std::string &name, std::vector<T> &values) 96 { 97 auto &subNode = GetSubNode(node, name); 98 if (subNode.is_null() || !subNode.is_array()) { 99 return false; 100 } 101 bool result = true; 102 values.resize(subNode.size()); 103 for (size_type i = 0; i < subNode.size(); ++i) { 104 result = GetValue(subNode[i], "", values[i]) && result; 105 } 106 return result; 107 } 108 109 template<typename T> SetValueSerializable110 static bool SetValue(json &node, const std::vector<T> &values) 111 { 112 bool result = true; 113 size_type i = 0; 114 node = json::value_t::array; 115 for (const auto &value : values) { 116 result = SetValue(node[i], value) && result; 117 i++; 118 } 119 return result; 120 } 121 122 API_EXPORT static const json &GetSubNode(const json &node, const std::string &name); 123 }; 124 } // namespace OHOS 125 #endif // DISTRIBUTED_RDB_SERIALIZABLE_H 126