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 16 #ifndef PANDA_GUARD_UTIL_JSON_UTIL_H 17 #define PANDA_GUARD_UTIL_JSON_UTIL_H 18 19 #include <map> 20 #include <string> 21 22 #include "utils/json_parser.h" 23 24 namespace panda::guard { 25 26 class JsonUtil { 27 public: 28 /** 29 * Obtain the content of a JSON object with a field type of object 30 * @param object json object 31 * @param field field name in json object 32 */ 33 static panda::JsonObject *GetJsonObject(const panda::JsonObject *object, const std::string_view &field); 34 35 /** 36 * Obtain the content of a JSON object with a field type of string 37 * @param object json object 38 * @param field field name in json object 39 * @param optionalField Optional field, default to true (optional). When the field is false, 40 * if there is no information about the field in the JSON object, an error will be reported 41 */ 42 static std::string GetStringValue(const panda::JsonObject *object, const std::string_view &field, 43 bool optionalField = true); 44 45 /** 46 * Obtain the content of a JSON object with a field type of double 47 * @param object json object 48 * @param field field name in json object 49 * @param optionalField Optional field, default to true (optional). When the field is false, 50 * if there is no information about the field in the JSON object, an error will be reported 51 */ 52 static double GetDoubleValue(const panda::JsonObject *object, const std::string_view &field, 53 bool optionalField = true); 54 55 /** 56 * Obtain the content of a JSON object with a field type of bool 57 * @param object json object 58 * @param field field name in json object 59 * @param optionalField Optional field, default to true (optional). When the field is false, 60 * if there is no information about the field in the JSON object, an error will be reported 61 */ 62 static bool GetBoolValue(const panda::JsonObject *object, const std::string_view &field, bool optionalField = true); 63 64 /** 65 * Obtain the content of a JSON object with a field type of string array 66 * @param object json object 67 * @param field field name in json object 68 */ 69 static std::vector<std::string> GetArrayStringValue(const panda::JsonObject *object, const std::string_view &field); 70 71 /** 72 * Obtain the content of a JSON object with a field type of string map 73 * @param object json object 74 * @param field field name in json object 75 */ 76 static std::map<std::string, std::string> GetMapStringValue(const panda::JsonObject *object, 77 const std::string_view &field); 78 }; 79 80 } // namespace panda::guard 81 82 #endif // PANDA_GUARD_UTIL_JSON_UTIL_H 83