• 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 "json_serializer.h"
17 #include <string_ex.h>
18 
19 namespace OHOS {
20 namespace EDM {
Deserialize(const std::string & jsonString,Json::Value & dataObj)21 bool JsonSerializer::Deserialize(const std::string &jsonString, Json::Value &dataObj)
22 {
23     const auto rawJsonLength = static_cast<int>(jsonString.length());
24     JSONCPP_STRING err;
25     Json::CharReaderBuilder builder;
26     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
27     if (!reader->parse(jsonString.c_str(), jsonString.c_str() + rawJsonLength, &dataObj, &err)) {
28         EDMLOGE("JsonSerializer::Deserialize jsonString error: %{public}s ", err.c_str());
29         return false;
30     }
31     return true;
32 }
33 
Serialize(const Json::Value & dataObj,std::string & jsonString)34 bool JsonSerializer::Serialize(const Json::Value &dataObj, std::string &jsonString)
35 {
36     Json::StreamWriterBuilder builder;
37     builder["indentation"] = "    ";
38     jsonString = Json::writeString(builder, dataObj);
39     return true;
40 }
41 
GetPolicy(MessageParcel & data,Json::Value & result)42 bool JsonSerializer::GetPolicy(MessageParcel &data, Json::Value &result)
43 {
44     std::string jsonString = Str16ToStr8(data.ReadString16());
45     return Deserialize(jsonString, result);
46 }
47 
WritePolicy(MessageParcel & reply,Json::Value & result)48 bool JsonSerializer::WritePolicy(MessageParcel &reply, Json::Value &result)
49 {
50     std::string jsonString;
51     if (!Serialize(result, jsonString)) {
52         return false;
53     }
54     return reply.WriteString16(Str8ToStr16(jsonString));
55 }
56 
MergePolicy(std::vector<Json::Value> & data,Json::Value & result)57 bool JsonSerializer::MergePolicy(std::vector<Json::Value> &data, Json::Value &result)
58 {
59     return true;
60 }
61 } // namespace EDM
62 } // namespace OHOS