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 CORE__LOADER__JSON_UTIL_H
17 #define CORE__LOADER__JSON_UTIL_H
18
19 #include <algorithm>
20 #include <cstdlib>
21
22 #include <base/containers/array_view.h>
23 #include <base/containers/string.h>
24 #include <base/containers/string_view.h>
25 #include <base/containers/type_traits.h>
26 #include <base/namespace.h>
27 #include <core/json/json.h>
28 #include <core/namespace.h>
29
CORE_BEGIN_NAMESPACE()30 CORE_BEGIN_NAMESPACE()
31 template<typename T, BASE_NS::enable_if_t<BASE_NS::is_same_v<bool, T>, bool> = true>
32 inline void from_json(const json::value& jsonData, T& result)
33 {
34 if (jsonData.is_boolean()) {
35 result = static_cast<T>(jsonData.boolean_);
36 }
37 }
38
39 template<typename T, BASE_NS::enable_if_t<!BASE_NS::is_same_v<bool, T> && BASE_NS::is_arithmetic_v<T>, bool> = true>
from_json(const json::value & jsonData,T & result)40 inline void from_json(const json::value& jsonData, T& result)
41 {
42 if (jsonData.is_number()) {
43 result = jsonData.as_number<T>();
44 }
45 }
46
47 template<typename T, BASE_NS::enable_if_t<BASE_NS::is_convertible_v<T, BASE_NS::string_view>, bool> = true>
from_json(const CORE_NS::json::value & jsonData,T & result)48 inline bool from_json(const CORE_NS::json::value& jsonData, T& result)
49 {
50 if (jsonData.is_string()) {
51 result = BASE_NS::string_view { jsonData.string_ };
52 return true;
53 }
54 return false;
55 }
56
57 namespace Detail {
58 template<typename T>
Convert(const json::value & value)59 inline T Convert(const json::value& value)
60 {
61 T result;
62 from_json(value, result);
63 return result;
64 }
65
66 template<typename Container, typename OutIt, typename Fn>
Transform(Container && container,OutIt dest,Fn func)67 inline OutIt Transform(Container&& container, OutIt dest, Fn func)
68 {
69 return std::transform(container.begin(), container.end(), dest, func);
70 }
71 } // namespace Detail
72
73 template<typename T>
from_json(const json::value & jsonData,BASE_NS::array_view<T> container)74 inline void from_json(const json::value& jsonData, BASE_NS::array_view<T> container)
75 {
76 if (jsonData.is_array()) {
77 const auto view =
78 BASE_NS::array_view(jsonData.array_.data(), BASE_NS::Math::min(jsonData.array_.size(), container.size()));
79 Detail::Transform(view, container.begin(), Detail::Convert<T>);
80 }
81 }
82
83 template<typename T, size_t N>
from_json(const json::value & jsonData,T (& container)[N])84 inline void from_json(const json::value& jsonData, T (&container)[N])
85 {
86 if (jsonData.is_array()) {
87 const auto view = BASE_NS::array_view(jsonData.array_.data(), BASE_NS::Math::min(jsonData.array_.size(), N));
88 Detail::Transform(view, std::begin(container), Detail::Convert<T>);
89 }
90 }
91
92 template<typename T, BASE_NS::enable_if_t<BASE_NS::is_arithmetic_v<T>, bool> = true>
SafeGetJsonValue(const json::value & jsonData,const BASE_NS::string_view element,BASE_NS::string & error,T & output)93 bool SafeGetJsonValue(
94 const json::value& jsonData, const BASE_NS::string_view element, BASE_NS::string& error, T& output)
95 {
96 if (auto const pos = jsonData.find(element); pos) {
97 if constexpr (BASE_NS::is_same_v<bool, T>) {
98 if (pos->is_boolean()) {
99 output = pos->boolean_;
100 return true;
101 }
102 } else {
103 if (pos->is_number()) {
104 output = pos->as_number<T>();
105 return true;
106 }
107 }
108 error += element + ": expected number.\n";
109 }
110 return false;
111 }
112
113 template<class T, BASE_NS::enable_if_t<BASE_NS::is_convertible_v<T, BASE_NS::string_view>, bool> = true>
SafeGetJsonValue(const json::value & jsonData,const BASE_NS::string_view element,BASE_NS::string & error,T & output)114 bool SafeGetJsonValue(
115 const json::value& jsonData, const BASE_NS::string_view element, BASE_NS::string& error, T& output)
116 {
117 if (auto const pos = jsonData.find(element); pos) {
118 if (pos->is_string()) {
119 output = BASE_NS::string_view { pos->string_ };
120 return true;
121 } else {
122 error += element + ": expected string.\n";
123 }
124 }
125 return false;
126 }
127 CORE_END_NAMESPACE()
128
129 #endif