By default, Java’s built-in serialization (Serializable
) works with binary streams — meaning the object is serialized into a byte stream (essentially a compact, unreadable format for humans). However, you can absolutely serialize objects into other formats like text (JSON, XML, YAML, etc.) — but not with Serializable
directly.
🔧 Options for Serialization to Text Formats
1️⃣ JSON Serialization
- Popular in modern apps.
- Human-readable, widely supported.
- Common libraries:
- Jackson (
com.fasterxml.jackson
) - Gson (
com.google.gson
)
- Jackson (
Example using Jackson
import com.fasterxml.jackson.databind.ObjectMapper;
public class Person {
private String name;
private int age;
// constructor, getters, setters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class JsonExample {
public static void main(String[] args) throws Exception {
Person person = new Person("John", 30);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(person);
System.out.println(json); // {"name":"John","age":30}
// Deserialize back
Person deserialized = mapper.readValue(json, Person.class);
System.out.println(deserialized.getName()); // John
}
}
2️⃣ XML Serialization
- Used in legacy systems, or systems that need strong typing/schema (like SOAP).
- Libraries:
- JAXB (Java’s built-in XML binder).
Example using JAXB
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person {
private String name;
private int age;
// Required no-arg constructor for JAXB
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@XmlElement
public String getName() { return name; }
@XmlElement
public int getAge() { return age; }
}
Serialization:
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Person person = new Person("John", 30);
marshaller.marshal(person, System.out);
3️⃣ YAML Serialization
- Useful for config files.
- Libraries:
- SnakeYAML
import org.yaml.snakeyaml.Yaml;
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Serialization:
Yaml yaml = new Yaml();
String output = yaml.dump(new Person("John", 30));
System.out.println(output);
💡 Key point
Serializable
is tightly coupled to Java’s own binary serialization format, but you are not limited to that mechanism. In modern systems, especially in microservices, JSON or Protocol Buffers are much more common.
🔥 Should I avoid Serializable
?
In modern best practices, many teams avoid Serializable
for a few reasons:
- Versioning is fragile (small changes break compatibility).
- Not human-readable.
- Can have security issues if deserializing untrusted data (deserialization attacks).
- Only works in Java (not language-agnostic).
✅ Summary
Format | Human-readable | Language-neutral | Popular Libraries |
---|---|---|---|
Java Serialization | ❌ | ❌ | Built-in |
JSON | ✅ | ✅ | Jackson, Gson |
XML | ✅ | ✅ | JAXB |
YAML | ✅ | ✅ | SnakeYAML |
Protobuf | ❌ | ✅ | Google Protobuf |
🚀 Quick Tip
If you’re building modern systems (microservices, APIs), go for JSON (or Protobuf if you need compact binary across languages).
For configuration files, YAML is often preferred.