Home > Software design >  Get data from serialized Java objects
Get data from serialized Java objects

Time:05-24

I have DB with a lot of data stored there as blobs. Those are basically binary serialized Java objects (not JSON or something easy to parse). AFAIK mostly array-like structures. I have the source code of this web app, but it's ancient and utterly broken, can't build app anymore because of missed dependencies and tools (also I have close to none knowledge of Java). Useful only to check logic it seems...

Question is how to get stored data? I'm not interested in objects itself, only in any meaningful data stored there, so converting it into JSON or similar would be fine. Preferably without programming in Java. Some library in PHP would be ideal.

It looks like Gson is right tool to use, but not clear how exactly to use it in the given situation.

UPD: Example of blob in question (in hex): ACED0005737200136A6176612E7574696C2E41727261794C6973747881D21D99C7619D03000149000473697A6578700000000077040000000078

UPD2: Previous sample actually is an empty ArrayList object, so another sample, not empty: ACED0005737200116A6176612E7574696C2E486173684D61700507DAC1C31660D103000246000A6C6F6164466163746F724900097468726573686F6C6478703F400000000000107708000000000000000078

It seems Node.js java.io package does what's needed, but I'll look for better options for now.

CodePudding user response:

First step is going to be to deserialize it to Java (Assuming that is the mechanism) using existing code. Once you have done that then you can serialize it to whatever you want (JSON,XML,CSV, etc...).

The hardest part is figuring the code to deserialize the values stored. Also if the classes have different serialization Ids than original that may be impossible.

If you post some code with what you have tried you might be able to get some help here.

CodePudding user response:

It looks that there are a couple of Python projects attempting to do this (I didn't try them):

Alternatively, you have to write a Java program that performs the deserialization. After that, you can convert the deserialized Java objects into JSON (for example using the Gson library you mentioned).

If the arrays contain instances of simple classes (like Integer or String) you would be able to do it as in the following minimal example (that uses the hex data you provided):

import com.google.gson.Gson;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;
// commons-codec library included just to parse the hex in the example
import org.apache.commons.codec.binary.Hex;

public class Main {

    public static void main(String[] args) throws Exception {

        String hexBlob = "ACED0005737200136A6176612E7574696C2E41727261794C6973747881D21D99C7619D03000149000473697A6578700000000077040000000078";

        byte[] bytes = Hex.decodeHex(hexBlob.toCharArray());

        Gson gson = new Gson();

        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
            ObjectInputStream objectInputStream = new ObjectInputStream(bais);
            List<Object> list = (List<Object>) objectInputStream.readObject();
            System.out.println(gson.toJson(list));
        }
    }
}

In this case it prints an empty array.

If the arrays contain classes defined by your project, things become more complicated.

For more information about this topic read Introduction to Java Serialization.

  • Related