python - is it possible to extend the set of things that xml-rpc can serialize?

夙愿已清 提交于 2019-12-13 16:48:22

问题


I have seen several questions asking how to send a numpy.ndarray through an xml-rpc call. This cannot be done out of the box because, as stated by the xml-rpc docs, there is a fixed set of supported types, basically limited to build-ins and containers of built-ins.

A simple solution is to convert the numpy array to a list using

<array to send>.tolist()

on the sending side, and convert back to a numpy array using

np.array(<list I received>)

on the receiving side. However, suppose I want to be able to support ndarray directly, without explicit flattening/unflattening. Is there an interface I can implement to make this work? For example, can I extend ndarray (I know that's tricky) with certain flattening/unflattening methods which will be registered with xml-rpc and thus used for serialization/de-serialization?


回答1:


I don't think there are any hooks for extension.

Rather than flatten/unflatten, you could encode/unencode the object -- for instance using pickle, or base64-encoded pickle.

If you do flatten keep in mind that you need:

<array>.shape
<array>.dtype
<array>.flat

Simple dtypes can be converted back and forth to strings; you may need to consider complex dtypes more carefully if you want to support them.

[EDIT] To answer your question succinctly: No -- no extension hooks are available.



来源:https://stackoverflow.com/questions/23399836/python-is-it-possible-to-extend-the-set-of-things-that-xml-rpc-can-serialize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!