Skip to content

Support for record types in JDK 14 #46

@ctash-dev

Description

@ctash-dev

Add support for record classes (data classes) from JDK 14 Feature Preview. https://cr.openjdk.java.net/~briangoetz/amber/datum.html

Right now the out-of-the-box experience while using records in jackson is the following:
Serializing a simple record with no extra methods:

public record TestRecord(int x, int y, int z) {
}

results in a "no properties" exception:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.company.TestRecord and no properties discovered to create BeanSerializer

A @JsonAutoDetect annotation can be use to solve the problem:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public record TestRecord(int x, int y, int z) {
}
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new TestRecord(1, 2, 3)));

results in a correct {"x":1,"y":2,"z":3} output

So right now it works with a little bit of work, but it would be great if there would be a correct out-of-the-box experience with record classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions