Serialization & Deserialization in Java
Serialization & Deserialization in Java
𝐒𝐞𝐫𝐢𝐚𝐥𝐢𝐳𝐚𝐭𝐢𝐨𝐧
𝐚𝐧𝐝
𝐃𝐞𝐬𝐞𝐫𝐢𝐚𝐥𝐢𝐳𝐚𝐭𝐢𝐨𝐧
𝐢𝐧 𝐉𝐚𝐯𝐚
𝗪𝗵𝗮𝘁 𝗶𝘀
𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻?
Serialization is the process of
converting an object into a byte
stream. This byte stream can then be:
𝗪𝗵𝗮𝘁 𝗶𝘀
𝗗𝗲𝘀𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻?
Deserialization is the reverse
process of Serialization. It
reconstructs an object from a byte
stream, restoring its state in
memory.
𝗛𝗼𝘄 𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻
𝗪𝗼𝗿𝗸𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮?
1. 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝘁𝗵𝗲 𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝗯𝗹𝗲
𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲 :
A class must implement the
𝗷𝗮𝘃𝗮.𝗶𝗼.𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝗯𝗹𝗲 interface to
allow its objects to be serialized.
2. 𝗨𝘀𝗲 𝗢𝗯𝗷𝗲𝗰𝘁𝗢𝘂𝘁𝗽𝘂𝘁𝗦𝘁𝗿𝗲𝗮𝗺 :
Write the serialized object to a file or
output stream.
3. 𝗨𝘀𝗲 𝗢𝗯𝗷𝗲𝗰𝘁𝗜𝗻𝗽𝘂𝘁𝗦𝘁𝗿𝗲𝗮𝗺 :
Read the serialized object from
a file or input stream.
1. Serialization
2. Deserialization
𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 𝗼𝗳
𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻
1. Save objects to disk for
persistence.
𝗟𝗶𝗺𝗶𝘁𝗮𝘁𝗶𝗼𝗻𝘀 𝗼𝗳
𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻
1. 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗼𝘃𝗲𝗿𝗵𝗲𝗮𝗱:
Serialization is slower than
other data storage methods.
2. 𝗖𝗼𝗺𝗽𝗮𝘁𝗶𝗯𝗶𝗹𝗶𝘁𝘆 𝗶𝘀𝘀𝘂𝗲𝘀:
Changes to a class can break
deserialization.
3. 𝗦𝗲𝗰𝘂𝗿𝗶𝘁𝘆 𝗰𝗼𝗻𝗰𝗲𝗿𝗻𝘀:
Serialized data can be
exploited if not handled
securely.
𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲
𝗦𝗲𝗿𝗶𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻?
1. When saving the state of
objects for future use.
IF YOU FIND
THIS HELPFUL,
LIKE AND
REPOST IT WITH
YOUR
CONNECTIONS
Nayankumar Dhome
[email protected] LIKE & REPOST