1 /*
2 * Copyright (c) 2024 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 "watermark_image_serializer.h"
17
18 #include "cJSON.h"
19 #include "cjson_check.h"
20
21 namespace OHOS {
22 namespace EDM {
23
24 const char* const BUNDLE_NAME = "bundleName";
25 const char* const ACCOUNT_ID = "accountId";
26 const char* const FILE_NAME = "fileName";
27 const char* const WIDTH = "width";
28 const char* const HEIGHT = "height";
29
Deserialize(const std::string & policy,std::map<std::pair<std::string,int32_t>,WatermarkImageType> & dataObj)30 bool WatermarkImageSerializer::Deserialize(const std::string &policy,
31 std::map<std::pair<std::string, int32_t>, WatermarkImageType> &dataObj)
32 {
33 if (policy.empty()) {
34 return true;
35 }
36 cJSON* root = cJSON_Parse(policy.c_str());
37 if (root == nullptr) {
38 return false;
39 }
40 cJSON* item;
41 cJSON_ArrayForEach(item, root) {
42 cJSON* bundleName = cJSON_GetObjectItem(item, BUNDLE_NAME);
43 cJSON* accountId = cJSON_GetObjectItem(item, ACCOUNT_ID);
44 cJSON* fileName = cJSON_GetObjectItem(item, FILE_NAME);
45 cJSON* width = cJSON_GetObjectItem(item, WIDTH);
46 cJSON* height = cJSON_GetObjectItem(item, HEIGHT);
47 if (bundleName == nullptr || accountId == nullptr || fileName == nullptr ||
48 width == nullptr || height == nullptr || !cJSON_IsString(bundleName)
49 || !cJSON_IsString(fileName) || !cJSON_IsNumber(width) ||
50 !cJSON_IsNumber(accountId) || !cJSON_IsNumber(height)) {
51 cJSON_Delete(root);
52 return false;
53 }
54 std::pair<std::string, int32_t> key{cJSON_GetStringValue(bundleName), accountId->valueint};
55 std::string fileNameStr = cJSON_GetStringValue(fileName);
56 WatermarkImageType imageType{fileNameStr, width->valueint, height->valueint};
57 dataObj.insert(std::make_pair(key, imageType));
58 }
59 cJSON_Delete(root);
60 return true;
61 }
62
Serialize(const std::map<std::pair<std::string,int32_t>,WatermarkImageType> & dataObj,std::string & policy)63 bool WatermarkImageSerializer::Serialize(const std::map<std::pair<std::string, int32_t>, WatermarkImageType> &dataObj,
64 std::string &policy)
65 {
66 if (dataObj.empty()) {
67 return true;
68 }
69 cJSON* root = nullptr;
70 CJSON_CREATE_ARRAY_AND_CHECK(root, false);
71 for (auto& it : dataObj) {
72 cJSON* item = nullptr;
73 CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(item, false, root);
74 cJSON_AddStringToObject(item, BUNDLE_NAME, it.first.first.c_str());
75 cJSON_AddNumberToObject(item, ACCOUNT_ID, it.first.second);
76 cJSON_AddStringToObject(item, FILE_NAME, it.second.fileName.c_str());
77 cJSON_AddNumberToObject(item, WIDTH, it.second.width);
78 cJSON_AddNumberToObject(item, HEIGHT, it.second.height);
79 cJSON_AddItemToArray(root, item);
80 }
81 char* jsonStr = cJSON_Print(root);
82 if (jsonStr == nullptr) {
83 cJSON_Delete(root);
84 return false;
85 }
86 policy = std::string(jsonStr);
87 cJSON_free(jsonStr);
88 cJSON_Delete(root);
89 return true;
90 }
91
GetPolicy(MessageParcel & data,std::map<std::pair<std::string,int32_t>,WatermarkImageType> & result)92 bool WatermarkImageSerializer::GetPolicy(MessageParcel &data,
93 std::map<std::pair<std::string, int32_t>, WatermarkImageType> &result)
94 {
95 return true;
96 }
97
WritePolicy(MessageParcel & reply,std::map<std::pair<std::string,int32_t>,WatermarkImageType> & result)98 bool WatermarkImageSerializer::WritePolicy(MessageParcel &reply,
99 std::map<std::pair<std::string, int32_t>, WatermarkImageType> &result)
100 {
101 return true;
102 }
103
MergePolicy(std::vector<std::map<std::pair<std::string,int32_t>,WatermarkImageType>> & data,std::map<std::pair<std::string,int32_t>,WatermarkImageType> & result)104 bool WatermarkImageSerializer::MergePolicy(
105 std::vector<std::map<std::pair<std::string, int32_t>, WatermarkImageType>> &data,
106 std::map<std::pair<std::string, int32_t>, WatermarkImageType> &result)
107 {
108 if (!data.empty()) {
109 result = data.back();
110 }
111 return true;
112 }
113 } // namespace EDM
114 } // namespace OHOS
115