How to use latest SequencedSet interface (java21) with jackson?

157 Views Asked by At

I have a class with a SequencedSet field, but I can't find a way to make it work with jackson deserialization.

class MyClass {
    private SequencedSet<UUID> ids;

    public SequencedSet<UUID> getIds() {
        return ids;
    }

    public void setIds(SequencedSet<UUID> ids) {
        this.ids = ids;
    }
}

I get this error

InvalidDefinition Cannot find a deserializer for non-concrete Collection type [collection type; class java.util.SequencedSet, contains [simple type, class java.util.UUID]]

Edit: I defaulted to using LinkedHashSet instead of SequencedSet

1

There are 1 best solutions below

1
On

As a workaround I made just the setter using LinkedHashSet. Then at least the field and the getter may use SequencedSet.

class MyClass {
    private SequencedSet<UUID> ids;

    public SequencedSet<UUID> getIds() {
        return ids;
    }

    public void setIds(LinkedHashSet<UUID> ids) {
        this.ids = ids;
    }
}

This requires (de)serialization via getters/setters (or constructors) instead of direct field access though.

To the question in the comments which implementation I expected: LinkedHashSet or similar because that preserves the properties of a SequencedSet: The order is preserved and no duplicates are allowed. For Set and List Jackson automatically selects concrete implementations too.