Is it possible to deserialise a SortedSet or TreeSet (or SortedMap or TreeMap as a matter of fact) with a provided Comparator?
Asked
Active
Viewed 432 times
2 Answers
1
There is an open Jackson issue (jackson-databind#2162) to allow specifying a Comparator
when deserializing a SortedMap
. It has been open since October 2018, but it doesn't look like any work has been done on it.

M. Justin
- 14,487
- 7
- 91
- 130
0
One solution is to create a custom Converter
which converts a SortedSet
to one which is sorted using the desired Comparator
:
public static class MyStringSetConverter
extends StdConverter<SortedSet<String>, SortedSet<String>> {
private static final Comparator<String> COMPARATOR =
String.CASE_INSENSITIVE_ORDER
.thenComparing(Comparator.naturalOrder());
@Override
public SortedSet<String> convert(SortedSet<String> value) {
SortedSet<String> sortedSet = new TreeSet<>(COMPARATOR);
sortedSet.addAll(value);
return sortedSet;
}
}
public static class MyType {
@JsonDeserialize(converter = MyStringSetConverter.class)
private SortedSet<String> value;
public SortedSet<String> getValue() { return value; }
public void setValue(SortedSet<String> value) { this.value = value; }
public String toString() { return Objects.toString(value); }
}

M. Justin
- 14,487
- 7
- 91
- 130