1 /* 2 * Copyright (c) 2022-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 #include "json_parser.h" 16 17 #include "usage_event_common.h" 18 19 namespace OHOS { 20 namespace HiviewDFX { 21 namespace { 22 const std::vector<std::string> PLUGIN_STATS_FIELDS = { 23 PluginStatsEventSpace::KEY_OF_PLUGIN_NAME, 24 PluginStatsEventSpace::KEY_OF_AVG_TIME, 25 PluginStatsEventSpace::KEY_OF_TOP_K_TIME, 26 PluginStatsEventSpace::KEY_OF_TOP_K_EVENT, 27 PluginStatsEventSpace::KEY_OF_TOTAL, 28 PluginStatsEventSpace::KEY_OF_PROC_NAME, 29 PluginStatsEventSpace::KEY_OF_PROC_TIME, 30 PluginStatsEventSpace::KEY_OF_TOTAL_TIME 31 }; 32 const std::vector<std::string> SYS_USAGE_FIELDS = { 33 SysUsageEventSpace::KEY_OF_START, 34 SysUsageEventSpace::KEY_OF_END, 35 SysUsageEventSpace::KEY_OF_POWER, 36 SysUsageEventSpace::KEY_OF_RUNNING 37 }; 38 } ParseJsonString(Json::Value & eventJson,const std::string & jsonStr)39 bool JsonParser::ParseJsonString(Json::Value& eventJson, const std::string& jsonStr) 40 { 41 Json::CharReaderBuilder jsonRBuilder; 42 Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_); 43 std::unique_ptr<Json::CharReader> const reader(jsonRBuilder.newCharReader()); 44 JSONCPP_STRING err; 45 return reader->parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), &eventJson, &err); 46 } 47 CheckJsonValue(const Json::Value & eventJson,const std::vector<std::string> & fields)48 bool JsonParser::CheckJsonValue(const Json::Value& eventJson, const std::vector<std::string>& fields) 49 { 50 for (auto field : fields) { 51 if (!eventJson.isMember(field)) { 52 return false; 53 } 54 } 55 return true; 56 } 57 ParseUInt32(const Json::Value & value)58 uint32_t JsonParser::ParseUInt32(const Json::Value& value) 59 { 60 return value.isUInt() ? value.asUInt() : 0; 61 } 62 ParseUInt64(const Json::Value & value)63 uint64_t JsonParser::ParseUInt64(const Json::Value& value) 64 { 65 return value.isUInt64() ? value.asUInt64() : 0; 66 } 67 ParseString(const Json::Value & value)68 std::string JsonParser::ParseString(const Json::Value& value) 69 { 70 return value.isString() ? value.asString() : ""; 71 } 72 ParseUInt32Vec(const Json::Value & root,std::vector<uint32_t> & vec)73 void JsonParser::ParseUInt32Vec(const Json::Value& root, std::vector<uint32_t>& vec) 74 { 75 if (!root.isArray()) { 76 return; 77 } 78 for (size_t i = 0; i < root.size(); ++i) { 79 vec.push_back(ParseUInt32(root[static_cast<int>(i)])); 80 } 81 } 82 ParseStringVec(const Json::Value & root,std::vector<std::string> & vec)83 void JsonParser::ParseStringVec(const Json::Value& root, std::vector<std::string>& vec) 84 { 85 if (!root.isArray()) { 86 return; 87 } 88 for (size_t i = 0; i < root.size(); ++i) { 89 vec.push_back(ParseString(root[static_cast<int>(i)])); 90 } 91 } 92 ParsePluginStatsEvent(std::shared_ptr<LoggerEvent> & event,const std::string & jsonStr)93 bool JsonParser::ParsePluginStatsEvent(std::shared_ptr<LoggerEvent>& event, const std::string& jsonStr) 94 { 95 using namespace PluginStatsEventSpace; 96 Json::Value root; 97 if (!ParseJsonString(root, jsonStr) || !CheckJsonValue(root, PLUGIN_STATS_FIELDS)) { 98 return false; 99 } 100 101 std::vector<uint32_t> topKTimes; 102 ParseUInt32Vec(root[KEY_OF_TOP_K_TIME], topKTimes); 103 event->Update(KEY_OF_TOP_K_TIME, topKTimes); 104 105 std::vector<std::string> topKEvents; 106 ParseStringVec(root[KEY_OF_TOP_K_EVENT], topKEvents); 107 event->Update(KEY_OF_TOP_K_EVENT, topKEvents); 108 109 event->Update(KEY_OF_PLUGIN_NAME, ParseString(root[KEY_OF_PLUGIN_NAME])); 110 event->Update(KEY_OF_TOTAL, ParseUInt32(root[KEY_OF_TOTAL])); 111 event->Update(KEY_OF_PROC_NAME, ParseString(root[KEY_OF_PROC_NAME])); 112 event->Update(KEY_OF_PROC_TIME, ParseUInt32(root[KEY_OF_PROC_TIME])); 113 event->Update(KEY_OF_TOTAL_TIME, ParseUInt64(root[KEY_OF_TOTAL_TIME])); 114 return true; 115 } 116 ParseSysUsageEvent(std::shared_ptr<LoggerEvent> & event,const std::string & jsonStr)117 bool JsonParser::ParseSysUsageEvent(std::shared_ptr<LoggerEvent>& event, const std::string& jsonStr) 118 { 119 using namespace SysUsageEventSpace; 120 Json::Value root; 121 if (!ParseJsonString(root, jsonStr) || !CheckJsonValue(root, SYS_USAGE_FIELDS)) { 122 return false; 123 } 124 event->Update(KEY_OF_START, ParseUInt64(root[KEY_OF_START])); 125 event->Update(KEY_OF_END, ParseUInt64(root[KEY_OF_END])); 126 event->Update(KEY_OF_POWER, ParseUInt64(root[KEY_OF_POWER])); 127 event->Update(KEY_OF_RUNNING, ParseUInt64(root[KEY_OF_RUNNING])); 128 return true; 129 } 130 } // namespace HiviewDFX 131 } // namespace OHOS 132