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 #include "serializable/serializable.h"
17 namespace OHOS {
18 namespace DistributedData {
Unmarshall(const std::string &jsonStr)19 bool Serializable::Unmarshall(const std::string &jsonStr)
20 {
21 json jsonObj = cJSON_Parse(jsonStr.c_str());
22 auto result = false;
23 if (jsonObj != nullptr) {
24 result = Unmarshal(jsonObj);
25 cJSON_Delete(jsonObj);
26 }
27 return result;
28 }
29
Marshall() const30 std::string Serializable::Marshall() const
31 {
32 json node;
33 Marshal(node);
34 char *value = cJSON_PrintUnformatted(node);
35 std::string result;
36 if (value != nullptr) {
37 result = std::string(value);
38 cJSON_free(value);
39 }
40 return result;
41 }
42
GetValue(const json node, const std::string &name, std::string &value)43 bool Serializable::GetValue(const json node, const std::string &name, std::string &value)
44 {
45 auto subNode = GetSubNode(node, name);
46 if (!cJSON_IsString(subNode)) {
47 return false;
48 }
49 value = cJSON_GetStringValue(subNode);
50 return true;
51 }
52
GetValue(const json node, const std::string &name, uint8_t &value)53 bool Serializable::GetValue(const json node, const std::string &name, uint8_t &value)
54 {
55 return GetNumber(node, name, value);
56 }
57
GetValue(const json node, const std::string &name, uint16_t &value)58 bool Serializable::GetValue(const json node, const std::string &name, uint16_t &value)
59 {
60 return GetNumber(node, name, value);
61 }
62
GetValue(const json node, const std::string &name, uint32_t &value)63 bool Serializable::GetValue(const json node, const std::string &name, uint32_t &value)
64 {
65 return GetNumber(node, name, value);
66 }
67
GetValue(const json node, const std::string &name, uint64_t &value)68 bool Serializable::GetValue(const json node, const std::string &name, uint64_t &value)
69 {
70 return GetNumber(node, name, value);
71 }
72
GetValue(const json node, const std::string &name, int32_t &value)73 bool Serializable::GetValue(const json node, const std::string &name, int32_t &value)
74 {
75 return GetNumber(node, name, value);
76 }
77
GetValue(const json node, const std::string &name, int64_t &value)78 bool Serializable::GetValue(const json node, const std::string &name, int64_t &value)
79 {
80 return GetNumber(node, name, value);
81 }
82
GetValue(const json node, const std::string &name, bool &value)83 bool Serializable::GetValue(const json node, const std::string &name, bool &value)
84 {
85 auto subNode = GetSubNode(node, name);
86 if (!cJSON_IsBool(subNode)) {
87 return false;
88 }
89 value = cJSON_IsTrue(subNode);
90 return true;
91 }
92
GetValue(const json node, const std::string &name, std::vector<uint8_t> &value)93 bool Serializable::GetValue(const json node, const std::string &name, std::vector<uint8_t> &value)
94 {
95 auto subNode = GetSubNode(node, name);
96 if (!cJSON_IsArray(subNode)) {
97 return false;
98 }
99 auto arraySize = cJSON_GetArraySize(subNode);
100 for (int i = 0; i < arraySize; i++) {
101 value.emplace_back(cJSON_GetNumberValue(cJSON_GetArrayItem(subNode, i)));
102 }
103 return true;
104 }
105
GetValue(const json node, const std::string &name, Serializable &value)106 bool Serializable::GetValue(const json node, const std::string &name, Serializable &value)
107 {
108 auto subNode = GetSubNode(node, name);
109 if (!cJSON_IsObject(subNode)) {
110 return false;
111 }
112 return value.Unmarshal(subNode);
113 }
114
SetValue(json &node, const std::string &value, const std::string &name)115 bool Serializable::SetValue(json &node, const std::string &value, const std::string &name)
116 {
117 json subNode = cJSON_CreateString(value.c_str());
118 if (name.empty()) {
119 node = subNode;
120 return true;
121 }
122 SetValueToObject(node, subNode, name);
123 return true;
124 }
125
SetValue(json &node, const uint8_t &value, const std::string &name)126 bool Serializable::SetValue(json &node, const uint8_t &value, const std::string &name)
127 {
128 return SetNumber(node, value, name);
129 }
130
SetValue(json &node, const uint16_t &value, const std::string &name)131 bool Serializable::SetValue(json &node, const uint16_t &value, const std::string &name)
132 {
133 return SetNumber(node, value, name);
134 }
135
SetValue(json &node, const uint32_t &value, const std::string &name)136 bool Serializable::SetValue(json &node, const uint32_t &value, const std::string &name)
137 {
138 return SetNumber(node, value, name);
139 }
140
SetValue(json &node, const uint64_t &value, const std::string &name)141 bool Serializable::SetValue(json &node, const uint64_t &value, const std::string &name)
142 {
143 return SetNumber(node, value, name);
144 }
145
SetValue(json &node, const int32_t &value, const std::string &name)146 bool Serializable::SetValue(json &node, const int32_t &value, const std::string &name)
147 {
148 return SetNumber(node, value, name);
149 }
150
SetValue(json &node, const int64_t &value, const std::string &name)151 bool Serializable::SetValue(json &node, const int64_t &value, const std::string &name)
152 {
153 return SetNumber(node, value, name);
154 }
155
SetValue(json &node, const bool &value, const std::string &name)156 bool Serializable::SetValue(json &node, const bool &value, const std::string &name)
157 {
158 json subNode = cJSON_CreateBool(value);
159 if (name.empty()) {
160 node = subNode;
161 return true;
162 }
163 SetValueToObject(node, subNode, name);
164 return true;
165 }
166
SetValue(json &node, const std::vector<uint8_t> &value, const std::string &name)167 bool Serializable::SetValue(json &node, const std::vector<uint8_t> &value, const std::string &name)
168 {
169 json subNode = cJSON_CreateArray();
170 for (const uint8_t &item : value) {
171 cJSON_AddItemToArray(subNode, cJSON_CreateNumber(item));
172 }
173 if (name.empty()) {
174 node = subNode;
175 return true;
176 }
177 SetValueToObject(node, subNode, name);
178 return true;
179 }
180
SetValue(json &node, const Serializable &value, const std::string &name)181 bool Serializable::SetValue(json &node, const Serializable &value, const std::string &name)
182 {
183 json subNode = nullptr;
184 if (!value.Marshal(subNode)) {
185 cJSON_Delete(subNode);
186 return false;
187 }
188 if (name.empty()) {
189 node = subNode;
190 return true;
191 }
192 SetValueToObject(node, subNode, name);
193 return true;
194 }
195
GetSubNode(const json node, const std::string &name)196 Serializable::json Serializable::GetSubNode(const json node, const std::string &name)
197 {
198 if (cJSON_IsNull(node)) {
199 return nullptr;
200 }
201
202 if (name.empty()) {
203 return node;
204 }
205 if (!cJSON_HasObjectItem(node, name.c_str())) {
206 return nullptr;
207 }
208
209 return cJSON_GetObjectItem(node, name.c_str());
210 }
211 } // namespace DistributedData
212 } // namespace OHOS