• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 RENDER_UTIL_JSON_UTIL_H
17 #define RENDER_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 
31 template<typename T, BASE_NS::enable_if_t<BASE_NS::is_same_v<bool, T>, bool> = true>
32 inline bool FromJson(const CORE_NS::json::value& jsonData, T& result)
33 {
34     if (jsonData.is_boolean()) {
35         result = static_cast<T>(jsonData.boolean_);
36         return true;
37     }
38     return false;
39 }
40 
41 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)42 inline bool FromJson(const CORE_NS::json::value& jsonData, T& result)
43 {
44     if (jsonData.is_number()) {
45         result = jsonData.as_number<T>();
46         return true;
47     }
48     return false;
49 }
50 
51 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)52 inline bool FromJson(const CORE_NS::json::value& jsonData, T& result)
53 {
54     if (jsonData.is_string()) {
55         result = BASE_NS::string_view { jsonData.string_ };
56         return true;
57     }
58     return false;
59 }
60 
61 namespace Detail {
62 constexpr const BASE_NS::string_view INVALID_DATATYPE = "Failed to read value, invalid datatype: ";
63 template<typename T>
Convert(const CORE_NS::json::value & value)64 inline T Convert(const CORE_NS::json::value& value)
65 {
66     T result;
67     FromJson(value, result);
68     return result;
69 }
70 
71 template<typename Container, typename OutIt, typename Fn>
Transform(Container && container,OutIt dest,Fn func)72 inline OutIt Transform(Container&& container, OutIt dest, Fn func)
73 {
74     return std::transform(container.begin(), container.end(), dest, func);
75 }
76 } // namespace Detail
77 
78 template<class JsonType, typename T>
FromJson(const JsonType & jsonData,BASE_NS::array_view<T> container)79 inline void FromJson(const JsonType& jsonData, BASE_NS::array_view<T> container)
80 {
81     if (jsonData.is_array()) {
82         const auto view =
83             BASE_NS::array_view(jsonData.array_.data(), BASE_NS::Math::min(jsonData.array_.size(), container.size()));
84         Detail::Transform(view, std::begin(container), [](const JsonType& value) { return Detail::Convert<T>(value); });
85     }
86 }
87 
88 template<class JsonType, typename T>
FromJson(const JsonType & jsonData,BASE_NS::vector<T> & container)89 inline void FromJson(const JsonType& jsonData, BASE_NS::vector<T>& container)
90 {
91     if (jsonData.is_array()) {
92         Detail::Transform(jsonData.array_, std::back_inserter(container),
93             [](const JsonType& value) { return Detail::Convert<T>(value); });
94     }
95 }
96 
97 template<class JsonType, typename T, size_t N>
FromJson(const JsonType & jsonData,T (& container)[N])98 inline void FromJson(const JsonType& jsonData, T (&container)[N])
99 {
100     FromJson(jsonData, BASE_NS::array_view(container));
101 }
102 
103 template<class JsonType, typename T,
104     BASE_NS::enable_if_t<BASE_NS::is_array_v<decltype(T::data)> &&
105                              BASE_NS::is_arithmetic_v<BASE_NS::remove_extent_t<decltype(T::data)>>,
106         bool> = true>
FromJson(const JsonType & jsonData,T & output)107 inline void FromJson(const JsonType& jsonData, T& output)
108 {
109     FromJson(jsonData, output.data);
110 }
111 
112 template<class JsonType, typename T>
SafeFromJsonValue(const JsonType * jsonData,T & output)113 inline void SafeFromJsonValue(const JsonType* jsonData, T& output)
114 {
115     if (jsonData) {
116         FromJson(*jsonData, output);
117     }
118 }
119 RENDER_END_NAMESPACE()
120 #endif // RENDER_UTIL_JSON_UTIL_H
121