Skip to content

Chapter 35 of 37

Serialization

Design versioned data formats and understand native serialization security risks.

38 minutes 10 quick checksBy Subha Prasad
Lesson 35 of 37Course navigation

Lesson content

Read, practise, then check your understanding

Serialization converts state to bytes or text. A durable format defines schema, field meanings, versions, encoding, limits, and compatibility independent of an in-memory class layout.

Explicit data transfer

record UserDto(long id, String name) {}

String encode(UserDto user) {
    return "{\"id\":" + user.id() + ",\"name\":\"" +
        escapeJson(user.name()) + "\"}";
}

Use a maintained JSON/CBOR/Protocol Buffers library rather than hand-building real JSON. Java native Serializable uses object graphs, serialVersionUID, and transient, but deserializing untrusted bytes can trigger dangerous behavior. Prefer allow-listed explicit schemas, validate before constructing domain objects, cap depth/size, and never serialize credentials or live resource handles.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes serialization?
Which Java term matches this description: Encoding object or domain state for storage or transfer.
Which statement best describes Serializable?
Which Java term matches this description: A marker enabling Java's native object serialization mechanism.
Which statement best describes serialVersionUID?
Which Java term matches this description: A version identifier used during native deserialization compatibility checks.
Which statement best describes transient?
Which Java term matches this description: A field modifier excluding a field from default native serialization.
Which statement best describes deserialization risk?
Which Java term matches this description: The security danger of constructing attacker-controlled object graphs.

0 of 10 checks passed

Your progress is saved on this device.