/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef UTIL__JSON_UTIL_H #define UTIL__JSON_UTIL_H #include #include #include #include #include #include #include #include RENDER_BEGIN_NAMESPACE() template, bool> = true> inline bool SafeGetJsonValue( const CORE_NS::json::value& jsonData, const BASE_NS::string_view element, BASE_NS::string& error, T& output) { if (auto const pos = jsonData.find(element); pos) { if (pos->is_number()) { output = pos->as_number(); return true; } else { error += element + ": expected number.\n"; } } return false; } template, bool> = true> inline bool SafeGetJsonValue( const CORE_NS::json::value& jsonData, const BASE_NS::string_view element, BASE_NS::string& error, T& output) { if (auto const pos = jsonData.find(element); pos) { if (pos->is_string()) { output = T(pos->string_.data(), pos->string_.size()); return true; } else { error += element + ": expected string.\n"; } } return false; } template, bool> = true> inline bool FromJson(const CORE_NS::json::value& jsonData, T& result) { if (jsonData.is_boolean()) { result = static_cast(jsonData.boolean_); return true; } return false; } template && BASE_NS::is_arithmetic_v, bool> = true> inline bool FromJson(const CORE_NS::json::value& jsonData, T& result) { if (jsonData.is_number()) { result = jsonData.as_number(); return true; } return false; } template, bool> = true> inline bool FromJson(const CORE_NS::json::value& jsonData, T& result) { if (jsonData.is_string()) { result = BASE_NS::string_view { jsonData.string_ }; return true; } return false; } namespace Detail { constexpr const BASE_NS::string_view INVALID_DATATYPE = "Failed to read value, invalid datatype: "; template inline T Convert(const CORE_NS::json::value& value) { T result; FromJson(value, result); return result; } template inline OutIt Transform(Container&& container, OutIt dest, Fn func) { return std::transform(container.begin(), container.end(), dest, func); } } // namespace Detail template inline void FromJson(const JsonType& jsonData, BASE_NS::array_view container) { if (jsonData.is_array()) { const auto view = BASE_NS::array_view(jsonData.array_.data(), BASE_NS::Math::min(jsonData.array_.size(), container.size())); Detail::Transform(view, std::begin(container), [](const JsonType& value) { return Detail::Convert(value); }); } } template inline void FromJson(const JsonType& jsonData, BASE_NS::vector& container) { if (jsonData.is_array()) { Detail::Transform(jsonData.array_, std::back_inserter(container), [](const JsonType& value) { return Detail::Convert(value); }); } } template inline void FromJson(const JsonType& jsonData, T (&container)[N]) { FromJson(jsonData, BASE_NS::array_view(container)); } template && BASE_NS::is_arithmetic_v>, bool> = true> inline void FromJson(const JsonType& jsonData, T& output) { FromJson(jsonData, output.data); } RENDER_END_NAMESPACE() #endif // UTIL__JSON_UTIL_H