Java की Advanced Serialization
In this page:
Advanced Serialization
Serialization किसी object की in-memory state को एक byte stream में बदल देता है ताकि इसे किसी file में save किया जा सके या network पर भेजा जा सके और बाद में फिर से बनाया जा सके। किसी class को यह support करने के लिए, इसे marker interface Serializable implement करना चाहिए, JVM को बताते हुए कि इसे serialize करना सुरक्षित है।
उदाहरण: Advanced Serialization
import java.io.*;
public class Main {
static class User implements Serializable { // marker interface -- tells the JVM it's safe to serialize
String name = "Riya";
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new ObjectOutputStream(bos).writeObject(new User());
System.out.println("Serialized to " + bos.size() + " bytes");
}
}
Login to try C/C++/Java/PHP code in the editor
The transient Keyword
किसी field पर transient keyword लगाना serialization process को इसे पूरी तरह skip करने के लिए कहता है, जो sensitive data (जैसे password field) या derived values के लिए बिल्कुल वही है जो serialized form में persist या तार पर नहीं भेजी जानी चाहिए।
उदाहरण: The transient Keyword
import java.io.*;
public class Main {
static class User implements Serializable {
String name = "Riya";
transient String password = "secret"; // skipped entirely during serialization
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new ObjectOutputStream(bos).writeObject(new User());
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
User restored = (User) in.readObject();
System.out.println(restored.name + " " + restored.password); // password is null after restore
}
}
Login to try C/C++/Java/PHP code in the editor
serialVersionUID
serialVersionUID एक static final long constant है जो deserialization time पर यह verify करने के लिए उपयोग होता है कि incoming byte stream असल में अभी loaded class के version से मेल खाती है। एक explicit के बिना, किसी class में बदलाव चुपचाप पहले से save किए data की deserialization तोड़ सकता है।
उदाहरण: serialVersionUID
import java.io.*;
public class Main {
static class User implements Serializable {
static final long serialVersionUID = 1L; // verified against the byte stream at deserialization
String name = "Riya";
}
public static void main(String[] args) {
System.out.println("serialVersionUID = " + User.serialVersionUID);
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Serialization
आप अपनी Serializable class के अंदर private writeObject() और readObject() methods define करके default serialization behavior को override कर सकते हैं, आपको उन fields को handle करने के लिए manual control देते हुए जिन्हें default field-by-field approach के बजाय custom encoding logic चाहिए।
उदाहरण: Custom Serialization
import java.io.*;
public class Main {
static class User implements Serializable {
String name = "Riya";
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(name.toUpperCase()); // custom encoding logic
}
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new ObjectOutputStream(bos).writeObject(new User());
System.out.println("Custom writeObject() ran, " + bos.size() + " bytes written");
}
}
Login to try C/C++/Java/PHP code in the editor
Externalizable Interface
Externalizable interface Serializable को extend करता है लेकिन आपको writeExternal() और readExternal() के ज़रिए पूरे serialization process पर पूरा, manual control देता है, बजाय Java के default reflective serialization mechanism पर बिल्कुल निर्भर रहने के।
उदाहरण: Externalizable Interface
import java.io.*;
public class Main {
static class User implements Externalizable {
String name = "Riya";
public User() {}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name); // fully manual control, no default reflective serialization
}
public void readExternal(ObjectInput in) throws IOException {
name = in.readUTF();
}
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new ObjectOutputStream(bos).writeObject(new User());
System.out.println("Externalizable wrote " + bos.size() + " bytes manually");
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी ऐसी class को serialize करना जो
Serializableimplement नहीं करती, जोNotSerializableExceptionफेंकता है, ठीक वैसे ही जैसे कोई field जिसका type serializable नहीं है। - यह उम्मीद करना कि एक
transientfield अपनी value बनाए रखेगा, जबकि deserialization के बाद यहnull,0याfalseके रूप में वापस आता है। serialVersionUIDdeclare न करना, जिससे बाद में class बदलने से पुराना data पढ़ते समयInvalidClassExceptionआ सकता है।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing