Program Listing for File json.hpp

Return to documentation for file (morpheus/_lib/include/morpheus/pybind11/json.hpp)

Copy
Copied!
            

/* * SPDX-FileCopyrightText: Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * 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. */ #pragma once #include "morpheus/utilities/json_types.hpp" #include <nlohmann/detail/macro_scope.hpp> #include <nlohmann/json.hpp> #include <nlohmann/json_fwd.hpp> #include <pybind11/pybind11.h> #include <pybind11/pytypes.h> #include <pymrc/utils.hpp> // NOLINTNEXTLINE(modernize-concat-nested-namespaces) namespace PYBIND11_NAMESPACE { namespace detail { template <> struct type_caster<nlohmann::json> { public: PYBIND11_TYPE_CASTER(nlohmann::json, _("object")); bool load(handle src, bool convert) { if (!src || src.is_none()) { return false; } value = mrc::pymrc::cast_from_pyobject(pybind11::reinterpret_borrow<pybind11::object>(src)); return true; } static handle cast(nlohmann::json src, return_value_policy policy, handle parent) { return mrc::pymrc::cast_from_json(src).release(); } }; template <> struct type_caster<nlohmann::json_dict> { public: PYBIND11_TYPE_CASTER(nlohmann::json_dict, _("dict[str, typing.Any]")); bool load(handle src, bool convert) { if (!src || src.is_none()) { return false; } if (!PyDict_Check(src.ptr())) { return false; } value = static_cast<const nlohmann::json_dict>( mrc::pymrc::cast_from_pyobject(pybind11::reinterpret_borrow<pybind11::object>(src))); return true; } static handle cast(nlohmann::json_dict src, return_value_policy policy, handle parent) { return mrc::pymrc::cast_from_json(src).release(); } }; template <> struct type_caster<nlohmann::json_list> { public: PYBIND11_TYPE_CASTER(nlohmann::json_list, _("list[typing.Any]")); bool load(handle src, bool convert) { if (!src || src.is_none()) { return false; } if (!PyList_Check(src.ptr())) { return false; } value = static_cast<const nlohmann::json_list>( mrc::pymrc::cast_from_pyobject(pybind11::reinterpret_borrow<pybind11::object>(src))); return true; } static handle cast(nlohmann::json_list src, return_value_policy policy, handle parent) { return mrc::pymrc::cast_from_json(src).release(); } }; } // namespace detail } // namespace PYBIND11_NAMESPACE

© Copyright 2024, NVIDIA. Last updated on Apr 25, 2024.