|
| 1 | +/*************************************************************************** |
| 2 | +* Copyright (c) 2019, Martin Renou * |
| 3 | +* * |
| 4 | +* Distributed under the terms of the BSD 3-Clause License. * |
| 5 | +* * |
| 6 | +* The full license is in the file LICENSE, distributed with this software. * |
| 7 | +****************************************************************************/ |
| 8 | + |
| 9 | +#ifndef PYBIND11_JSON_HPP |
| 10 | +#define PYBIND11_JSON_HPP |
| 11 | + |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "behaviortree_cpp/contrib/json.hpp" |
| 16 | + |
| 17 | +#include "pybind11/pybind11.h" |
| 18 | + |
| 19 | +namespace pyjson |
| 20 | +{ |
| 21 | + namespace py = pybind11; |
| 22 | + namespace nl = nlohmann; |
| 23 | + |
| 24 | + inline py::object from_json(const nl::json& j) |
| 25 | + { |
| 26 | + if (j.is_null()) |
| 27 | + { |
| 28 | + return py::none(); |
| 29 | + } |
| 30 | + else if (j.is_boolean()) |
| 31 | + { |
| 32 | + return py::bool_(j.get<bool>()); |
| 33 | + } |
| 34 | + else if (j.is_number_unsigned()) |
| 35 | + { |
| 36 | + return py::int_(j.get<nl::json::number_unsigned_t>()); |
| 37 | + } |
| 38 | + else if (j.is_number_integer()) |
| 39 | + { |
| 40 | + return py::int_(j.get<nl::json::number_integer_t>()); |
| 41 | + } |
| 42 | + else if (j.is_number_float()) |
| 43 | + { |
| 44 | + return py::float_(j.get<double>()); |
| 45 | + } |
| 46 | + else if (j.is_string()) |
| 47 | + { |
| 48 | + return py::str(j.get<std::string>()); |
| 49 | + } |
| 50 | + else if (j.is_array()) |
| 51 | + { |
| 52 | + py::list obj(j.size()); |
| 53 | + for (std::size_t i = 0; i < j.size(); i++) |
| 54 | + { |
| 55 | + obj[i] = from_json(j[i]); |
| 56 | + } |
| 57 | + return obj; |
| 58 | + } |
| 59 | + else // Object |
| 60 | + { |
| 61 | + py::dict obj; |
| 62 | + for (nl::json::const_iterator it = j.cbegin(); it != j.cend(); ++it) |
| 63 | + { |
| 64 | + obj[py::str(it.key())] = from_json(it.value()); |
| 65 | + } |
| 66 | + return obj; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + inline nl::json to_json(const py::handle& obj) |
| 71 | + { |
| 72 | + if (obj.ptr() == nullptr || obj.is_none()) |
| 73 | + { |
| 74 | + return nullptr; |
| 75 | + } |
| 76 | + if (py::isinstance<py::bool_>(obj)) |
| 77 | + { |
| 78 | + return obj.cast<bool>(); |
| 79 | + } |
| 80 | + if (py::isinstance<py::int_>(obj)) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + nl::json::number_integer_t s = obj.cast<nl::json::number_integer_t>(); |
| 85 | + if (py::int_(s).equal(obj)) |
| 86 | + { |
| 87 | + return s; |
| 88 | + } |
| 89 | + } |
| 90 | + catch (...) |
| 91 | + { |
| 92 | + } |
| 93 | + try |
| 94 | + { |
| 95 | + nl::json::number_unsigned_t u = obj.cast<nl::json::number_unsigned_t>(); |
| 96 | + if (py::int_(u).equal(obj)) |
| 97 | + { |
| 98 | + return u; |
| 99 | + } |
| 100 | + } |
| 101 | + catch (...) |
| 102 | + { |
| 103 | + } |
| 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>()); |
| 105 | + } |
| 106 | + if (py::isinstance<py::float_>(obj)) |
| 107 | + { |
| 108 | + return obj.cast<double>(); |
| 109 | + } |
| 110 | + if (py::isinstance<py::bytes>(obj)) |
| 111 | + { |
| 112 | + py::module base64 = py::module::import("base64"); |
| 113 | + return base64.attr("b64encode")(obj).attr("decode")("utf-8").cast<std::string>(); |
| 114 | + } |
| 115 | + if (py::isinstance<py::str>(obj)) |
| 116 | + { |
| 117 | + return obj.cast<std::string>(); |
| 118 | + } |
| 119 | + if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj)) |
| 120 | + { |
| 121 | + auto out = nl::json::array(); |
| 122 | + for (const py::handle value : obj) |
| 123 | + { |
| 124 | + out.push_back(to_json(value)); |
| 125 | + } |
| 126 | + return out; |
| 127 | + } |
| 128 | + if (py::isinstance<py::dict>(obj)) |
| 129 | + { |
| 130 | + auto out = nl::json::object(); |
| 131 | + for (const py::handle key : obj) |
| 132 | + { |
| 133 | + out[py::str(key).cast<std::string>()] = to_json(obj[key]); |
| 134 | + } |
| 135 | + return out; |
| 136 | + } |
| 137 | + throw std::runtime_error("to_json not implemented for this type of object: " + py::repr(obj).cast<std::string>()); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// nlohmann_json serializers |
| 142 | +namespace nlohmann |
| 143 | +{ |
| 144 | + namespace py = pybind11; |
| 145 | + |
| 146 | + #define MAKE_NLJSON_SERIALIZER_DESERIALIZER(T) \ |
| 147 | + template <> \ |
| 148 | + struct adl_serializer<T> \ |
| 149 | + { \ |
| 150 | + inline static void to_json(json& j, const T& obj) \ |
| 151 | + { \ |
| 152 | + j = pyjson::to_json(obj); \ |
| 153 | + } \ |
| 154 | + \ |
| 155 | + inline static T from_json(const json& j) \ |
| 156 | + { \ |
| 157 | + return pyjson::from_json(j); \ |
| 158 | + } \ |
| 159 | + } |
| 160 | + |
| 161 | + #define MAKE_NLJSON_SERIALIZER_ONLY(T) \ |
| 162 | + template <> \ |
| 163 | + struct adl_serializer<T> \ |
| 164 | + { \ |
| 165 | + inline static void to_json(json& j, const T& obj) \ |
| 166 | + { \ |
| 167 | + j = pyjson::to_json(obj); \ |
| 168 | + } \ |
| 169 | + } |
| 170 | + |
| 171 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::object); |
| 172 | + |
| 173 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::bool_); |
| 174 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::int_); |
| 175 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::float_); |
| 176 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::str); |
| 177 | + |
| 178 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::list); |
| 179 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::tuple); |
| 180 | + MAKE_NLJSON_SERIALIZER_DESERIALIZER(py::dict); |
| 181 | + |
| 182 | + MAKE_NLJSON_SERIALIZER_ONLY(py::handle); |
| 183 | + MAKE_NLJSON_SERIALIZER_ONLY(py::detail::item_accessor); |
| 184 | + MAKE_NLJSON_SERIALIZER_ONLY(py::detail::list_accessor); |
| 185 | + MAKE_NLJSON_SERIALIZER_ONLY(py::detail::tuple_accessor); |
| 186 | + MAKE_NLJSON_SERIALIZER_ONLY(py::detail::sequence_accessor); |
| 187 | + MAKE_NLJSON_SERIALIZER_ONLY(py::detail::str_attr_accessor); |
| 188 | + MAKE_NLJSON_SERIALIZER_ONLY(py::detail::obj_attr_accessor); |
| 189 | + |
| 190 | + #undef MAKE_NLJSON_SERIALIZER |
| 191 | + #undef MAKE_NLJSON_SERIALIZER_ONLY |
| 192 | +} |
| 193 | + |
| 194 | +// pybind11 caster |
| 195 | +namespace pybind11 |
| 196 | +{ |
| 197 | + namespace detail |
| 198 | + { |
| 199 | + template <> struct type_caster<nlohmann::json> |
| 200 | + { |
| 201 | + public: |
| 202 | + PYBIND11_TYPE_CASTER(nlohmann::json, _("json")); |
| 203 | + |
| 204 | + bool load(handle src, bool) |
| 205 | + { |
| 206 | + try |
| 207 | + { |
| 208 | + value = pyjson::to_json(src); |
| 209 | + return true; |
| 210 | + } |
| 211 | + catch (...) |
| 212 | + { |
| 213 | + return false; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + static handle cast(nlohmann::json src, return_value_policy /* policy */, handle /* parent */) |
| 218 | + { |
| 219 | + object obj = pyjson::from_json(src); |
| 220 | + return obj.release(); |
| 221 | + } |
| 222 | + }; |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +#endif |
0 commit comments