本文整理汇总了Java中com.google.gson.typeadapters.RuntimeTypeAdapterFactory类的典型用法代码示例。如果您正苦于以下问题:Java RuntimeTypeAdapterFactory类的具体用法?Java RuntimeTypeAdapterFactory怎么用?Java RuntimeTypeAdapterFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RuntimeTypeAdapterFactory类属于com.google.gson.typeadapters包,在下文中一共展示了RuntimeTypeAdapterFactory类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getGson
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
/**
* Generates a gson that has all of the common features loaded in.
* @return a gson object
*/
private static Gson getGson() {
if (sGson == null) {
GsonBuilder builder = new GsonBuilder();
RuntimeTypeAdapterFactory<BaseClass> adapter = RuntimeTypeAdapterFactory
.of(BaseClass.class)
.registerSubtype(Amazon.class)
.registerSubtype(Barbarian.class)
.registerSubtype(Centaur.class)
.registerSubtype(Elementalist.class)
.registerSubtype(Hunter.class)
.registerSubtype(Lyrist.class)
.registerSubtype(Magician.class)
.registerSubtype(Noble.class)
.registerSubtype(Nymph.class)
.registerSubtype(Priest.class)
.registerSubtype(Sorcerer.class)
.registerSubtype(Spearman.class)
.registerSubtype(Thief.class);
builder.registerTypeAdapter(PlayerCharacter.class, new CharacterSerializer());
builder.registerTypeAdapterFactory(adapter);
builder.setPrettyPrinting();
sGson = builder.create();
}
return sGson;
}
示例2: fromFile
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
/**
* Reads a stash tab from file
*
* @param fileName The file containing stash info
* @return Requested stash tab
* @throws Exception
*/
public static StashTab fromFile(String fileName) throws Exception {
RuntimeTypeAdapterFactory<Item> itemAdapter = RuntimeTypeAdapterFactory.of(Item.class, new ItemTypePredicate())
.registerSubtype(Currency.class)
.registerSubtype(Equipment.class)
.registerSubtype(Gem.class)
.registerSubtype(Map.class);
Gson gson = new GsonBuilder()
.enableComplexMapKeySerialization()
.registerTypeAdapterFactory(itemAdapter)
.registerTypeAdapter(Property.class, new PropertyDeserializer())
.registerTypeAdapter(Property.class, new RequirementDeserializer())
.registerTypeAdapter(Sockets.class, new SocketDeserializer())
.registerTypeAdapter(ExplicitMod.class, new ExplicitModDeserializer())
.registerTypeAdapter(ImplicitMod.class, new ImplicitModDeserializer()).create();
return gson.fromJson(new JsonReader(new FileReader(fileName)), StashTab.class);
}
示例3: getGson
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
private static Gson getGson() {
if (sGson == null) {
GsonBuilder builder = new GsonBuilder();
RuntimeTypeAdapterFactory adapter = RuntimeTypeAdapterFactory
.of(Equipment.class)
.registerSubtype(Equipment.class)
.registerSubtype(Weapon.class)
.registerSubtype(Armor.class)
.registerSubtype(Mythics.class);
builder.registerTypeAdapterFactory(adapter);
builder.setPrettyPrinting();
sGson = builder.create();
}
return sGson;
}
示例4: createGsonBuilder
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
@Override
@NonNull
protected GsonBuilder createGsonBuilder(@NonNull GsonBuilder gsonBuilder) {
return super.createGsonBuilder(gsonBuilder)
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(ActEvent.class)
.registerSubtype(AfterUpdateEvent.class)
.registerSubtype(OneShotEvent.class)
.registerSubtype(StepEvent.class)
.registerSubtype(TimeEvent.class));
}
示例5: setup
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
@Before
public void setup() {
timeToAct = new GsonTimeToAct(RuntimeEnvironment.application) {
@Override
@NonNull
protected GsonBuilder createGsonBuilder(@NonNull GsonBuilder gsonBuilder) {
return super.createGsonBuilder(gsonBuilder)
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(ActEvent.class)
.registerSubtype(TestEvent.class)
.registerSubtype(SecondTestEvent.class));
}
};
}
示例6: JupyterUtil
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
public JupyterUtil() {
this.cellTypeFactory = RuntimeTypeAdapterFactory.of(Cell.class, "cell_type")
.registerSubtype(MarkdownCell.class, "markdown").registerSubtype(CodeCell.class, "code")
.registerSubtype(RawCell.class, "raw").registerSubtype(HeadingCell.class, "heading");
this.outputTypeFactory = RuntimeTypeAdapterFactory.of(Output.class, "output_type")
.registerSubtype(ExecuteResult.class, "execute_result")
.registerSubtype(DisplayData.class, "display_data").registerSubtype(Stream.class, "stream")
.registerSubtype(Error.class, "error");
this.markdownProcessor = new PegdownParser();
}
示例7: getPlacePointAdapterFactory
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
public static RuntimeTypeAdapterFactory<PlacePoint> getPlacePointAdapterFactory() {
// NOTES: TransitStop is derived from PlacePoint.
// API returns list of PlacePoint for GET static/stops where each can be PlacePoint or TransitStop
// For each item in the list, field "subClassType" is used to discriminate between these types.
// RuntimeTypeAdapterFactory<PlacePoint> provides mechanism for polymorphic deserialization.
RuntimeTypeAdapterFactory<PlacePoint> retVal = RuntimeTypeAdapterFactory.of(
PlacePoint.class,
"subClassType");
retVal.registerSubtype(TransitStop.class, "TransitStop");
return retVal;
}
示例8: getJourneyLegAdapterFactory
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
public static RuntimeTypeAdapterFactory<JourneyLeg> getJourneyLegAdapterFactory() {
// NOTES: TransitJourneyLeg is derived from JourneyLeg.
// Journey contains ArrayList<JourneyLeg> where each leg might be JourneyLeg or TransitJourneyLeg
// For each item in the list, (default) field "type" is used to discriminate between these types.
// RuntimeTypeAdapterFactory<JourneyLeg> provides mechanism for polymorphic deserialization.
RuntimeTypeAdapterFactory<JourneyLeg> retVal = RuntimeTypeAdapterFactory.of(
JourneyLeg.class);
retVal.registerSubtype(TransitJourneyLeg.class, "TransitJourneyLeg");
return retVal;
}
示例9: SampleDomainGsonFactory
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; //导入依赖的package包/类
public SampleDomainGsonFactory() {
final RuntimeTypeAdapterFactory<AggregateRoot> aggregateRootAdapter =
RuntimeTypeAdapterFactory.of(AggregateRoot.class)
.registerSubtype(InventoryItemAggregateRoot.class, InventoryItemAggregateRoot.class.getSimpleName());
final RuntimeTypeAdapterFactory<Command> commandAdapter =
RuntimeTypeAdapterFactory.of(Command.class)
.registerSubtype(CreateInventoryItem.class, CreateInventoryItem.class.getSimpleName())
.registerSubtype(IncreaseInventory.class, IncreaseInventory.class.getSimpleName())
.registerSubtype(DecreaseInventory.class, DecreaseInventory.class.getSimpleName());
final RuntimeTypeAdapterFactory<Event> eventAdapter =
RuntimeTypeAdapterFactory.of(Event.class)
.registerSubtype(InventoryItemCreated.class, InventoryItemCreated.class.getSimpleName())
.registerSubtype(InventoryIncreased.class, InventoryIncreased.class.getSimpleName())
.registerSubtype(InventoryDecreased.class, InventoryDecreased.class.getSimpleName());
this.gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT)
.registerTypeAdapterFactory(aggregateRootAdapter)
.registerTypeAdapterFactory(commandAdapter)
.registerTypeAdapterFactory(eventAdapter)
//.setPrettyPrinting()
.create();
}