Implementing Serialization/Deserialization
1. Implementing JSON Serialization
Example: Jackson ObjectMapper
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.build();
String json = mapper.writeValueAsString(order);
Order o = mapper.readValue(json, Order.class);
2. Implementing Custom Serializers
Example: Money serializer
public class MoneySerializer extends JsonSerializer<Money> {
public void serialize(Money v, JsonGenerator g, SerializerProvider sp) throws IOException {
g.writeStartObject();
g.writeStringField("currency", v.currency().getCurrencyCode());
g.writeNumberField("amount", v.amount());
g.writeEndObject();
}
}
3. Handling Date/Time Serialization
| Practice | Detail |
| UTC | Always serialize in UTC |
| ISO-8601 | 2024-05-30T14:00:00Z |
| java.time | Use Instant, OffsetDateTime, LocalDate |
| Avoid | java.util.Date, timestamps in body |
4. Handling Polymorphic Types
Example: Sealed + JsonTypeInfo
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Card.class, name = "card"),
@JsonSubTypes.Type(value = BankXfer.class, name = "bank")
})
public sealed interface PaymentMethod permits Card, BankXfer {}
5. Implementing Deep Copy Logic
| Approach | Detail |
| Records + immutables | No copy needed |
| Copy constructor | Explicit |
| Serialize/deserialize | Quick deep clone via Jackson |
| Library | MapStruct generates copy mappers |
6. Implementing Field Filtering
| Mechanism | Detail |
| @JsonIgnore | Per-field exclusion |
| @JsonView | Multiple projections of same class |
| Filters | @JsonFilter dynamic include/exclude |
| DTOs | Cleanest; explicit shape per use |
7. Handling Null Values
| Setting | Behavior |
| ALWAYS | Include nulls |
| NON_NULL | Omit nulls |
| NON_EMPTY | Omit nulls + empty collections |
| NON_DEFAULT | Omit defaults (0, false) |
8. Implementing Versioned DTOs
| Strategy | Detail |
| Separate classes | OrderV1Dto, OrderV2Dto |
| @JsonView per version | Single class, multiple views |
| Mappers | v1 ↔ v2 bidirectional |
9. Handling Circular References
| Approach | Detail |
| @JsonManagedReference / @JsonBackReference | Parent serialized, child back-ref omitted |
| @JsonIdentityInfo | Object identity by ID |
| DTOs | Break cycles structurally |
Warning: JPA entities frequently have lazy bidirectional relations — serialize DTOs, not entities.
10. Implementing Schema Validation
| Spec | Use |
| JSON Schema | Validate JSON shape |
| OpenAPI | Per-endpoint schemas |
| Bean Validation | JVM-side post-bind |
| Avro/Protobuf | Compile-time + runtime |
11. Handling Binary Data
| Approach | Detail |
| Base64 in JSON | ~33% overhead |
| Multipart upload | Separate binary part |
| Pre-signed URL | Upload directly to object store |
| Protobuf | Native binary fields |
12. Implementing Compression
| Codec | Trade-off |
| gzip | Universal, moderate |
| deflate | Similar to gzip |
| br (Brotli) | Best ratio for text |
| zstd | Fast + great ratio |
| snappy/lz4 | Speed-focused (Kafka) |