用不同的语言反序列化 [英] Deserialize in a different language

查看:107
本文介绍了用不同的语言反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

log4j网络适配器将事件作为序列化的java对象发送。我希望能够捕获此对象并使用其他语言(python)对其进行反序列化。这可能吗?

The log4j network adapter sends events as a serialised java object. I would like to be able to capture this object and deserialise it in a different language (python). Is this possible?

注意网络捕获很容易;它只是一个TCP套接字并在流中读取。困难在于反序列化部分

NOTE The network capturing is easy; its just a TCP socket and reading in a stream. The difficulty is the deserialising part

推荐答案

一般来说,没有。

Java序列化的流格式在本文档中中定义,但您需要访问原始类定义(以及将其加载到Java运行时)以将流数据转换回接近原始对象的内容。例如,类可以定义writeObject()和readObject()方法来自定义自己的序列化表单。

The stream format for Java serialization is defined in this document, but you need access to the original class definitions (and a Java runtime to load them into) to turn the stream data back into something approaching the original objects. For example, classes may define writeObject() and readObject() methods to customise their own serialized form.

编辑: lubos hasko建议拥有一个小的java程序来反序列化Python前面的对象,但问题是为了使它工作,你的小java程序需要加载它可能反序列化的所有相同类的相同版本。如果你这很棘手从一个应用程序接收日志消息,如果你要多路复用多个日志流,那真的很棘手。无论哪种方式,它都不再是一个小程序。 edit2:我可能是错的在这里,我不知道什么是序列化的。如果它只是log4j类你应该没问题。另一方面,它可以记录任意异常,如果它们被放入流中,我的观点就是。)

(edit: lubos hasko suggests having a little java program to deserialize the objects in front of Python, but the problem is that for this to work, your "little java program" needs to load the same versions of all the same classes that it might deserialize. Which is tricky if you're receiving log messages from one app, and really tricky if you're multiplexing more than one log stream. Either way, it's not going to be a little program any more. edit2: I could be wrong here, I don't know what gets serialized. If it's just log4j classes you should be fine. On the other hand, it's possible to log arbitrary exceptions, and if they get put in the stream as well my point stands.)

定制log4j网络适配器并用更容易的deserial替换原始序列化会容易得多形式化(例如,您可以使用XStream将对象转换为XML表示形式)

It would be much easier to customise the log4j network adapter and replace the raw serialization with some more easily-deserialized form (for example you could use XStream to turn the object into an XML representation)

这篇关于用不同的语言反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆