/* * 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 CORE__LOADER__JSON_UTIL_H #define CORE__LOADER__JSON_UTIL_H #include #include #include #include #include #include #include #include #include CORE_BEGIN_NAMESPACE() template, bool> = true> inline void from_json(const json::value& jsonData, T& result) { if (jsonData.is_boolean()) { result = static_cast(jsonData.boolean_); } } template && BASE_NS::is_arithmetic_v, bool> = true> inline void from_json(const json::value& jsonData, T& result) { if (jsonData.is_number()) { result = jsonData.as_number(); } } template, bool> = true> inline bool from_json(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 { template inline T Convert(const json::value& value) { T result; from_json(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 from_json(const json::value& 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, container.begin(), Detail::Convert); } } template inline void from_json(const json::value& jsonData, T (&container)[N]) { if (jsonData.is_array()) { const auto view = BASE_NS::array_view(jsonData.array_.data(), BASE_NS::Math::min(jsonData.array_.size(), N)); Detail::Transform(view, std::begin(container), Detail::Convert); } } template, bool> = true> bool SafeGetJsonValue( const json::value& jsonData, const BASE_NS::string_view element, BASE_NS::string& error, T& output) { if (auto const pos = jsonData.find(element); pos) { if constexpr (BASE_NS::is_same_v) { if (pos->is_boolean()) { output = pos->boolean_; return true; } } else { if (pos->is_number()) { output = pos->as_number(); return true; } } error += element + ": expected number.\n"; } return false; } template, bool> = true> bool SafeGetJsonValue( const 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 = BASE_NS::string_view { pos->string_ }; return true; } else { error += element + ": expected string.\n"; } } return false; } CORE_END_NAMESPACE() #endif