forked from ukaea/parallel-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpybind_json.hpp
More file actions
181 lines (163 loc) · 5.58 KB
/
pybind_json.hpp
File metadata and controls
181 lines (163 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/***************************************************************************
* Copyright (c) 2019, Martin Renou *
* source downloaded from https://github.com/martinRenou/pybind_json *
* auto pyobject conversion, test example are added by Qingfeng Xia, 2020
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef PYBIND_JSON_HPP
#define PYBIND_JSON_HPP
#include <string>
#include <vector>
#include "./nlohmann/json.hpp"
#include <pybind11/pybind11.h>
namespace py = pybind11;
namespace nlohmann
{
template <> struct adl_serializer<py::object>
{
static py::object from_json(const json& j);
static void to_json(json& j, const py::object& obj);
};
namespace detail
{
inline py::object from_json_impl(const json& j)
{
if (j.is_null())
{
return py::none();
}
else if (j.is_boolean())
{
return py::bool_(j.get<bool>());
}
else if (j.is_number())
{
double number = j.get<double>();
if (number == std::floor(number))
{
return py::int_(j.get<long>());
}
else
{
return py::float_(number);
}
}
else if (j.is_string())
{
return py::str(j.get<std::string>());
}
else if (j.is_array())
{
py::list obj;
for (const auto& el : j)
{
obj.attr("append")(from_json_impl(el));
}
return obj;
}
else // Object
{
py::dict obj;
for (json::const_iterator it = j.cbegin(); it != j.cend(); ++it)
{
obj[py::str(it.key())] = from_json_impl(it.value());
}
return obj;
}
}
inline json to_json_impl(const py::handle& obj)
{
if (obj.is_none())
{
return nullptr;
}
if (py::isinstance<py::bool_>(obj))
{
return obj.cast<bool>();
}
if (py::isinstance<py::int_>(obj))
{
return obj.cast<long>();
}
if (py::isinstance<py::float_>(obj))
{
return obj.cast<double>();
}
if (py::isinstance<py::str>(obj))
{
return obj.cast<std::string>();
}
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
{
auto out = json::array();
for (const py::handle& value : obj)
{
out.push_back(to_json_impl(value));
}
return out;
}
if (py::isinstance<py::dict>(obj))
{
auto out = json::object();
for (const py::handle& key : obj)
{
out[py::str(key).cast<std::string>()] = to_json_impl(obj[key]);
}
return out;
}
throw std::runtime_error("to_json not implemented for this type of object: " + obj.cast<std::string>());
}
} // namespace detail
inline py::object adl_serializer<py::object>::from_json(const json& j)
{
return detail::from_json_impl(j);
}
inline void adl_serializer<py::object>::to_json(json& j, const py::object& obj)
{
j = detail::to_json_impl(obj);
}
} // namespace nlohmann
namespace pybind11
{
namespace detail
{
template <> struct type_caster<nlohmann::json>
{
public:
/**
* This macro establishes the name 'user_type' in
* function signatures and declares a local variable 'value' of user type
*/
PYBIND11_TYPE_CASTER(nlohmann::json, _("json"));
/**
* Conversion part 1 (Python->C++): convert a PyObject into a inty
* instance or return false upon failure. The second argument
* indicates whether implicit conversions should be applied.
*/
bool load(handle src, bool)
{
/* Extract PyObject from handle */
/* Now try to convert into a C++ type */
value = nlohmann::detail::to_json_impl(src);
if (value.is_null())
return false;
return true;
}
/**
* Conversion part 2 (C++ -> Python): convert an inty instance into
* a Python object. The second and third arguments are used to
* indicate the return value policy and parent object (for
* ``return_value_policy::reference_internal``) and are generally
* ignored by implicit casters.
*/
static handle cast(nlohmann::json src, return_value_policy /* policy */, handle /* parent */)
{
object pyo = nlohmann::detail::from_json_impl(src);
return pyo.release();
}
};
} // namespace detail
} // namespace pybind11
#endif