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