Java: Stopping Serialization and Deserialization

There are scenario/ situation where we want to avoid our class to be serialized or deserialized. Like: our class is a singleton and we want to prevent any new object creation.

Note: As we know that deserialization process creates a new object.

Two way to stop serialization process

Method-1: By throwing NotSerializableException:

We write two private methods writeObject/ readObject to just throw the NotSerializableException.

1.1) private void writeObject( ObjectOutputStream oos) throws IOException {

throw new NotSerializableException("This object not support Serialization");

}

1.2) private void readObject( ObjectInputStream ois) throws IOException, ClassNotFoundException {

throw new NotSerializableException("This object not support Serialization");

}

Note: Any attempt to serialize/ deserialize will now always throw the exception.

1.3) Issue: However, this is a violation of the Liskov Substitution Principle.


Method-2: By using writeReplace/ readResolve:

2.1) private Object writeReplace() throws ObjectStreamException {

return this.

}

2.2) private Object readResolve() throws ObjectStreamException {

return this;

}

2.3) Description: In ObjectStream, these methods are used to allow an object to provide an alternative representation for itself.

In other words, writeReplace can be used to change the data that is serialized through writeObject. And readResolve can be used to change the data that is desesrialized through the readObject.


******** END ************************************


要查看或添加评论,请登录

Arun Dhwaj的更多文章

  • Data Flow Diagram (DFD): Brief Tutorial

    Data Flow Diagram (DFD): Brief Tutorial

    1) How to create it Step1: Start with definition and purpose. Step2: Explain components with symbols.

    1 条评论
  • Translation of Work to ROI

    Translation of Work to ROI

    Why Translate Your Work to ROI ( Return On Investment) ? 1) It will make easy for you to show your stakeholders, about…

  • Sr. Architect/ Solution Architect Responsibility

    Sr. Architect/ Solution Architect Responsibility

    I) A brief dive of Responsibility 1) Understand the System’s Requirements, 2) Understand the Non-Functional…

  • 7 Tips for Optimising System Performance

    7 Tips for Optimising System Performance

    Step-1:Measure most of the things as much as you can. Step-2: Prioritise based on outcome vs intake/ Cost ( Money/…

  • Great Leader creates DNA/Culture

    Great Leader creates DNA/Culture

    They took the following steps to creates: 1) Routine The purpose of Routine is to creates The Sense of Community and…

  • Leadership: Remotely Leading the Team

    Leadership: Remotely Leading the Team

    1) Be Output Oriented Instead of inputs, be output oriented. Explain the team, they would like to see these…

  • Be on the TOP of the WORLD

    Be on the TOP of the WORLD

    1) Be a Constant Learner Make a habit of learning, learning and learning. There is no alternative to a constant…

  • Troubleshooting/ Debugging: Kubernetes Pods

    Troubleshooting/ Debugging: Kubernetes Pods

    1) Background 1.1) Throughout we will use kubectl command-line utility to interact with K8S.

  • React: Demystifying Component, Props, and State

    React: Demystifying Component, Props, and State

    ******************* 1.0) Part-1: Component - Start ************************* What is React Component? Background: In…

  • Best Practice: Versioning REST API

    Best Practice: Versioning REST API

    1) When we should do the Versioning of API Versioning (REST) API is often a last priority during development process…

社区洞察

其他会员也浏览了