1 /*
2 * Copyright (c) 2022 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 "map_string_serializer.h"
17 #include <string_ex.h>
18
19 namespace OHOS {
20 namespace EDM {
Deserialize(const std::string & jsonString,std::map<std::string,std::string> & dataObj)21 bool MapStringSerializer::Deserialize(const std::string &jsonString, std::map<std::string, std::string> &dataObj)
22 {
23 if (jsonString.empty()) {
24 return true;
25 }
26 Json::Value root;
27 const auto rawJsonLength = static_cast<int>(jsonString.length());
28 JSONCPP_STRING err;
29 Json::CharReaderBuilder builder;
30 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
31 if (!reader->parse(jsonString.c_str(), jsonString.c_str() + rawJsonLength, &root, &err)) {
32 EDMLOGE("MapStringSerializer::Deserialize jsonString error: %{public}s", err.c_str());
33 return false;
34 }
35 if (!root.isObject()) {
36 EDMLOGE("MapStringSerializer::Deserialize jsonString is not map.");
37 return false;
38 }
39 if (root.empty()) {
40 return true;
41 }
42 for (auto &member : root.getMemberNames()) {
43 if (root[member].type() != Json::ValueType::stringValue && root[member].type() != Json::ValueType::intValue &&
44 root[member].type() != Json::ValueType::uintValue && root[member].type() != Json::ValueType::booleanValue) {
45 EDMLOGE("MapStringSerializer::Deserialize member type is not support.");
46 return false;
47 }
48 dataObj.insert(std::pair<std::string, std::string>(member, root[member].asString()));
49 }
50 return true;
51 }
52
Serialize(const std::map<std::string,std::string> & dataObj,std::string & jsonString)53 bool MapStringSerializer::Serialize(const std::map<std::string, std::string> &dataObj, std::string &jsonString)
54 {
55 if (dataObj.empty()) {
56 jsonString = "";
57 return true;
58 }
59 Json::Value root;
60 for (const auto &item : dataObj) {
61 root[item.first] = item.second;
62 }
63 Json::StreamWriterBuilder builder;
64 builder["indentation"] = " ";
65 jsonString = Json::writeString(builder, root);
66 return true;
67 }
68
GetPolicy(MessageParcel & data,std::map<std::string,std::string> & result)69 bool MapStringSerializer::GetPolicy(MessageParcel &data, std::map<std::string, std::string> &result)
70 {
71 std::vector<std::string> keys;
72 std::vector<std::string> values;
73 if (!data.ReadStringVector(&keys)) {
74 EDMLOGE("MapStringSerializer::read map keys fail.");
75 return false;
76 }
77 if (!data.ReadStringVector(&values)) {
78 EDMLOGE("MapStringSerializer::read map values fail.");
79 return false;
80 }
81 if (keys.size() != values.size()) {
82 return false;
83 }
84 for (uint64_t i = 0; i < keys.size(); ++i) {
85 result.insert(std::make_pair(keys.at(i), values.at(i)));
86 }
87 return true;
88 }
89
WritePolicy(MessageParcel & reply,std::map<std::string,std::string> & result)90 bool MapStringSerializer::WritePolicy(MessageParcel &reply, std::map<std::string, std::string> &result)
91 {
92 std::vector<std::string> keys;
93 std::vector<std::string> values;
94 for (const auto &item : result) {
95 keys.push_back(item.first);
96 values.push_back(item.second);
97 }
98 return reply.WriteStringVector(keys) && reply.WriteStringVector(values);
99 }
100
MergePolicy(std::vector<std::map<std::string,std::string>> & data,std::map<std::string,std::string> & result)101 bool MapStringSerializer::MergePolicy(std::vector<std::map<std::string, std::string>> &data,
102 std::map<std::string, std::string> &result)
103 {
104 if (!data.empty()) {
105 result = *(data.rbegin());
106 }
107 return true;
108 }
109 } // namespace EDM
110 } // namespace OHOS