Javatpoint Logo
Javatpoint Logo

Unmarshalling in Java

Deserialization, commonly called unmarshalling, returns serialized data to its original form so that an application may utilize it. Through the Serializable and Externalizable interfaces, which are used to turn objects into streams of bytes and back again, Java supports serialization and deserialization.

Custom Serialisation

By implementing the Serializable interface and providing unique readObject() and writeObject() methods, Java enables you to customize the serialization process. This might be helpful for addressing unique situations like transitory fields, circular references, and encryption.

Externalizable Interface

Java also has an Externalizable interface, which gives users even more control over the serialization and deserialization process and the Serializable interface. Two methods that define how the object's state is written to and read from a stream are required for externalizable objects to implement: writeExternal() and readExternal().

ObjectInputStream

The ObjectInputStream class offers a means for checking the serialized data during deserialization. ObjectInputStream Validation. You may provide unique tests to ensure the data is correct before it is deserialized by implementing the validateObject() function in the class of the serialized object.

Versioning and Serialised Form

When serializing and deserializing things, it's crucial to consider the compatibility between various versions of the object's class. An object's serialized form contains information about its class, including the name, serialVersionUID, field names, and contents. You may guarantee that serialized objects are always compatible across multiple versions of your program by carefully controlling the versioning of your classes and utilizing version control tools.

Serialisation and Deserialization

An item is transformed into a stream of bytes known as serialization, which may then be saved in a file, sent over a network, or stored in a database. Developers can implement serialization for their classes using Java's Serializable and Externalizable interfaces.

Why unmarshalling is crucial?

A key component of Java serialization is unmarshalling. Without losing their state, it enables the transfer of objects between various applications, platforms, or languages. For instance, if both apps have access to the class definition of the serialized object, an object may be serialized in one application and deserialized in another.

Unmarshalling is also helpful when preserving items in a database or cache system. Objects can be serialized and saved as byte arrays rather than stored in their unprocessed state. When required, time and resources can be saved by deserializing the object from the byte array.

Implementation of Unmarshalling

Java has built-in support for unmarshalling through the ObjectInputStream class, which deserializes objects from a stream of bytes.

Create an ObjectInputStream object and use its readObject() function to read the object from the input stream to deserialize an object. Here's an illustration:

File name: Employee.java

Unmarshalling.java

Employee.ser

Output:

Deserialized Employee object:
Name: John Doe
ID: 12345
Salary: 50000.0

Explanation

This result verifies that the Employee object was properly deserialized and that the serialized values of its fields were applied correctly. The program would print an IOException stack trace if the file "employee. ser" did not exist or was empty. The program would generate a ClassNotFoundException stack trace if the serialized object in the file is of an unknown class or has been altered during serialization.

Conclusion

Unmarshalling, a crucial component of Java serialization, enables the transfer of objects between applications, platforms, or languages without causing them to lose their state. Java has built-in support for unmarshalling through the ObjectInputStream class that deserializes objects from a stream of bytes. In this article, we examined the Java implementation of unmarshalling and gave a detailed illustration of the procedure.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA