9 #ifndef PYBIND11_JSON_HPP
10 #define PYBIND11_JSON_HPP
17 #include "pybind11/pybind11.h"
32 return py::bool_(j.
get<
bool>());
44 return py::float_(j.
get<
double>());
48 return py::str(j.
get<std::string>());
52 py::list obj(j.
size());
53 for (std::size_t i = 0; i < j.
size(); i++)
57 return std::move(obj);
64 obj[py::str(it.key())] =
from_json(it.value());
66 return std::move(obj);
72 if (obj.ptr() ==
nullptr || obj.is_none())
76 if (py::isinstance<py::bool_>(obj))
78 return obj.cast<
bool>();
80 if (py::isinstance<py::int_>(obj))
85 if (py::int_(s).equal(obj))
96 if (py::int_(u).equal(obj))
104 throw std::runtime_error(
"to_json received an integer out of range for both nl::json::number_integer_t and nl::json::number_unsigned_t type: " + py::repr(obj).cast<std::string>());
106 if (py::isinstance<py::float_>(obj))
108 return obj.cast<
double>();
110 if (py::isinstance<py::bytes>(obj))
112 py::module base64 = py::module::import(
"base64");
113 return base64.attr(
"b64encode")(obj).attr(
"decode")(
"utf-8").cast<std::string>();
115 if (py::isinstance<py::str>(obj))
117 return obj.cast<std::string>();
119 if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
122 for (
const py::handle value : obj)
128 if (py::isinstance<py::dict>(obj))
131 for (
const py::handle key : obj)
133 out[py::str(key).cast<std::string>()] =
to_json(obj[key]);
137 throw std::runtime_error(
"to_json not implemented for this type of object: " + py::repr(obj).cast<std::string>());
144 #define MAKE_NLJSON_SERIALIZER_DESERIALIZER(T) \
146 struct adl_serializer<T> \
148 inline static void to_json(json& j, const T& obj) \
150 j = pyjson::to_json(obj); \
153 inline static T from_json(const json& j) \
155 return pyjson::from_json(j); \
159 #define MAKE_NLJSON_SERIALIZER_ONLY(T) \
161 struct adl_serializer<T> \
163 inline static void to_json(json& j, const T& obj) \
165 j = pyjson::to_json(obj); \
188 #undef MAKE_NLJSON_SERIALIZER
189 #undef MAKE_NLJSON_SERIALIZER_ONLY
218 return obj.release();
a class to store JSON values
basic_json get() const
get special-case overload
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
NumberIntegerType number_integer_t
a type for a number (integer)
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
const_iterator cend() const noexcept
returns a const iterator to one past the last element
size_type size() const noexcept
returns the number of elements
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
constexpr bool is_boolean() const noexcept
return whether value is a boolean
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
constexpr bool is_string() const noexcept
return whether value is a string
constexpr bool is_array() const noexcept
return whether value is an array
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
NumberUnsignedType number_unsigned_t
a type for a number (unsigned)
const_iterator cbegin() const noexcept
returns a const iterator to the first element
iter_impl< const basic_json > const_iterator
a const iterator for a basic_json container
constexpr bool is_null() const noexcept
return whether value is null
namespace for Niels Lohmann
MAKE_NLJSON_SERIALIZER_ONLY(py::handle)
MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::object)
nl::json to_json(const py::handle &obj)
py::object from_json(const nl::json &j)
PYBIND11_TYPE_CASTER(nl::json, _("json"))
bool load(handle src, bool)
static handle cast(nl::json src, return_value_policy, handle)