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 #include "json_util.h"
17
18 #include <optional>
19 #include "util/assert_util.h"
20
21 namespace {
22 constexpr std::string_view TAG = "[JsonUtil]";
23
24 template <typename T>
GetJsonValue(const panda::JsonObject * object,const std::string_view & field)25 std::optional<T> GetJsonValue(const panda::JsonObject *object, const std::string_view &field)
26 {
27 auto value = object->GetValue<T>(field.data());
28 if (!value) {
29 return std::nullopt;
30 }
31 return *value;
32 }
33 } // namespace
34
GetJsonObject(const panda::JsonObject * object,const std::string_view & field)35 panda::JsonObject *panda::guard::JsonUtil::GetJsonObject(const panda::JsonObject *object, const std::string_view &field)
36 {
37 auto value = object->GetValue<panda::JsonObject::JsonObjPointer>(field.data());
38 if (!value) {
39 return nullptr;
40 }
41 return value->get();
42 }
43
GetStringValue(const panda::JsonObject * object,const std::string_view & field,bool optionalField)44 std::string panda::guard::JsonUtil::GetStringValue(const panda::JsonObject *object, const std::string_view &field,
45 bool optionalField)
46 {
47 auto result = GetJsonValue<panda::JsonObject::StringT>(object, field);
48 if (result.has_value()) {
49 return result.value();
50 }
51 PANDA_GUARD_ASSERT_PRINT(!optionalField, TAG, ErrorCode::NOT_CONFIGURED_REQUIRED_FIELD,
52 "fail to obtain required field: " << field << " from json file");
53 return "";
54 }
55
GetDoubleValue(const panda::JsonObject * object,const std::string_view & field,bool optionalField)56 double panda::guard::JsonUtil::GetDoubleValue(const panda::JsonObject *object, const std::string_view &field,
57 bool optionalField)
58 {
59 auto result = GetJsonValue<panda::JsonObject::NumT>(object, field);
60 if (result.has_value()) {
61 return result.value();
62 }
63 PANDA_GUARD_ASSERT_PRINT(!optionalField, TAG, ErrorCode::NOT_CONFIGURED_REQUIRED_FIELD,
64 "fail to obtain required field: " << field << " from json file");
65 return 0;
66 }
67
GetBoolValue(const panda::JsonObject * object,const std::string_view & field,bool optionalField)68 bool panda::guard::JsonUtil::GetBoolValue(const panda::JsonObject *object, const std::string_view &field,
69 bool optionalField)
70 {
71 auto result = GetJsonValue<panda::JsonObject::BoolT>(object, field);
72 if (result.has_value()) {
73 return result.value();
74 }
75 PANDA_GUARD_ASSERT_PRINT(!optionalField, TAG, ErrorCode::NOT_CONFIGURED_REQUIRED_FIELD,
76 "fail to obtain required field: " << field << " from json file");
77 return false;
78 }
79
GetArrayStringValue(const panda::JsonObject * object,const std::string_view & field)80 std::vector<std::string> panda::guard::JsonUtil::GetArrayStringValue(const panda::JsonObject *object,
81 const std::string_view &field)
82 {
83 std::vector<std::string> res;
84 auto arrValues = object->GetValue<panda::JsonObject::ArrayT>(field.data());
85 if (!arrValues) {
86 return res;
87 }
88 res.reserve(arrValues->size());
89 for (auto &value : *arrValues) {
90 res.emplace_back(*(value.Get<panda::JsonObject::StringT>()));
91 }
92 return res;
93 }
94
GetMapStringValue(const panda::JsonObject * object,const std::string_view & field)95 std::map<std::string, std::string> panda::guard::JsonUtil::GetMapStringValue(const panda::JsonObject *object,
96 const std::string_view &field)
97 {
98 std::map<std::string, std::string> res;
99 auto mapObj = GetJsonObject(object, field);
100 if (!mapObj) {
101 return res;
102 }
103 for (size_t idx = 0; idx < mapObj->GetSize(); idx++) {
104 auto mapKey = mapObj->GetKeyByIndex(idx);
105 auto mapValue = GetStringValue(mapObj, mapKey);
106 res.emplace(mapKey, mapValue);
107 }
108 return res;
109 }
110