What Is Serialization PDF
What Is Serialization PDF
What Is Serialization PDF
validateAge();
}
@Override
public String toString() {
return String.format("Employee {firstName='%s',
middleName='%s', lastName='%s', age='%s', department='%s'}",
firstName, middleName, lastName, age, department);
}
// Serialization
serialize(empObj);
// Deserialization
Employee deserialisedEmpObj = deserialize();
System.out.println("Object after deserialization => " +
deserialisedEmpObj.toString());
}
// Serialization code
static void serialize(Employee empObj) throws IOException {
try (FileOutputStream fos = new FileOutputStream("data.obj");
ObjectOutputStream oos = new ObjectOutputStream(fos))
{
oos.writeObject(empObj);
}
}
// Deserialization code
static Employee deserialize() throws IOException,
ClassNotFoundException {
try (FileInputStream fis = new FileInputStream("data.obj");
ObjectInputStream ois = new ObjectInputStream(fis))
{
return (Employee) ois.readObject();
}
}
}
All the static fields belong to the class instead of the object, and the
serialization process serialises the object so static fields can not be
serialized.
1. Serialization does not care about access modifiers of the field such as
private . All non-transient and non-static fields are considered part of
an object's persistent state and are eligible for serialisation.
You can use a utility that comes with the JDK distribution called
serialver to see what that code would be by default (it is just the hash
code of the object by default).
But we can override this the default serialization behaviour inside our
Java class and provide some additional logic to enhance the normal
process. This can be done by providing two methods writeObject and
readObject inside the class that we want to serialize:
Even though those specialized private methods are provided, the object
serialization works the same way by calling
ObjectOutputStream.writeObject() or ObjectInputStream.readObject() .
Those private methods can be used for any customization you want to
make in the serialization process, e.g. encryption can be added to the
output and decryption to the input (note that the bytes are written and
read in cleartext with no obfuscation at all). They could be used to add
extra data to the stream, perhaps a company versioning code, the
possibilities are truly limitless.
It means anybody can serialize and deserialize the object of our class. But
what if we do not want our class to be serialized or deserialized e.g. our
class is a singleton and we want to prevent any new object creation,
remember the deserialization process creates a new object.
To stop the serialization for our class, we can once again use the above
private methods to just throw the NotSerializableException . Any attempt
to serialize or deserialise our object will now always result in the
exception being thrown. And since those methods are declared as
private, nobody can override your methods and change them.
Java serialization can also be used to deep clone an object. Java cloning
is the most debatable topic in Java community and it surely does have its
drawbacks but it is still the most popular and easy way of creating a copy
of an object until that object is full filling mandatory conditions of Java
cloning. I have covered cloning in details in a 3 article long Java Cloning
Series which includes articles like Java Cloning And Types Of Cloning
(Shallow And Deep) In Details With Example, Java Cloning — Copy
Constructor Versus Cloning, Java Cloning — Even Copy Constructors Are
Not Sufficient, go ahead and read them if you want to know more about
cloning.
Conclusion
1. Serialization is the process of saving an object’s state to a sequence
of bytes which then can be stored on a file or sent over the network
and deserialization is the process of reconstructing an object from
those bytes.
methods.
You can find the complete source code for this article on this Github
Repository and please feel free to provide your valuable feedback.
. . .
Java Serialization
54 claps
WRIT T EN BY
ProgrammingMitra.com Follow
Injecting and Mocking Measuring Your Heart Add Home Screen Quick Powering the Internet
static frameworks in Rate Using Your Phone’s Actions in Swift and iOS with Base64
Swift Camera and Flutter 13 Vickie Li in T he Startup
Cyril Le Pottier in Afonso Raposo in Better Domenico Nicoli in Better
Tech@OpenClassrooms Programming Programming
Manipulating File Paths CSS for Beginners What’s in the Google Create a REST full API
with Python Cem Eygi in Better PageSpeed Score? using Python and Flask
John Au-Yeung in Python In Programming Csaba Pal in Expedia Group (Part 1)
Plain English Technology Kasun Dissanayake