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