• 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 #ifndef PRINT_JSON_UTIL_H
17 #define PRINT_JSON_UTIL_H
18 
19  #include <string>
20  #include <memory>
21  #include <json/json.h>
22  #include <print_log.h>
23 
24 namespace OHOS::Print {
25 
26 class PrintJsonUtil {
27 public:
28     static bool IsMember(const Json::Value &jsonObject, const std::string &key);
29     static bool Parse(const std::string &root, Json::Value &jsonObject);
30     static bool ParseFromStream(Json::IStream &ifs, Json::Value &jsonObject);
31     static std::string WriteString(const Json::Value &jsonObject);
32     static std::string WriteStringUTF8(const Json::Value &jsonObject);
33 };
34 
35 /*
36  Json::Value 非object对象直接调用isMember会程序崩溃
37  */
IsMember(const Json::Value & jsonObject,const std::string & key)38 inline bool PrintJsonUtil::IsMember(const Json::Value &jsonObject, const std::string &key)
39 {
40     if (jsonObject.isObject() || jsonObject.isNull()) {
41         return jsonObject.isMember(key);
42     }
43     return false;
44 }
45 
46 /*
47 string对象,不包含数组,或者对象格式,使用此方法转换
48 */
Parse(const std::string & root,Json::Value & jsonObject)49 inline bool PrintJsonUtil::Parse(const std::string &root, Json::Value &jsonObject)
50 {
51     Json::CharReaderBuilder rBuilder;
52     std::unique_ptr<Json::CharReader> reader(rBuilder.newCharReader());
53     JSONCPP_STRING err;
54     if (!reader->parse(root.c_str(), root.c_str() + root.length(), &jsonObject, &err)) {
55         PRINT_HILOGE("PrintJsonUtil string parse error! ErrorInfo: %{public}s", err.c_str());
56         return false;
57     }
58     return true;
59 }
60 
61 /*
62 文件流转化json,使用此方法转换
63 */
ParseFromStream(Json::IStream & ifs,Json::Value & jsonObject)64 inline bool PrintJsonUtil::ParseFromStream(Json::IStream &ifs, Json::Value &jsonObject)
65 {
66     Json::CharReaderBuilder rBuilder;
67     JSONCPP_STRING err;
68     if (!parseFromStream(rBuilder, ifs, &jsonObject, &err)) {
69         PRINT_HILOGE("PrintJsonUtil stream parse error! ErrorInfo: %{public}s", err.c_str());
70         return false;
71     }
72     return true;
73 }
74 
WriteString(const Json::Value & jsonObject)75 inline std::string PrintJsonUtil::WriteString(const Json::Value &jsonObject)
76 {
77     Json::StreamWriterBuilder wBuilder;
78     wBuilder["indentation"] = "";
79     return Json::writeString(wBuilder, jsonObject);
80 }
81 
WriteStringUTF8(const Json::Value & jsonObject)82 inline std::string PrintJsonUtil::WriteStringUTF8(const Json::Value &jsonObject)
83 {
84     Json::StreamWriterBuilder wBuilder;
85     wBuilder["indentation"] = "";
86     wBuilder["emitUTF8"] = true;
87     return Json::writeString(wBuilder, jsonObject);
88 }
89 }
90 
91 #endif // HARMONYOSDEV_PRINT_JSON_HELPER_H