/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PRINT_JSON_UTIL_H #define PRINT_JSON_UTIL_H #include #include #include #include namespace OHOS::Print { class PrintJsonUtil { public: static bool IsMember(const Json::Value &jsonObject, const std::string &key); static bool Parse(const std::string &root, Json::Value &jsonObject); static bool ParseFromStream(Json::IStream &ifs, Json::Value &jsonObject); static std::string WriteString(const Json::Value &jsonObject); static std::string WriteStringUTF8(const Json::Value &jsonObject); }; /* Json::Value 非object对象直接调用isMember会程序崩溃 */ inline bool PrintJsonUtil::IsMember(const Json::Value &jsonObject, const std::string &key) { if (jsonObject.isObject() || jsonObject.isNull()) { return jsonObject.isMember(key); } return false; } /* string对象,不包含数组,或者对象格式,使用此方法转换 */ inline bool PrintJsonUtil::Parse(const std::string &root, Json::Value &jsonObject) { Json::CharReaderBuilder rBuilder; std::unique_ptr reader(rBuilder.newCharReader()); JSONCPP_STRING err; if (!reader->parse(root.c_str(), root.c_str() + root.length(), &jsonObject, &err)) { PRINT_HILOGE("PrintJsonUtil string parse error! ErrorInfo: %{public}s", err.c_str()); return false; } return true; } /* 文件流转化json,使用此方法转换 */ inline bool PrintJsonUtil::ParseFromStream(Json::IStream &ifs, Json::Value &jsonObject) { Json::CharReaderBuilder rBuilder; JSONCPP_STRING err; if (!parseFromStream(rBuilder, ifs, &jsonObject, &err)) { PRINT_HILOGE("PrintJsonUtil stream parse error! ErrorInfo: %{public}s", err.c_str()); return false; } return true; } inline std::string PrintJsonUtil::WriteString(const Json::Value &jsonObject) { Json::StreamWriterBuilder wBuilder; wBuilder["indentation"] = ""; return Json::writeString(wBuilder, jsonObject); } inline std::string PrintJsonUtil::WriteStringUTF8(const Json::Value &jsonObject) { Json::StreamWriterBuilder wBuilder; wBuilder["indentation"] = ""; wBuilder["emitUTF8"] = true; return Json::writeString(wBuilder, jsonObject); } } #endif // HARMONYOSDEV_PRINT_JSON_HELPER_H