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 dataObj.insert(std::pair<std::string, std::string>(member, root[member].asString()));
44 }
45 return true;
46 }
47
Serialize(const std::map<std::string,std::string> & dataObj,std::string & jsonString)48 bool MapStringSerializer::Serialize(const std::map<std::string, std::string> &dataObj, std::string &jsonString)
49 {
50 if (dataObj.empty()) {
51 jsonString = "";
52 return true;
53 }
54 Json::Value root;
55 for (const auto &item : dataObj) {
56 root[item.first] = item.second;
57 }
58 Json::StreamWriterBuilder builder;
59 builder["indentation"] = " ";
60 jsonString = Json::writeString(builder, root);
61 return true;
62 }
63
GetPolicy(MessageParcel & data,std::map<std::string,std::string> & result)64 bool MapStringSerializer::GetPolicy(MessageParcel &data, std::map<std::string, std::string> &result)
65 {
66 std::vector<std::u16string> keys16;
67 std::vector<std::u16string> values16;
68 if (!data.ReadString16Vector(&keys16)) {
69 EDMLOGE("MapStringSerializer::read map keys fail.");
70 return false;
71 }
72 if (!data.ReadString16Vector(&values16)) {
73 EDMLOGE("MapStringSerializer::read map values fail.");
74 return false;
75 }
76 if (keys16.size() != values16.size()) {
77 return false;
78 }
79 std::vector<std::string> keys;
80 std::vector<std::string> values;
81 for (const std::u16string &key : keys16) {
82 keys.push_back(Str16ToStr8(key));
83 }
84 for (const std::u16string &value : values16) {
85 values.push_back(Str16ToStr8(value));
86 }
87 for (unsigned long i = 0; i < keys.size(); ++i) {
88 result.insert(std::make_pair(keys.at(i), values.at(i)));
89 }
90 return true;
91 }
92
WritePolicy(MessageParcel & reply,std::map<std::string,std::string> & result)93 bool MapStringSerializer::WritePolicy(MessageParcel &reply, std::map<std::string, std::string> &result)
94 {
95 std::vector<std::u16string> keys;
96 std::vector<std::u16string> values;
97 for (const auto &item : result) {
98 keys.push_back(Str8ToStr16(item.first));
99 values.push_back(Str8ToStr16(item.second));
100 }
101 return reply.WriteString16Vector(keys) && reply.WriteString16Vector(values);
102 }
103
MergePolicy(std::vector<std::map<std::string,std::string>> & data,std::map<std::string,std::string> & result)104 bool MapStringSerializer::MergePolicy(std::vector<std::map<std::string, std::string>> &data,
105 std::map<std::string, std::string> &result)
106 {
107 for (auto iter = data.rbegin(); iter != data.rend(); ++iter) {
108 result = *iter;
109 return true;
110 }
111 return true;
112 }
113 } // namespace EDM
114 } // namespace OHOS