我使用 boost.python 和 py++ 创建了一个共享库。我可以从库中定义的类型实例化对象。我想通过 json 对这些对象进行编码/解码。我使用jsonpickle
模块。但是,它不编码属性。我做了一些研究。最有可能出现问题是因为编码对象__dict__
是空的。
共享库中的示例类:
struct Coordinate
{
int x;
int y;
Coordinate(int aX, int aY);
};
这是python包装器:
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 );
//...
}
来自python的代码片段:
cord = pyplusplus_test.Coordinate(10,10)
cord.x = 23
cord.y = -11
tmpStr = jsonpickle.encode(cord)
print tmpStr
并且,输出:
{"py/object": "pyplusplus_test.Coordinate"}
请注意,json 输出中没有x
or y
。
有什么建议吗?
谢谢。