Data Serialization: Object Streams, JSON, and Protocol Buffers over TCP
Data Serialization: Object Streams, JSON, and Protocol Buffers over TCP
आज हम एक बहुत ही important और frequently asked topic समझने वाले हैं — Data Serialization: Object Streams, JSON, and Protocol Buffers over TCP। ये topic Java networking और distributed systems दोनों के लिए काफी useful है, और college exams में अक्सर short notes या program based questions में पूछा जाता है। चलिए इसे step-by-step आसान भाषा में समझते हैं।
What is Data Serialization?
Serialization एक process है जिसमें हम object को byte stream में convert करते हैं ताकि उसे network पर भेजा जा सके या file में store किया जा सके। Deserialization इसका उल्टा process होता है — यानी byte stream से वापस object बनाना।
Simple words में कहें तो Serialization का मतलब है — data को ऐसे format में बदलना जिससे वो transmit या save किया जा सके और बाद में original form में वापस लाया जा सके।
Why Serialization is Needed?
- Network पर complex objects को भेजने के लिए।
- Data को persistent form (जैसे file या database) में store करने के लिए।
- Distributed systems में client-server के बीच communication के लिए।
- Remote Method Invocation (RMI) या TCP socket communication में data exchange के लिए।
जब हम Java या किसी भी OOP language में objects को use करते हैं, तो वो memory में रहते हैं। लेकिन network या disk पर भेजने के लिए उन्हें convert करना पड़ता है। यही काम Serialization करता है।
Object Streams in Java
Java में Object Streams का use serialized objects को पढ़ने या लिखने के लिए किया जाता है। इसके लिए दो main classes होती हैं —
- ObjectOutputStream — object को serialize करके output stream में लिखता है।
- ObjectInputStream — byte stream से object को deserialize करके पढ़ता है।
Example: Sending Serialized Object over TCP
चलो एक simple client-server example देखते हैं जहाँ एक object TCP socket के जरिए भेजा जाता है।
// Serializable class
import java.io.Serializable;
public class Student implements Serializable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
// Server side
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Student st = (Student) ois.readObject();
System.out.println("Received: " + st.name);
ois.close(); s.close(); ss.close();
}
}
// Client side
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 5000);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
Student st = new Student(101, "Rahul");
oos.writeObject(st);
oos.close(); s.close();
}
}
ऊपर दिए गए example में ObjectOutputStream object को byte stream में बदलता है और TCP socket के जरिए server तक भेज देता है। Server उसे ObjectInputStream के जरिए वापस object में बदल लेता है।
Advantages of Object Streams
- Java object को directly network पर भेज सकते हैं।
- Code implementation आसान होता है।
- Automatic type safety और class information maintain रहती है।
Limitations of Object Streams
- Platform dependent — केवल Java systems में काम करता है।
- Serialization slow होती है और file size बड़ा होता है।
- Cross-language communication possible नहीं है।
JSON Serialization
अब बात करते हैं एक widely used serialization format की — JSON (JavaScript Object Notation)। JSON एक lightweight text-based data format है, जिसे almost हर language support करती है।
What is JSON?
JSON basically key-value pairs में data represent करता है। इसकी syntax JavaScript जैसी होती है, लेकिन readable और compact होती है।
{
"id": 101,
"name": "Rahul",
"course": "B.Tech"
}
JSON Serialization in Java
Java में JSON serialization के लिए libraries जैसे Gson या Jackson का use किया जाता है। चलो एक छोटा example देखें।
// Using Gson for JSON serialization
import com.google.gson.Gson;
public class JsonExample {
public static void main(String[] args) {
Student st = new Student(101, "Rahul");
Gson gson = new Gson();
String json = gson.toJson(st);
System.out.println(json);
}
}
Advantages of JSON
- Cross-language compatible है — Java, Python, C#, आदि में use हो सकता है।
- Lightweight और human-readable format है।
- REST APIs और web applications में standard format है।
Disadvantages of JSON
- Binary data को efficiently represent नहीं कर पाता।
- Parsing text-based होने की वजह से थोड़ा slow होता है।
- Type information automatically maintain नहीं होती।
Protocol Buffers (Protobuf)
अब हम बात करेंगे Protocol Buffers की, जिसे short में Protobuf कहते हैं। इसे Google ने develop किया था ताकि JSON और XML से तेज़ और compact data exchange हो सके।
What are Protocol Buffers?
Protocol Buffers एक binary serialization format है जो structured data को efficient तरीके से serialize करता है। इसे cross-platform और language-neutral दोनों के लिए design किया गया है।
How it Works?
- पहले एक .proto file बनाई जाती है जिसमें data structure define होता है।
- फिर इस file से Java, Python, C++ आदि languages के लिए code auto-generate होता है।
- Generated code का use करके data serialize और deserialize किया जा सकता है।
Example: Student.proto File
syntax = "proto3";
message Student {
int32 id = 1;
string name = 2;
string course = 3;
}
Advantages of Protocol Buffers
- Compact binary format — data size बहुत कम होता है।
- Serialization और deserialization बहुत fast होती है।
- Cross-language और backward compatible है।
- Data contracts strongly defined रहते हैं।
Disadvantages of Protocol Buffers
- Human-readable नहीं होता (binary format)।
- Learning curve थोड़ा ज्यादा होता है।
- Simple use-cases के लिए JSON ज्यादा convenient होता है।
Comparison: Object Streams vs JSON vs Protocol Buffers
| Feature | Object Streams | JSON | Protocol Buffers |
|---|---|---|---|
| Format Type | Binary | Text-based | Binary |
| Speed | Moderate | Slow | Very Fast |
| Cross-Platform | No | Yes | Yes |
| Readability | No | Yes | No |
| Data Size | Large | Medium | Small |
| Use Case | Java specific apps | Web APIs | High performance systems |
Serialization over TCP
TCP एक reliable connection-oriented protocol है जो data packets को सही क्रम में पहुंचाता है। जब हम Serialization को TCP के साथ use करते हैं, तो data objects या JSON messages को direct socket streams के जरिए भेजा जाता है।
Example: Sending JSON over TCP
// Server
import java.io.*;
import java.net.*;
public class JsonServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(6000);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String json = br.readLine();
System.out.println("Received JSON: " + json);
ss.close();
}
}
// Client
import java.io.*;
import java.net.*;
public class JsonClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 6000);
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
String json = "{\"id\":101,\"name\":\"Rahul\",\"course\":\"B.Tech\"}";
pw.println(json);
s.close();
}
}
इस example में JSON data TCP socket के माध्यम से भेजा जा रहा है — यानी real-time client-server communication possible है।
Key Points to Remember
- Serialization object को byte stream में बदलने की process है।
- Object Streams Java specific हैं और cross-language compatible नहीं।
- JSON readable और widely used format है।
- Protocol Buffers fast और compact binary format प्रदान करते हैं।
- TCP communication में इन तीनों formats को use किया जा सकता है — use-case के अनुसार selection किया जाता है।
Exam Notes (Quick Revision)
- Definition: Serialization = Object → Byte Stream
- Deserialization: Byte Stream → Object
- Java Classes: ObjectOutputStream, ObjectInputStream
- Formats: Object Stream, JSON, Protocol Buffers
- Best for Java Apps: Object Streams
- Best for Web APIs: JSON
- Best for High-Performance Apps: Protocol Buffers
- Protocol Buffers file: .proto
- TCP Role: Reliable data transfer channel
- Question Type: Short Notes / Program-based / Comparison Table