1 /*
2 * Copyright (c) 2025 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 "array_int_serializer.h"
17 #include "cJSON.h"
18 #include "cjson_check.h"
19 #include "edm_constants.h"
20
21 namespace OHOS {
22 namespace EDM {
23
SetUnionPolicyData(std::vector<int32_t> & data,std::vector<int32_t> & currentData)24 std::vector<int32_t> ArrayIntSerializer::SetUnionPolicyData(std::vector<int32_t> &data,
25 std::vector<int32_t> ¤tData)
26 {
27 std::vector<int32_t> mergeData;
28 std::sort(data.begin(), data.end());
29 std::sort(currentData.begin(), currentData.end());
30 std::set_union(data.begin(), data.end(), currentData.begin(), currentData.end(), back_inserter(mergeData));
31 return mergeData;
32 }
33
SetDifferencePolicyData(std::vector<int32_t> & data,std::vector<int32_t> & currentData)34 std::vector<int32_t> ArrayIntSerializer::SetDifferencePolicyData(std::vector<int32_t> &data,
35 std::vector<int32_t> ¤tData)
36 {
37 std::vector<int32_t> mergeData;
38 std::sort(data.begin(), data.end());
39 std::sort(currentData.begin(), currentData.end());
40 std::set_difference(currentData.begin(), currentData.end(), data.begin(), data.end(), back_inserter(mergeData));
41 return mergeData;
42 }
43
SetIntersectionPolicyData(std::vector<int32_t> & data,std::vector<int32_t> & currentData)44 std::vector<int32_t> ArrayIntSerializer::SetIntersectionPolicyData(std::vector<int32_t> &data,
45 std::vector<int32_t> ¤tData)
46 {
47 std::vector<int32_t> mergeData;
48 std::sort(data.begin(), data.end());
49 std::sort(currentData.begin(), currentData.end());
50 std::set_intersection(currentData.begin(), currentData.end(), data.begin(), data.end(), back_inserter(mergeData));
51 return mergeData;
52 }
53
Deserialize(const std::string & jsonString,std::vector<int32_t> & dataObj)54 bool ArrayIntSerializer::Deserialize(const std::string &jsonString, std::vector<int32_t> &dataObj)
55 {
56 cJSON* json = cJSON_Parse(jsonString.c_str());
57 if (json == nullptr) {
58 return true;
59 }
60 if (!cJSON_IsArray(json)) {
61 cJSON_Delete(json);
62 return false;
63 }
64 cJSON *item = json->child;
65 while (item != nullptr) {
66 if (cJSON_IsNumber(item)) {
67 int32_t value = (int32_t)item->valueint;
68 dataObj.push_back(value);
69 }
70 item = item->next;
71 }
72 cJSON_Delete(json);
73 return true;
74 }
75
Serialize(const std::vector<int32_t> & dataObj,std::string & jsonString)76 bool ArrayIntSerializer::Serialize(const std::vector<int32_t> &dataObj, std::string &jsonString)
77 {
78 if (dataObj.empty()) {
79 return true;
80 }
81 cJSON* jsonArray = nullptr;
82 CJSON_CREATE_ARRAY_AND_CHECK(jsonArray, false);
83 for (const auto& item : dataObj) {
84 cJSON_AddItemToArray(jsonArray, cJSON_CreateNumber(item));
85 }
86 char* jsonStr = cJSON_Print(jsonArray);
87 if (jsonStr == nullptr) {
88 cJSON_Delete(jsonArray);
89 return false;
90 }
91 jsonString = std::string(jsonStr);
92 cJSON_free(jsonStr);
93 cJSON_Delete(jsonArray);
94 return true;
95 }
96
GetPolicy(MessageParcel & data,std::vector<int32_t> & result)97 bool ArrayIntSerializer::GetPolicy(MessageParcel &data, std::vector<int32_t> &result)
98 {
99 std::vector<int32_t> readVector;
100 if (!data.ReadInt32Vector(&readVector)) {
101 return false;
102 }
103 // Data will be appended to result, and the original data of result will not be deleted.
104 for (const auto &item : readVector) {
105 result.push_back(item);
106 }
107 Deduplication(result);
108 return true;
109 }
110
WritePolicy(MessageParcel & reply,std::vector<int32_t> & result)111 bool ArrayIntSerializer::WritePolicy(MessageParcel &reply, std::vector<int32_t> &result)
112 {
113 return true;
114 }
115
MergePolicy(std::vector<std::vector<int32_t>> & data,std::vector<int32_t> & result)116 bool ArrayIntSerializer::MergePolicy(std::vector<std::vector<int32_t>> &data, std::vector<int32_t> &result)
117 {
118 std::set<int32_t> stData;
119 for (const auto &dataItem : data) {
120 for (const auto &item : dataItem) {
121 stData.insert(item);
122 }
123 }
124 result.assign(stData.begin(), stData.end());
125 Deduplication(result);
126 return true;
127 }
128
Deduplication(std::vector<int32_t> & dataObj)129 void ArrayIntSerializer::Deduplication(std::vector<int32_t> &dataObj)
130 {
131 std::sort(dataObj.begin(), dataObj.end());
132 auto iter = std::unique(dataObj.begin(), dataObj.end());
133 dataObj.erase(iter, dataObj.end());
134 }
135 } // namespace EDM
136 } // namespace OHOS