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!
Result set is empty because in custom deserialiser you do not parse array; only creates new set and returns it. Simple implementation with real deserialising class should look like this:
class SortedSetDeserializer implements JsonDeserializer<SortedPersonSet> {
@Override
public SortedPersonSet deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
SortedPersonSet people = new SortedPersonSet(Comparator.comparingLong(Person::getAge));
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
array.forEach(e -> {
people.add(context.deserialize(e, Person.class));
});
}
return people;
}
}
From other side, creating new subtype of TreeSet
does not sound good. If your Person
class will implement Comparable
:
class Person implements Comparable<Person> {
@Override
public int compareTo(Person o) {
return Long.compare(this.getAge(), o.getAge());
}
}
You can easily deserialise this array to TreeSet
:
Type type = new TypeToken<TreeSet<Person>>() {
}.getType();
TreeSet<Person> newPersons = gsonb.fromJson(personSetAsString, type);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.