47 #ifndef _MIRA_TOOLBOXES_PYTHON_JSON2PYTHON_H_ 48 #define _MIRA_TOOLBOXES_PYTHON_JSON2PYTHON_H_ 56 namespace mira {
namespace python {
64 if ( o.ptr() == Py_None )
66 else if ( o.ptr() == Py_False )
68 else if ( o.ptr() == Py_True )
70 else if ( boost::python::extract<int64>(o).check() )
71 return json::Value(boost::python::extract<int64>(o)());
72 else if ( boost::python::extract<double>(o).check() )
73 return json::Value(boost::python::extract<double>(o)());
74 else if (boost::python::extract<boost::python::list>(o).check()
75 || boost::python::extract< boost::python::tuple >(o).check()
76 || PyAnySet_Check(o.ptr()))
78 if ( PyAnySet_Check(o.ptr()) )
79 o = boost::python::list(o);
80 std::size_t len = boost::python::len(o);
83 for (std::size_t p = 0; p != len; ++p)
86 }
else if (boost::python::extract<boost::python::dict>(o).check())
90 boost::python::object keys = boost::python::list(o.attr(
"keys")());
92 for (std::size_t len = boost::python::len( keys ); len > 0; --len)
94 boost::python::object key = keys[len - 1];
97 if (PyType_IsSubtype((PyTypeObject*)key.ptr()->ob_type, &PyUnicode_Type)) {
98 boost::python::str strFromUnicode(key);
99 strFromUnicode.encode(
"utf-8");
100 keystr = boost::python::extract<std::string>(strFromUnicode)();
102 keystr = boost::python::extract<std::string>(key)();
104 object[keystr] = boost::python::extract<json::Value>(o[key])();
107 }
else if (boost::python::extract<mira::Time>(o).check()) {
108 auto time = boost::python::extract<mira::Time>(o)();
110 }
else if (boost::python::extract<std::string>(o).check())
111 return json::Value(boost::python::extract<std::string>(o)());
112 else if (boost::python::extract<json::Object>(o).check())
113 return boost::python::extract<json::Object>(o)();
114 else if (boost::python::extract<json::Array>(o).check())
115 return boost::python::extract<json::Array>(o)();
119 if (PyType_IsSubtype((PyTypeObject*)o.ptr()->ob_type, &PyUnicode_Type)) {
120 boost::python::str strFromUnicode(o);
121 strFromUnicode.encode(
"utf-8");
122 if (boost::python::extract<std::string>(strFromUnicode).check())
123 return json::Value(boost::python::extract<std::string>(strFromUnicode)());
128 MIRA_THROW(XNotImplemented,
"python::to_json(boost::python::object) got object of unsupported type");
132 if (o.ptr() && o.ptr()->ob_type && o.ptr()->ob_type->tp_name)
133 MIRA_RETHROW(e,
"Whilst converting a Python object of type " 134 << o.ptr()->ob_type->tp_name <<
" to a json::Value");
135 MIRA_RETHROW(e,
"Whilst converting a Python object of unknown type to a json::Value");
144 return boost::python::object();
145 else if (j.type() == json_spirit::bool_type)
146 return boost::python::object(j.get_bool());
147 else if (j.type() == json_spirit::int_type)
148 return boost::python::object(j.get_int64());
149 else if (j.type() == json_spirit::real_type)
150 return boost::python::object(j.get_real());
151 else if (j.type() == json_spirit::str_type)
152 return boost::python::object(j.get_str());
153 else if (j.type() == json_spirit::array_type) {
154 boost::python::list l;
155 foreach(
auto e, j.get_array())
159 else if (j.type() == json_spirit::obj_type) {
160 return boost::python::object(j.get_obj());
163 MIRA_THROW(XNotImplemented,
"python::from_json(const json::Value&) got json value of unsupported type");
166 MIRA_RETHROW(e,
"Whilst converting a json:Value to its equivalent Python type\n" void write(const Value &value, std::ostream &ioStream, bool formatted=false, int precision=-1)
json_spirit::mArray Array
#define MIRA_RETHROW(ex, msg)
#define MIRA_THROW(ex, msg)
json_spirit::mObject Object
boost::python::object from_json(const json::Value &j)
Definition: JSON2Python.h:139
json_spirit::mValue Value
json::Value to_json(boost::python::object o)
Definition: JSON2Python.h:60
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...