I created a shared library using boost.python and py++. I can instantiate objects from types defined in the library. I want to encode/decode these objects via json.
I use jsonpickle
module. But, it doesn't encode attributes. I did some research. Most probably the problem occurs because encoded object's __dict__
is empty.
Sample class in the shared library:
struct Coordinate
{
int x;
int y;
Coordinate(int aX, int aY);
};
This is python wrapper:
BOOST_PYTHON_MODULE(pyplusplus_test){
bp::class_< Coordinate >( "Coordinate", bp::init< int, int >(( bp::arg("aX"), bp::arg("aY") )) )
.enable_pickling()
.def_readwrite( "x", &Coordinate::x )
.def_readwrite( "y", &Coordinate::y );
//...
}
Code piece from python:
cord = pyplusplus_test.Coordinate(10,10)
cord.x = 23
cord.y = -11
tmpStr = jsonpickle.encode(cord)
print tmpStr
And, the output:
{"py/object": "pyplusplus_test.Coordinate"}
Notice that there is no x
or y
in json output.
Any suggestion?
Thanks.