我需要打印序列化 Java 对象的内容(例如,从缓存中检索到的 java.io.Serializable POJO),但事先不知道其类。
更具体地说,我不需要使用经典的 ObjectInputStream 实例化一个新对象,我只需要获得一个简单的人类可读的对象表示(最好是 JSON),而不需要在类路径中加载它的类。
换句话说,我需要在不重新实现 ObjectInputStream 的情况下将 byte[](原始序列化对象)转换为 JSON 字符串。
最佳答案
这个看起来很有希望:https://github.com/unsynchronized/jdeserialize 不过我还没有尝试过。
It is a full implementation of the Object Serialization Stream Protocol, as described in the Java Object Serialization Specification, chapter 6. It does not instantiate any classes described in the stream; instead, it builds up an intermediate representation of the types, instances, and values. Because of this, it can analyze streams without access to the class code that generated them.
关于java - 如何反序列化未知类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19258433/