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 #include "serializable/serializable.h"
17 namespace OHOS {
18 namespace DistributedData {
Marshall() const19 Serializable::json Serializable::Marshall() const
20 {
21     json root;
22     Marshal(root);
23     return root;
24 }
25 
Unmarshall(const std::string & jsonStr)26 bool Serializable::Unmarshall(const std::string &jsonStr)
27 {
28     json jsonObj = json::parse(jsonStr, nullptr, false);
29     if (jsonObj.is_discarded()) {
30         // if the string size is less than 1, means the string is invalid.
31         if (jsonStr.empty()) {
32             return false;
33         }
34         jsonObj = json::parse(jsonStr.substr(1), nullptr, false); // drop first char to adapt A's value;
35         if (jsonObj.is_discarded()) {
36             return false;
37         }
38     }
39     return Unmarshal(jsonObj);
40 }
41 
ToJson(const std::string & jsonStr)42 Serializable::json Serializable::ToJson(const std::string &jsonStr)
43 {
44     json jsonObj = json::parse(jsonStr, nullptr, false);
45     if (jsonObj.is_discarded()) {
46         // if the string size is less than 1, means the string is invalid.
47         if (jsonStr.empty()) {
48             return {};
49         }
50         jsonObj = json::parse(jsonStr.substr(1), nullptr, false); // drop first char to adapt A's value;
51         if (jsonObj.is_discarded()) {
52             return {};
53         }
54     }
55     return jsonObj;
56 }
57 
IsJson(const std::string & jsonStr)58 bool Serializable::IsJson(const std::string &jsonStr)
59 {
60     if (!json::accept(jsonStr)) {
61         return json::accept(jsonStr.begin() + 1, jsonStr.end());
62     }
63     return true;
64 }
65 
GetValue(const json & node,const std::string & name,std::string & value)66 bool Serializable::GetValue(const json &node, const std::string &name, std::string &value)
67 {
68     auto &subNode = GetSubNode(node, name);
69     if (subNode.is_null() || !subNode.is_string()) {
70         return false;
71     }
72     value = subNode;
73     return true;
74 }
75 
GetValue(const json & node,const std::string & name,uint32_t & value)76 bool Serializable::GetValue(const json &node, const std::string &name, uint32_t &value)
77 {
78     auto &subNode = GetSubNode(node, name);
79     if (subNode.is_null() || !subNode.is_number_unsigned()) {
80         return false;
81     }
82     subNode.get_to(value);
83     return true;
84 }
85 
GetValue(const json & node,const std::string & name,int32_t & value)86 bool Serializable::GetValue(const json &node, const std::string &name, int32_t &value)
87 {
88     auto &subNode = GetSubNode(node, name);
89     if (subNode.is_null() || !subNode.is_number_integer()) {
90         return false;
91     }
92     subNode.get_to(value);
93     return true;
94 }
95 
GetValue(const json & node,const std::string & name,int64_t & value)96 bool Serializable::GetValue(const json &node, const std::string &name, int64_t &value)
97 {
98     auto &subNode = GetSubNode(node, name);
99     if (subNode.is_null() || !subNode.is_number_integer()) {
100         return false;
101     }
102     subNode.get_to(value);
103     return true;
104 }
105 
GetValue(const json & node,const std::string & name,uint64_t & value)106 bool Serializable::GetValue(const json &node, const std::string &name, uint64_t &value)
107 {
108     auto &subNode = GetSubNode(node, name);
109     if (subNode.is_null() || !subNode.is_number_unsigned()) {
110         return false;
111     }
112     subNode.get_to(value);
113     return true;
114 }
115 
GetValue(const json & node,const std::string & name,uint16_t & value)116 bool Serializable::GetValue(const json &node, const std::string &name, uint16_t &value)
117 {
118     auto &subNode = GetSubNode(node, name);
119     if (subNode.is_null() || !subNode.is_number_unsigned()) {
120         return false;
121     }
122     subNode.get_to(value);
123     return true;
124 }
125 
GetValue(const json & node,const std::string & name,bool & value)126 bool Serializable::GetValue(const json &node, const std::string &name, bool &value)
127 {
128     auto &subNode = GetSubNode(node, name);
129     if (subNode.is_boolean()) {
130         subNode.get_to(value);
131         return true;
132     }
133 
134     if (subNode.is_number_unsigned()) {
135         uint32_t number = 0;
136         subNode.get_to(number);
137         value = number != 0;
138         return true;
139     }
140 
141     return false;
142 }
143 
GetValue(const json & node,const std::string & name,std::vector<uint8_t> & value)144 bool Serializable::GetValue(const json &node, const std::string &name, std::vector<uint8_t> &value)
145 {
146     auto &subNode = GetSubNode(node, name);
147     if (subNode.is_null() || !subNode.is_array()) {
148         return false;
149     }
150     value = std::vector<uint8_t>(subNode);
151     return true;
152 }
153 
GetValue(const json & node,const std::string & name,Serializable & value)154 bool Serializable::GetValue(const json &node, const std::string &name, Serializable &value)
155 {
156     auto &subNode = GetSubNode(node, name);
157     if (subNode.is_null() || !subNode.is_object()) {
158         return false;
159     }
160     return value.Unmarshal(subNode);
161 }
162 
SetValue(json & node,const std::string & value)163 bool Serializable::SetValue(json &node, const std::string &value)
164 {
165     node = value;
166     return true;
167 }
168 
SetValue(json & node,const uint32_t & value)169 bool Serializable::SetValue(json &node, const uint32_t &value)
170 {
171     node = value;
172     return true;
173 }
174 
SetValue(json & node,const int32_t & value)175 bool Serializable::SetValue(json &node, const int32_t &value)
176 {
177     node = value;
178     return true;
179 }
180 
SetValue(json & node,const int64_t & value)181 bool Serializable::SetValue(json &node, const int64_t &value)
182 {
183     node = value;
184     return true;
185 }
186 
SetValue(json & node,const double & value)187 bool Serializable::SetValue(json &node, const double &value)
188 {
189     node = value;
190     return true;
191 }
192 
SetValue(json & node,const uint64_t & value)193 bool Serializable::SetValue(json &node, const uint64_t &value)
194 {
195     node = value;
196     return true;
197 }
198 
SetValue(json & node,const uint16_t & value)199 bool Serializable::SetValue(json &node, const uint16_t &value)
200 {
201     node = value;
202     return true;
203 }
204 
SetValue(json & node,const std::vector<uint8_t> & value)205 bool Serializable::SetValue(json &node, const std::vector<uint8_t> &value)
206 {
207     node = value;
208     return true;
209 }
210 
SetValue(json & node,const Serializable & value)211 bool Serializable::SetValue(json &node, const Serializable &value)
212 {
213     return value.Marshal(node);
214 }
215 
GetSubNode(const json & node,const std::string & name)216 const Serializable::json &Serializable::GetSubNode(const json &node, const std::string &name)
217 {
218     static const json jsonNull = json::value_t::null;
219     if (node.is_discarded() || node.is_null()) {
220         return jsonNull;
221     }
222 
223     if (name.empty()) {
224         return node;
225     }
226 
227     auto it = node.find(name);
228     if (it == node.end()) {
229         return jsonNull;
230     }
231     return *it;
232 }
233 } // namespace DistributedData
234 } // namespace OHOS
235