Search code examples
javaserializationdeserializationfst

Deserializing byte array into Java object in a different JVM


I want to do the below, would it theoretically work? (I am working on coding it and checking but that is taking some time)

  1. Serialize a Java object of Custom class C1 (convert into byte[]) using FST library.
  2. Store the byte[] in Amazon s3 bucket.
  3. Download the byte[] in a different JVM (C1 class is not loaded in the JVM)
  4. De-serialize byte[] to Java object downloaded in step-3.

Question: Would the above work without JVM having the class loaded corresponding to the serialized object?


Solution

  • Serialized Java Objects can only be deserialized iff

    1. The class is loaded in the VM trying to deserialize
    2. The class version in the VM is the same as the version that was used for serialization

    The second point is really critical. Default Java serialization is very brittle because if you update your class, you won't be able to deserialize objects serialized with a previous version.

    The better way to serialize objects is to use XML or JSON. There are popular Java libraries for doing this.