Serialization

  • Converting objects/data structures to a format suitable for storage or transmission (e.g. JSON, Protocol Buffers)
  • We can serialize objects on one platform and deserialize them on another

Parsing

  • Analyzing and extracting meaning from structured data to build data structures (e.g. JSON string → object)

Encoding

  • Converting data between different representations (e.g. Base64, UTF-8)

Transcoding

  • Converting content from one encoded format to another (e.g., MP4 → WebM)

Marshalling and Unmarshalling

  • https://stackoverflow.com/questions/770474/what-is-the-difference-between-serialization-and-marshaling
  • Serialization: transform object state into stream of bytes (JSON, XML…) for saving, sharing, transforming
  • Marshalling: Serialization + CodeBase
    • Usually it used by Java Remote Method Invocation(Java RMI) where you are able to invoke a object’s method which is hosted on remote Java processes
    • Serialization is a part of Marshalling
    • CodeBase: place or URL to class definition where it can be downloaded by ClassLoader
java -Djava.rmi.server.codebase="<some_URL>" -jar <some.jar>

Java standard object Serialization

  • https://www.baeldung.com/java-serialization
  • Marker interface: Serializable
  • Note: Jackson does not use Java standard object serialization
  • Important Classes
    • ObjectInputStream
      • convert stream of bytes to object
      • method: Object readObject()
    • ObjectOutputStream
      • convert object to stream of bytes
      • method: void writeObject(Object o)
  • static fields belong to a class (as opposed to an object) and are not serialized
  • transient fields are not serialized
  • When a class implements the Serializable interface, all its sub-classes are serializable as well (since inheritance will make the subclass Serializable)
    • Conversely, when an field has a reference to another object, these objects must implement Serializable, or else a NotSerializableException will be thrown while performing serialization
    • If one of the fields is an array of objects, then all of these objects must implement Serializable as well, or else a NotSerializableException will be thrown while performing serialization
Person person = new Person();
person.setAge(20);
person.setName("Joe");
 
// Serialization
FileOutputStream fileOutputStream = new FileOutputStream("yourfile.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
 
objectOutputStream.writeObject(person);
objectOutputStream.flush();
objectOutputStream.close();
 
// Deserialization
FileInputStream fileInputStream
  = new FileInputStream("yourfile.txt");
ObjectInputStream objectInputStream
  = new ObjectInputStream(fileInputStream);
Person p2 = (Person) objectInputStream.readObject();
objectInputStream.close(); 
 
// Assertions
assertTrue(p2.getAge() == person.getAge());
assertTrue(p2.getName().equals(person.getName()));

Custom Serialization

  • Use cases:
    • When you want to encrypt important fields of a class
    • When you want to use a more compressed serialization.
  • Java gives us two methods that we can use to customize the serialization process
    • void writeObject(ObjectOutputStream)
    • void readObject(ObjectInputStream)
  • None of these methods are inherited, overridden or overloaded

serialVersionUID

class MyClass implements Serializable {
    private static final long serialVersionUID = 1L; // version 1.0
}

transient (write a practical java example)

Jackson Library

  • Does not require the Serializable interface.
  • Uses JSON (or other formats like XML, YAML, etc.) for serialization and deserialization
  • Works by introspecting the structure of objects via reflection or annotations like @JsonProperty, @JsonIgnore, etc.
  • Produces human-readable JSON output by default

JavaBean