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 
SetValue(json & node,const std::string & value)144 bool Serializable::SetValue(json &node, const std::string &value)
145 {
146     node = value;
147     return true;
148 }
149 
SetValue(json & node,const uint32_t & value)150 bool Serializable::SetValue(json &node, const uint32_t &value)
151 {
152     node = value;
153     return true;
154 }
155 
SetValue(json & node,const int32_t & value)156 bool Serializable::SetValue(json &node, const int32_t &value)
157 {
158     node = value;
159     return true;
160 }
161 
SetValue(json & node,const uint64_t & value)162 bool Serializable::SetValue(json &node, const uint64_t &value)
163 {
164     node = value;
165     return true;
166 }
167 
SetValue(json & node,const int64_t & value)168 bool Serializable::SetValue(json &node, const int64_t &value)
169 {
170     node = value;
171     return true;
172 }
173 
SetValue(json & node,const bool & value)174 bool Serializable::SetValue(json &node, const bool &value)
175 {
176     node = value;
177     return true;
178 }
179 
SetValue(json & node,const std::vector<uint8_t> & value)180 bool Serializable::SetValue(json &node, const std::vector<uint8_t> &value)
181 {
182     node = value;
183     return true;
184 }
185 
SetValue(json & node,const Serializable & value)186 bool Serializable::SetValue(json &node, const Serializable &value)
187 {
188     return value.Marshal(node);
189 }
190 
GetSubNode(const json & node,const std::string & name)191 const Serializable::json &Serializable::GetSubNode(const json &node, const std::string &name)
192 {
193     static const json jsonNull = json::value_t::null;
194     if (node.is_discarded() || node.is_null()) {
195         return jsonNull;
196     }
197 
198     if (name.empty()) {
199         return node;
200     }
201 
202     auto it = node.find(name);
203     if (it == node.end()) {
204         return jsonNull;
205     }
206     return *it;
207 }
208 } // namespace OHOS
209