• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "cjson_check.h"
19 
20 namespace OHOS {
21 namespace EDM {
Deserialize(const std::string & jsonString,std::map<std::string,std::string> & dataObj)22 bool MapStringSerializer::Deserialize(const std::string &jsonString, std::map<std::string, std::string> &dataObj)
23 {
24     if (jsonString.empty()) {
25         return true;
26     }
27 
28     cJSON *root = cJSON_Parse(jsonString.c_str());
29     if (root == nullptr) {
30         return false;
31     }
32 
33     if (!cJSON_IsObject(root)) {
34         EDMLOGE("JSON is not an object.");
35         cJSON_Delete(root);
36         return false;
37     }
38 
39     cJSON *item = nullptr;
40     cJSON_ArrayForEach(item, root) {
41         if (!cJSON_IsString(item)) {
42             EDMLOGE("item is not a string.");
43             cJSON_Delete(root);
44             return false;
45         }
46         dataObj.emplace(item->string, item->valuestring);
47     }
48 
49     cJSON_Delete(root);
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     cJSON *root = nullptr;
56     CJSON_CREATE_OBJECT_AND_CHECK(root, false);
57 
58     for (const auto &item : dataObj) {
59         if (!cJSON_AddStringToObject(root, item.first.c_str(), item.second.c_str())) {
60             cJSON_Delete(root);
61             return false;
62         }
63     }
64 
65     char *jsonStr = cJSON_Print(root);
66     if (jsonStr == nullptr) {
67         cJSON_Delete(root);
68         return false;
69     }
70 
71     jsonString = jsonStr;
72     cJSON_free(jsonStr);
73     cJSON_Delete(root);
74     return true;
75 }
76 
GetPolicy(MessageParcel & data,std::map<std::string,std::string> & result)77 bool MapStringSerializer::GetPolicy(MessageParcel &data, std::map<std::string, std::string> &result)
78 {
79     std::vector<std::string> keys;
80     std::vector<std::string> values;
81     if (!data.ReadStringVector(&keys)) {
82         EDMLOGE("MapStringSerializer::read map keys fail.");
83         return false;
84     }
85     if (!data.ReadStringVector(&values)) {
86         EDMLOGE("MapStringSerializer::read map values fail.");
87         return false;
88     }
89     if (keys.size() != values.size()) {
90         return false;
91     }
92     for (uint64_t i = 0; i < keys.size(); ++i) {
93         result.insert(std::make_pair(keys.at(i), values.at(i)));
94     }
95     return true;
96 }
97 
WritePolicy(MessageParcel & reply,std::map<std::string,std::string> & result)98 bool MapStringSerializer::WritePolicy(MessageParcel &reply, std::map<std::string, std::string> &result)
99 {
100     std::vector<std::string> keys;
101     std::vector<std::string> values;
102     for (const auto &item : result) {
103         keys.push_back(item.first);
104         values.push_back(item.second);
105     }
106     return reply.WriteStringVector(keys) && reply.WriteStringVector(values);
107 }
108 
MergePolicy(std::vector<std::map<std::string,std::string>> & data,std::map<std::string,std::string> & result)109 bool MapStringSerializer::MergePolicy(std::vector<std::map<std::string, std::string>> &data,
110     std::map<std::string, std::string> &result)
111 {
112     if (!data.empty()) {
113         result = *(data.rbegin());
114     }
115     return true;
116 }
117 } // namespace EDM
118 } // namespace OHOS