I am running into an odd situation with GSON polymorphic objects in my current project. The situation is this: I have two different abstract base classes with two different use cases:
- the first class is contained within a List in and of itself, and
- the second class is also contained within a List, but the list is part of a larger parent object
Simplified versions of contrived classes (constructors and accessors left out for brevity; discriminator fields defined but commented out for illustration):
public abstract class ClassNumeric {
//private String numericType;
}
public class ClassOne extends ClassNumeric {
private String hex;
}
public class ClassTwo extends ClassNumeric {
private Integer whole;
private Integer fraction;
}
public abstract class ClassAlphabetic {
//private String alphabeticType;
}
public class ClassAlpha extends ClassAlphabetic {
private String name;
}
public class ClassBravo extends ClassAlphabetic {
private Integer age;
private Integer numberOfMarbles;
}
public class Container {
private String group;
private List<ClassAlphabetic> alphabetics;
}
Adapter factories and their registrations with GSON:
public RuntimeTypeAdapterFactory<ClassNumeric> numericTypeFactory = RuntimeTypeAdapterFactory
.of(ClassNumeric.class, "numericType")
.registerSubtype(ClassOne.class)
.registerSubtype(ClassTwo.class);
public RuntimeTypeAdapterFactory<ClassAlphabetic> alphabeticTypeFactory = RuntimeTypeAdapterFactory
.of(ClassAlphabetic.class, "alphabeticType")
.registerSubtype(ClassAlpha.class)
.registerSubtype(ClassBravo.class);
public final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.registerTypeAdapterFactory(numericTypeFactory)
.registerTypeAdapterFactory(alphabeticTypeFactory)
.create();
Based on what I've read so far, I don't have to (and actually aren't supposed to) declare the discriminator field in the base classes because GSON handles those internally as the JSON is serialized and deserialized.
Here is an example of how these can be used:
ClassOne c1 = ClassOne.builder().hex("EF8A").build();
ClassTwo c2 = ClassTwo.builder().whole(1).fraction(3).build();
List<ClassNumeric> numerics = Arrays.asList(c1, c2); // List of child objects
log.debug("Numerics: " + gson.toJson(numerics));
ClassAlpha ca = ClassAlpha.builder().name("Fred").build();
ClassBravo cb = ClassBravo.builder().age(5).numberOfMarbles(42).build();
List<ClassAlphabetic> alphas = Arrays.asList(ca, cb);
Container container = Container.builder().group("Test Group 1").alphabetics(alpha).build(); // List of objects field on larger object
log.debug("Alphas (container): " + gson.toJson(container));
The problem I'm having is that the ClassAlphabetic
objects work fine (the discriminator field is present in the JSON), while the ClassNumeric
objects do not (discriminator field missing). Sample output:
09:12:17.910 [main] DEBUG c.s.s.s.s.GSONPolymorphismTest - Numerics: [
{
"hex": "EF8A"
},
{
"whole": 1,
"fraction": 3
}
]
09:12:17.926 [main] DEBUG c.s.s.s.s.GSONPolymorphismTest - Alphas (container): {
"group": "Test Group 1",
"alphabetics": [
{
"alphabeticType": "ClassAlpha",
"name": "Fred"
},
{
"alphabeticType": "ClassBravo",
"age": 5,
"numberOfMarbles": 42
}
]
}
What am I missing here? These are essentially defined and set up with GSON the same way, but one use case works where the other doesn't.