Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize an object of unknown class

I need to print the content of a serialized Java object (e.g. a java.io.Serializable POJO retrieved from cache) without knowing its class a priori.

Being more specific, I do NOT need to instantiate a new object using the classic ObjectInputStream, I just need to obtain a simple human-readable representation (preferrably JSON) of the object without loading its class in the classpath.

In other words, I need to convert a byte[] (the raw serialized object) to a JSON string without reimplementing ObjectInputStream.

like image 972
Cristian Greco Avatar asked Oct 08 '13 21:10

Cristian Greco


People also ask

Which method is used to deserialize an object?

The ObjectInputStream class contains readObject() method for deserializing an object.

How do you serialize and deserialize a class?

For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.

What is the method used to deserialize an object in Python?

Python refers to serialization and deserialization by terms pickling and unpickling respectively. The 'pickle' module bundled with Python's standard library defines functions for serialization (dump() and dumps()) and deserialization (load() and loads()).

What does it mean to deserialize an object?

Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.


1 Answers

This one seems promising: https://github.com/unsynchronized/jdeserialize I haven't tried it though.

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.

like image 158
Michal Vician Avatar answered Oct 05 '22 22:10

Michal Vician