• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,bool & value)116 bool Serializable::GetValue(const json &node, const std::string &name, bool &value)
117 {
118     auto &subNode = GetSubNode(node, name);
119     if (subNode.is_boolean()) {
120         subNode.get_to(value);
121         return true;
122     }
123 
124     if (subNode.is_number_unsigned()) {
125         uint32_t number = 0;
126         subNode.get_to(number);
127         value = number != 0;
128         return true;
129     }
130 
131     return false;
132 }
133 
GetValue(const json & node,const std::string & name,std::vector<uint8_t> & value)134 bool Serializable::GetValue(const json &node, const std::string &name, std::vector<uint8_t> &value)
135 {
136     auto &subNode = GetSubNode(node, name);
137     if (subNode.is_null() || !subNode.is_array()) {
138         return false;
139     }
140     value = std::vector<uint8_t>(subNode);
141     return true;
142 }
143 
GetValue(const json & node,const std::string & name,Serializable & value)144 bool Serializable::GetValue(const json &node, const std::string &name, Serializable &value)
145 {
146     auto &subNode = GetSubNode(node, name);
147     if (subNode.is_null() || !subNode.is_object()) {
148         return false;
149     }
150     return value.Unmarshal(subNode);
151 }
152 
SetValue(json & node,const std::string & value)153 bool Serializable::SetValue(json &node, const std::string &value)
154 {
155     node = value;
156     return true;
157 }
158 
SetValue(json & node,const uint32_t & value)159 bool Serializable::SetValue(json &node, const uint32_t &value)
160 {
161     node = value;
162     return true;
163 }
164 
SetValue(json & node,const int32_t & value)165 bool Serializable::SetValue(json &node, const int32_t &value)
166 {
167     node = value;
168     return true;
169 }
170 
SetValue(json & node,const int64_t & value)171 bool Serializable::SetValue(json &node, const int64_t &value)
172 {
173     node = value;
174     return true;
175 }
176 
SetValue(json & node,const double & value)177 bool Serializable::SetValue(json &node, const double &value)
178 {
179     node = value;
180     return true;
181 }
182 
SetValue(json & node,const uint64_t & value)183 bool Serializable::SetValue(json &node, const uint64_t &value)
184 {
185     node = value;
186     return true;
187 }
188 
SetValue(json & node,const std::vector<uint8_t> & value)189 bool Serializable::SetValue(json &node, const std::vector<uint8_t> &value)
190 {
191     node = value;
192     return true;
193 }
194 
SetValue(json & node,const Serializable & value)195 bool Serializable::SetValue(json &node, const Serializable &value)
196 {
197     return value.Marshal(node);
198 }
199 
GetSubNode(const json & node,const std::string & name)200 const Serializable::json &Serializable::GetSubNode(const json &node, const std::string &name)
201 {
202     static const json jsonNull = json::value_t::null;
203     if (node.is_discarded() || node.is_null()) {
204         return jsonNull;
205     }
206 
207     if (name.empty()) {
208         return node;
209     }
210 
211     auto it = node.find(name);
212     if (it == node.end()) {
213         return jsonNull;
214     }
215     return *it;
216 }
217 } // namespace DistributedData
218 } // namespace OHOS
219