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 SetValue(json &node, const std::string &value);
63     API_EXPORT static bool SetValue(json &node, const uint32_t &value);
64     API_EXPORT static bool SetValue(json &node, const int32_t &value);
65     API_EXPORT static bool SetValue(json &node, const int64_t &value);
66     API_EXPORT static bool SetValue(json &node, const uint64_t &value);
67     API_EXPORT static bool SetValue(json &node, const bool &value);
68     API_EXPORT static bool SetValue(json &node, const std::vector<uint8_t> &value);
69     API_EXPORT static bool SetValue(json &node, const Serializable &value);
70 
71 protected:
72     API_EXPORT ~Serializable() = default;
73 
74     template<typename T>
GetValueSerializable75     static bool GetValue(const json &node, const std::string &name, T *&value)
76     {
77         auto &subNode = GetSubNode(node, name);
78         if (subNode.is_null()) {
79             return false;
80         }
81         value = new (std::nothrow) T();
82         if (value == nullptr) {
83             return false;
84         }
85         bool result = GetValue(subNode, "", *value);
86         if (!result) {
87             delete value;
88             value = nullptr;
89         }
90         return result;
91     }
92     template<typename T>
GetValueSerializable93     static bool GetValue(const json &node, const std::string &name, std::vector<T> &values)
94     {
95         auto &subNode = GetSubNode(node, name);
96         if (subNode.is_null() || !subNode.is_array()) {
97             return false;
98         }
99         bool result = true;
100         values.resize(subNode.size());
101         for (size_type i = 0; i < subNode.size(); ++i) {
102             result = GetValue(subNode[i], "", values[i]) && result;
103         }
104         return result;
105     }
106 
107     template<typename T>
SetValueSerializable108     static bool SetValue(json &node, const std::vector<T> &values)
109     {
110         bool result = true;
111         size_type i = 0;
112         node = json::value_t::array;
113         for (const auto &value : values) {
114             result = SetValue(node[i], value) && result;
115             i++;
116         }
117         return result;
118     }
119 
120     API_EXPORT static const json &GetSubNode(const json &node, const std::string &name);
121 };
122 } // namespace OHOS
123 #endif // DISTRIBUTED_RDB_SERIALIZABLE_H
124