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
GetValue(const json & node,const std::string & name,std::string & value)58 bool Serializable::GetValue(const json &node, const std::string &name, std::string &value)
59 {
60 auto &subNode = GetSubNode(node, name);
61 if (subNode.is_null() || !subNode.is_string()) {
62 return false;
63 }
64 value = subNode;
65 return true;
66 }
67
GetValue(const json & node,const std::string & name,uint32_t & value)68 bool Serializable::GetValue(const json &node, const std::string &name, uint32_t &value)
69 {
70 auto &subNode = GetSubNode(node, name);
71 if (subNode.is_null() || !subNode.is_number_unsigned()) {
72 return false;
73 }
74 subNode.get_to(value);
75 return true;
76 }
77
GetValue(const json & node,const std::string & name,int32_t & value)78 bool Serializable::GetValue(const json &node, const std::string &name, int32_t &value)
79 {
80 auto &subNode = GetSubNode(node, name);
81 if (subNode.is_null() || !subNode.is_number_integer()) {
82 return false;
83 }
84 subNode.get_to(value);
85 return true;
86 }
87
GetValue(const json & node,const std::string & name,int64_t & value)88 bool Serializable::GetValue(const json &node, const std::string &name, int64_t &value)
89 {
90 auto &subNode = GetSubNode(node, name);
91 if (subNode.is_null() || !subNode.is_number_integer()) {
92 return false;
93 }
94 subNode.get_to(value);
95 return true;
96 }
97
GetValue(const json & node,const std::string & name,bool & value)98 bool Serializable::GetValue(const json &node, const std::string &name, bool &value)
99 {
100 auto &subNode = GetSubNode(node, name);
101 if (subNode.is_null() || !subNode.is_boolean()) {
102 return false;
103 }
104 subNode.get_to(value);
105 return true;
106 }
107
GetValue(const json & node,const std::string & name,std::vector<uint8_t> & value)108 bool Serializable::GetValue(const json &node, const std::string &name, std::vector<uint8_t> &value)
109 {
110 auto &subNode = GetSubNode(node, name);
111 if (subNode.is_null() || !subNode.is_array()) {
112 return false;
113 }
114 value = std::vector<uint8_t>(subNode);
115 return true;
116 }
117
GetValue(const json & node,const std::string & name,Serializable & value)118 bool Serializable::GetValue(const json &node, const std::string &name, Serializable &value)
119 {
120 auto &subNode = GetSubNode(node, name);
121 if (subNode.is_null() || !subNode.is_object()) {
122 return false;
123 }
124 return value.Unmarshal(subNode);
125 }
126
SetValue(json & node,const std::string & value)127 bool Serializable::SetValue(json &node, const std::string &value)
128 {
129 node = value;
130 return true;
131 }
132
SetValue(json & node,const uint32_t & value)133 bool Serializable::SetValue(json &node, const uint32_t &value)
134 {
135 node = value;
136 return true;
137 }
138
SetValue(json & node,const int32_t & value)139 bool Serializable::SetValue(json &node, const int32_t &value)
140 {
141 node = value;
142 return true;
143 }
144
SetValue(json & node,const int64_t & value)145 bool Serializable::SetValue(json &node, const int64_t &value)
146 {
147 node = value;
148 return true;
149 }
150
SetValue(json & node,const bool & value)151 bool Serializable::SetValue(json &node, const bool &value)
152 {
153 node = value;
154 return true;
155 }
156
SetValue(json & node,const std::vector<uint8_t> & value)157 bool Serializable::SetValue(json &node, const std::vector<uint8_t> &value)
158 {
159 node = value;
160 return true;
161 }
162
SetValue(json & node,const Serializable & value)163 bool Serializable::SetValue(json &node, const Serializable &value)
164 {
165 return value.Marshal(node);
166 }
167
GetSubNode(const json & node,const std::string & name)168 const Serializable::json &Serializable::GetSubNode(const json &node, const std::string &name)
169 {
170 static const json jsonNull = json::value_t::null;
171 if (node.is_discarded() || node.is_null()) {
172 return jsonNull;
173 }
174
175 if (name.empty()) {
176 return node;
177 }
178
179 auto it = node.find(name);
180 if (it == node.end()) {
181 return jsonNull;
182 }
183 return *it;
184 }
185 } // namespace DistributedData
186 } // namespace OHOS
187