-
Notifications
You must be signed in to change notification settings - Fork 6
Closed
Description
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.
cchacin, JelleBlaauw, thinkbigthings, detinho, aelgn and 39 morejiangyinzuo, rivancic, nanomader, danielwegener, sankarge and 10 more
Metadata
Metadata
Assignees
Labels
No labels