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