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 #include "event_json_util.h"
16
17 namespace OHOS {
18 namespace HiviewDFX {
19 namespace EventJsonUtil {
ParseUInt32(const Json::Value & root,const std::string & key)20 uint32_t ParseUInt32(const Json::Value& root, const std::string& key)
21 {
22 return (root.isMember(key) && root[key].isUInt()) ? root[key].asUInt() : 0;
23 }
24
ParseInt(const Json::Value & root,const std::string & key)25 int ParseInt(const Json::Value& root, const std::string& key)
26 {
27 return (root.isMember(key) && root[key].isInt()) ? root[key].asInt() : 0;
28 }
29
ParseString(const Json::Value & root,const std::string & key)30 std::string ParseString(const Json::Value& root, const std::string& key)
31 {
32 return (root.isMember(key) && root[key].isString()) ? root[key].asString() : "";
33 }
34
ParseStrings(const Json::Value & root,const std::string & key,std::unordered_set<std::string> & strs)35 void ParseStrings(const Json::Value& root, const std::string& key, std::unordered_set<std::string>& strs)
36 {
37 if (!root.isMember(key) || !root[key].isArray()) {
38 return;
39 }
40 for (Json::ArrayIndex i = 0; i < root[key].size(); ++i) {
41 if (root[key][i].isString()) {
42 strs.insert(root[key][i].asString());
43 }
44 }
45 }
46
GetJsonObjectFromJsonString(Json::Value & eventJson,const std::string & paramString)47 bool GetJsonObjectFromJsonString(Json::Value& eventJson, const std::string& paramString)
48 {
49 Json::Reader reader(Json::Features::strictMode());
50 if (!reader.parse(paramString, eventJson)) {
51 return false;
52 }
53 if (!eventJson.isObject()) {
54 return false;
55 }
56 return true;
57 }
58 } // namespace EventJsonUtil
59 } // namespace HiviewDFX
60 } // namespace OHOS
61