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