Hey @damienburke,
Thanks for making it a bit more clear.
So, I have assembled some code for the example given in docs.
You can use Person2
and People2
while serializing the POJO to json.
For deserialization purpose, you can choose a pair from one of the following as per your need:
-
People1
and Person1
OR
-
People
and Person
You can refer the tests in attached code for more clarity on how to use them.
import com.google.gson.*;
import com.google.gson.annotations.SerializedName;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class Playground {
public static final String json = "{\n" +
" \"persons\": [\n" +
" {\n" +
" \"name\": \"Alice\",\n" +
" \"friend\": [\n" +
" {\n" +
" \"name\": \"Bob\",\n" +
" \"car|since\": \"2006-02-02T13:01:09Z\",\n" +
" \"car\": \"MA0134\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Charlie\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Dave\"\n" +
" }\n" +
" ],\n" +
" \"friend|close\": {\n" +
" \"0\": true,\n" +
" \"1\": false,\n" +
" \"2\": true\n" +
" },\n" +
" \"friend|relative\": {\n" +
" \"0\": false,\n" +
" \"1\": true,\n" +
" \"2\": true\n" +
" }\n" +
" }\n" +
" ]\n" +
" }";
@Test
public void TestPersonDeSerialization() {
Gson gson = new Gson();
People people = gson.fromJson(json, People.class);
System.out.println(people);
// People{persons=[Person{name='Alice', car='null', since='null', friend=[Person{name='Bob', car='MA0134', since='2006-02-02T13:01:09Z', friend=null, close=null, relative=null}, Person{name='Charlie', car='null', since='null', friend=null, close=null, relative=null}, Person{name='Dave', car='null', since='null', friend=null, close=null, relative=null}], close={0=true, 1=false, 2=true}, relative={0=false, 1=true, 2=true}}]}
}
@Test
public void TestPerson1DeSerialization() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Person1.class, new Person1Deserializer());
Gson gson = gsonBuilder.create();
People1 people = gson.fromJson(json, People1.class);
System.out.println(people);
// People{persons=[Person{name='Alice', car='null', since='null', friend=[Person{name='Bob', car='MA0134', since='2006-02-02T13:01:09Z', friend=null, close=true, relative=false}, Person{name='Charlie', car='null', since='null', friend=null, close=false, relative=true}, Person{name='Dave', car='null', since='null', friend=null, close=true, relative=true}], close=null, relative=null}]}
}
@Test
public void TestPerson2Serialization() {
People2 people = new People2();
people.persons = Collections.singletonList(
new Person2("Alice", null, null, Arrays.asList(
new Person2("Bob", "MA0134", "2006-02-02T13:01:09Z", null, true, false),
new Person2("Charlie", false, true),
new Person2("Dave", true, true)
), null, null)
);
Gson gson = new Gson();
System.out.println(gson.toJson(people));
// {"persons":[{"name":"Alice","friend":[{"name":"Bob","car":"MA0134","car|since":"2006-02-02T13:01:09Z","friend|close":true,"friend|relative":false},{"name":"Charlie","friend|close":false,"friend|relative":true},{"name":"Dave","friend|close":true,"friend|relative":true}]}]}
}
}
class People {
List<Person> persons;
@Override
public String toString() {
return "People{" +
"persons=" + persons +
'}';
}
}
class Person {
String name;
String car;
@SerializedName("car|since")
String since;
List<Person> friend;
@SerializedName("friend|close")
Map<String, Boolean> close;
@SerializedName("friend|relative")
Map<String, Boolean> relative;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", car='" + car + '\'' +
", since='" + since + '\'' +
", friend=" + friend +
", close=" + close +
", relative=" + relative +
'}';
}
}
class People1 {
List<Person1> persons;
@Override
public String toString() {
return "People{" +
"persons=" + persons +
'}';
}
}
class Person1 {
String name;
String car;
@SerializedName("car|since")
String since;
List<Person1> friend;
Boolean close;
Boolean relative;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", car='" + car + '\'' +
", since='" + since + '\'' +
", friend=" + friend +
", close=" + close +
", relative=" + relative +
'}';
}
}
class Person1Deserializer implements JsonDeserializer<Person1> {
private static final Gson gson = new Gson();
@Override
public Person1 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
JsonObject closeFriends = jsonObject.getAsJsonObject("friend|close");
JsonObject relatives = jsonObject.getAsJsonObject("friend|relative");
Person1 person1 = gson.fromJson(json, Person1.class);
int i = 0;
for (Person1 friend : person1.friend) {
friend.close = closeFriends.get(String.valueOf(i)).getAsBoolean();
friend.relative = relatives.get(String.valueOf(i)).getAsBoolean();
i++;
}
return person1;
}
}
class People2 {
List<Person2> persons;
@Override
public String toString() {
return "People{" +
"persons=" + persons +
'}';
}
}
class Person2 {
String name;
String car;
@SerializedName("car|since")
String since;
List<Person2> friend;
@SerializedName("friend|close")
Boolean close;
@SerializedName("friend|relative")
Boolean relative;
public Person2(String name, Boolean close, Boolean relative) {
this.name = name;
this.close = close;
this.relative = relative;
}
public Person2(String name, String car, String since, List<Person2> friend, Boolean close, Boolean relative) {
this.name = name;
this.car = car;
this.since = since;
this.friend = friend;
this.close = close;
this.relative = relative;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", car='" + car + '\'' +
", since='" + since + '\'' +
", friend=" + friend +
", close=" + close +
", relative=" + relative +
'}';
}
}