jackson-future-ideas icon indicating copy to clipboard operation
jackson-future-ideas copied to clipboard

Support records as map keys without a @JsonDeserialize

Open nlisker opened this issue 2 years ago • 5 comments

record Point(int x, int y) {}

Map<Point, Object> map = ...

Because the map key is a composite class, Jackson needs a deserializer like

@JsonDeserialize(keyUsing = Point.Deserializer.class)
record Point(int x, int y) {

    public static class Deserializer extends KeyDeserializer {

        @Override
        public Object deserializeKey(String key, DeserializationContext ctxt) {
            ...
        }
    }
}

However, records are special. Their default serialization and string representation are known. In this case it's "Point[x=2, y=2]". This means that a deserializer could be synthesized for them without the user needing to specify one. Basically, it's "look inside [ ] and read the name=value pairs". If the user decides to override the string representation of the record, then it's on them to provide a matching deserializer.

nlisker avatar Nov 25 '21 18:11 nlisker