Mastering Java Serialization: Native, Hessian, JSON, Protobuf & Kryo

This article explains five common serialization techniques—Java native serialization, Hessian, JSON, Google Protobuf, and Kryo—detailing their principles, performance characteristics, code examples, and when to choose each for Java backend systems.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mastering Java Serialization: Native, Hessian, JSON, Protobuf & Kryo

1. Java Native Serialization

Java classes implement the Serializable interface to enable object serialization. The interface has no methods and serves only as a marker. Java serialization preserves class metadata and object data, offering the best compatibility but limited to Java and moderate performance.

public class TestBean implements Serializable {
    private Integer id;
    private String name;
    private Date date;
    // getters, setters, toString omitted
}

It is recommended to declare a serialVersionUID field explicitly; otherwise the compiler generates one based on class details, which may change after source modifications, breaking compatibility.

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1234567890L;
    private int id;
    private String name;
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person: " + id + " " + name;
    }
}

2. Hessian Serialization

Hessian is a binary web service protocol that supports dynamic types, cross‑language communication, and object transmission. Its features include self‑describing types, language independence, a simple protocol, and, in version 2, compression that reduces payload size to about 50% of Java serialization and improves speed.

Self‑describing types without external schemas.

Language‑agnostic, works with scripting languages.

Simple protocol, more efficient than Java native serialization.

Hessian 2 adds compression: ~50% size, 30% serialization time, 20% deserialization time compared to Java.

To use Hessian, add the Maven dependency:

<dependency>
  <groupId>com.caucho</groupId>
  <artifactId>hessian</artifactId>
  <version>4.0.62</version>
</dependency>

Both Java native and Hessian serialization require the class to implement Serializable. Example:

public class Test {
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Hessian2Output output = new Hessian2Output(os);
        output.writeObject(Person.hehe(123L, "wangyong"));
        output.close();
        ByteArrayInputStream in = new ByteArrayInputStream(os.toByteArray());
        Hessian2Input input = new Hessian2Input(in);
        System.out.println(input.readObject());
    }
}

class Person implements Serializable {
    private Long id;
    private String name;
    private Person(Long id, String name) { this.id = id; this.name = name; }
    public static Person hehe(Long id, String name) { return new Person(id, name); }
    @Override
    public String toString() { return "id=" + id + ", name=" + name; }
}

3. JSON Serialization

JSON is a lightweight data‑exchange format widely used in both front‑end and back‑end development. JSON serialization converts an object to a JSON string, discarding type information but offering excellent readability and ease of debugging.

Basic JSON syntax rules:

Data is expressed as key‑value pairs.

Pairs are separated by commas.

Objects are enclosed in curly braces.

Arrays are enclosed in square brackets.

Example:

{
  "employees": [
    { "firstName": "Mike", "lastName": "Chen" },
    { "firstName": "Anna", "lastName": "Smith" },
    { "firstName": "Peter", "lastName": "Jones" }
  ]
}

4. Protobuf

Google Protocol Buffers (Protobuf) provide a language‑neutral, platform‑neutral, extensible way to serialize structured data. It is ideal for high‑performance communication and storage when payload size and speed are critical, and when the data schema changes infrequently.

Typical usage involves defining a .proto file:

syntax = "proto3";

message SendRequest {
  string query = 1;
  int32 page_number = 2;
  repeated int32 result_per_page = 3;
}

The first line selects the proto3 syntax. message defines a data structure; multiple messages can be declared.

Each field has a name, type, and numeric tag. repeated denotes a list.

5. Kryo

Kryo is a fast Java serialization framework that leverages bytecode generation for high performance and compact output. It offers a simple API but works only within the JVM, so it does not support cross‑language scenarios.

Very high serialization speed.

Resulting payload is small.

Easy‑to‑use API.

Example usage:

static void quickStart() throws FileNotFoundException {
    Kryo kryo = new Kryo();
    Output output = new Output(new FileOutputStream("file.bin"));
    SomeClass someObject = new SomeClass();
    someObject.setValue("this is someObject.");
    kryo.writeObject(output, someObject);
    output.close();

    Input input = new Input(new FileInputStream("file.bin"));
    SomeClass deSomeObject = kryo.readObject(input, SomeClass.class);
    input.close();
    Assert.assertEquals(someObject.getValue(), deSomeObject.getValue());
}

Summary

The article reviews five common serialization mechanisms—Java native serialization, Hessian, JSON, Protobuf, and Kryo—highlighting their characteristics, performance trade‑offs, and providing concrete code snippets to help developers choose the most suitable approach for their backend systems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaserializationProtobufJSONHessianKryo
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.