Customizing Serialization

Customizing Serialization

If a class contains non-serializable variables, and you need to preserve the state of those variables, you will have to customize the default serialization behavior of the class. You can achieve this by providing an implementation for the readObject() and writeObject() methods in the class. These implementations should manually serialize and deserialize the non-serializable objects. If a class provides an implementation for these methods, the ObjectOutputStream and ObjectInputStream used to read write and read objects of that class will call these methods. For example, consider the TestSerialization class from the Tip “Watch Out for the NotSerializableException”. In a new definition, you will add these methods.

 1.     public void writeObject (ObjectOutputStream out)2.       throws IOException {3. 4.       out.defaultWriteObject();5. 	   out.writeObject(o_.getStr());6.     }7. 8.      public void readObject (ObjectInputStream in) 9.          throws IOException, ClassNotFoundException {10. 	11.    in.defaultReadObject();12.    String str = (String)(in.readObject());13.    this.o_ = new NonSerializableObject(str);14. }

On Line 4, in the method writeObject, you call the method defaultWriteObject(). This method writes out all the non-transient fields. On Line 5, you write out the object o of type NonSerializableObject. Thus, the transient fields will have to be written out separately. Similarly, on Line 11, in the method readObject, the method defaultReadObject reads in the non-transient fields. You read in the transient field on Line 12 and assign it to the object on Line 13.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes