- Type Parameters:
T - provides the marker interface that identifies the union. All union members wrappers
must implement this interface.
- All Implemented Interfaces:
- com.google.gson.TypeAdapterFactory
public class TypedDefinitionAdapterFactory<T>
extends java.lang.Object
implements com.google.gson.TypeAdapterFactory
GSON TypeAdapterFactory for "typedDefinition" and
"flatTypedDefinition".
Both are tagged union formats.
For example, consider a union "AnswerFormat" with the union member options of "TextEntry" and
"MultipleChoice".
"typedDefinitions"
Example JSON:
{
"typeName": "textEntry",
"definition": { "textEntryField1": ... }
}
Example Usage with a GSON Java class:
JsonAdapter(AnswerFormat.Adapter.class)
interface AnswerFormat {
public static final class TextEntryMember implements AnswerFormat {
private static final String TYPE_NAME = "textEntry";
public final String typeName = TYPE_NAME;
public TextEntry definition;
}
public final class MultipleChoiceMember implements AnswerFormat {
// ...
}
final class Adapter extends TypedDefinitionAdapterFactory<AnswerFormat> {
Adapter() {
super(AnswerFormat.class, new Resolver<AnswerFormat>() {
public Class<? extends AnswerFormat> resolve(String typeName) {
switch(typeName) {
case AnswerFormat.TYPE_NAME: return TextEntryMember.class;
case MultipleChoice.TYPE_NAME: return MultipleChoice.class;
// ...
}
}
});
}
}
}
"flatTypedDefinition"
Example JSON:
{
"typeName": "textEntry",
"textEntryField1": ...
}
Example Usage with a GSON Java class:
JsonAdapter(AnswerFormat.Adapter.class)
interface AnswerFormat {
public static final class TextEntryMember extends TextEntry implements AnswerFormat {
private static final String TYPE_NAME = "textEntry";
public final String typeName = TYPE_NAME;
}
public final class MultipleChoiceMember implements AnswerFormat {
// ...
}
final class Adapter extends TypedDefinitionAdapterFactory<AnswerFormat> {
Adapter() {
super(AnswerFormat.class, new Resolver<AnswerFormat>() {
public Class<? extends AnswerFormat> resolve(String typeName) {
switch(typeName) {
case AnswerFormat.TYPE_NAME: return TextEntryMember.class;
case MultipleChoice.TYPE_NAME: return MultipleChoice.class;
// ...
}
}
});
}
}
}