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 "cjson_serializer.h"
17
18 namespace OHOS {
19 namespace EDM {
Deserialize(const std::string & jsonString,cJSON * & dataObj)20 bool CjsonSerializer::Deserialize(const std::string &jsonString, cJSON* &dataObj)
21 {
22 if (jsonString.empty()) {
23 return true;
24 }
25 dataObj = cJSON_Parse(jsonString.c_str());
26 return dataObj != nullptr;
27 }
28
Serialize(cJSON * const & dataObj,std::string & jsonString)29 bool CjsonSerializer::Serialize(cJSON *const &dataObj, std::string &jsonString)
30 {
31 if (dataObj == nullptr) {
32 return false;
33 }
34 char *cJsonStr = cJSON_Print(dataObj);
35 if (cJsonStr != nullptr) {
36 jsonString = std::string(cJsonStr);
37 cJSON_free(cJsonStr);
38 }
39 return !jsonString.empty();
40 }
41
GetPolicy(MessageParcel & data,cJSON * & result)42 bool CjsonSerializer::GetPolicy(MessageParcel &data, cJSON* &result)
43 {
44 std::string jsonString = data.ReadString();
45 return Deserialize(jsonString, result);
46 }
47
WritePolicy(MessageParcel & reply,cJSON * & result)48 bool CjsonSerializer::WritePolicy(MessageParcel &reply, cJSON* &result)
49 {
50 std::string jsonString;
51 if (!Serialize(result, jsonString)) {
52 return false;
53 }
54 return reply.WriteString(jsonString);
55 }
56
MergePolicy(std::vector<cJSON * > & data,cJSON * & result)57 bool CjsonSerializer::MergePolicy(std::vector<cJSON*> &data, cJSON* &result)
58 {
59 return true;
60 }
61 } // namespace EDM
62 } // namespace OHOS