1 /* 2 * Copyright (c) 2021 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_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H 17 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H 18 #include <string> 19 #include <vector> 20 #include "visibility.h" 21 #ifndef JSON_NOEXCEPTION 22 #define JSON_NOEXCEPTION 23 #endif 24 #include <nlohmann/json.hpp> 25 namespace OHOS { 26 namespace DistributedData { 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 virtual bool Marshal(json &node) const = 0; 52 virtual bool Unmarshal(const json &node) = 0; 53 API_EXPORT static bool GetValue(const json &node, const std::string &name, std::string &value); 54 API_EXPORT static bool GetValue(const json &node, const std::string &name, uint32_t &value); 55 API_EXPORT static bool GetValue(const json &node, const std::string &name, int32_t &value); 56 API_EXPORT static bool GetValue(const json &node, const std::string &name, int64_t &value); 57 API_EXPORT static bool GetValue(const json &node, const std::string &name, bool &value); 58 API_EXPORT static bool GetValue(const json &node, const std::string &name, std::vector<uint8_t> &value); 59 API_EXPORT static bool GetValue(const json &node, const std::string &name, Serializable &value); 60 API_EXPORT static bool SetValue(json &node, const std::string &value); 61 API_EXPORT static bool SetValue(json &node, const uint32_t &value); 62 API_EXPORT static bool SetValue(json &node, const int32_t &value); 63 API_EXPORT static bool SetValue(json &node, const int64_t &value); 64 API_EXPORT static bool SetValue(json &node, const bool &value); 65 API_EXPORT static bool SetValue(json &node, const std::vector<uint8_t> &value); 66 API_EXPORT static bool SetValue(json &node, const Serializable &value); 67 protected: 68 API_EXPORT ~Serializable() = default; 69 70 template<typename T> GetValueSerializable71 static bool GetValue(const json &node, const std::string &name, T *&value) 72 { 73 auto &subNode = GetSubNode(node, name); 74 if (subNode.is_null()) { 75 return false; 76 } 77 value = new(std::nothrow) T(); 78 if (value == nullptr) { 79 return false; 80 } 81 bool result = GetValue(subNode, "", *value); 82 if (!result) { 83 delete value; 84 value = nullptr; 85 } 86 return result; 87 } 88 template<typename T> GetValueSerializable89 static bool GetValue(const json &node, const std::string &name, std::vector<T> &values) 90 { 91 auto &subNode = GetSubNode(node, name); 92 if (subNode.is_null() || !subNode.is_array()) { 93 return false; 94 } 95 bool result = true; 96 values.resize(subNode.size()); 97 for (size_type i = 0; i < subNode.size(); ++i) { 98 result = GetValue(subNode[i], "", values[i]) && result; 99 } 100 return result; 101 } 102 103 template<typename T> SetValueSerializable104 static bool SetValue(json &node, const std::vector<T> &values) 105 { 106 bool result = true; 107 size_type i = 0; 108 node = json::value_t::array; 109 for (const auto &value : values) { 110 result = SetValue(node[i], value) && result; 111 i++; 112 } 113 return result; 114 } 115 116 API_EXPORT static const json &GetSubNode(const json &node, const std::string &name); 117 }; 118 } // namespace DistributedData 119 } // namespace OHOS 120 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H 121