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