- 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 UnionAdapterFactory<T>
extends java.lang.Object
implements com.google.gson.TypeAdapterFactory
GSON TypeAdapterFactory for Pegasus style unions.
For example, consider a union "AnswerFormat" with the union member options of "TextEntry" and
"MultipleChoice".
Example JSON:
{
"org.example.TextEntry": { "textEntryField1": ... }
}
Example Usage with a GSON Java class:
JsonAdapter(AnswerFormat.Adapter.class)
interface AnswerFormat {
public final class TextEntryMember implements AnswerFormat {
private static final String MEMBER_KEY = "org.example.TextEntry";
SerializedName(MEMBER_KEY)
public TextEntry member;
}
public final class MultipleChoiceMember implements AnswerFormat {
// ...
}
final class Adapter extends UnionAdapterFactory<AnswerFormat> {
Adapter() {
super(AnswerFormat.class, new Resolver<AnswerFormat>() {
public Class<? extends AnswerFormat> resolve(String memberKey) {
switch(memberKey) {
case AnswerFormat.MEMBER_KEY: return TextEntryMember.class;
case MultipleChoice.MEMBER_KEY: return MultipleChoice.class;
// ...
}
}
});
}
}
}