I have a class which extends the TreeSet
class. Its constructor provides a custom comparator to the TreeSet
:
public class SortedPersonSet extends TreeSet<Person> {
public SortedPersonSet(Comparator<Person> comp) {
super(comp);
}
}
I want to serialize and deserialize that class using GSON
for example:
SortedPersonSet personSet =
new SortedPersonSet((p1, p2) -> Long.compare(p1.getAge(), p2.getAge()));
personSet.add(new Person("Peter", 21));
personSet.add(new Person("Klaus", 17));
personSet.add(new Person("Martin", 27));
personSet.add(new Person("John", 22));
// Serialize
Gson gson = new GsonBuilder().create();
String personSetAsString = gson.toJson(personSet);
System.out.println(personSetAsString);
// Deserialize
Gson gsonb = new GsonBuilder().create();
SortedPersonSet newPersons = gsonb.fromJson(personSetAsString, SortedPersonSet.class);
System.out.println(newPersons);
However, this version throws an exception because class Person
does not implement Comparable
.
So I have tried the approach which helped here by implementing a custom JsonDeserializer
which returns a SortedPersonSet
with a custom comparator:
public class SortedSetDeserializer implements JsonDeserializer<SortedPersonSet> {
@Override
public SortedPersonSet deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return new SortedPersonSet((p1, p2) -> Long.compare(p1.getAge(), p2.getAge()));
}
}
// Deserialization in main
Gson gsonb = new GsonBuilder()
.registerTypeAdapter(SortedPersonSet.class, new SortedSetDeserializer()).create();
SortedPersonSet newPersons = gsonb.fromJson(personSetAsString, SortedPersonSet.class);
System.out.println(newPersons);
Unfortunately, there is still a mistake in my code, because when I deserialize the JSON string via SortedPersonSet newPersons = gsonb.fromJson(personSetAsString, SortedPersonSet.class);
, the resulting SortedPersonSet
is empty. I hope you can point out where I made the mistake. Another (hopefully simpler) option would be appreciated as well.
Thanks in advance!