Java 类com.google.gson.typeadapters.RuntimeTypeAdapterFactory 实例源码

项目:mazes_and_minotaurs    文件:SaveAndLoadPerformer.java   
/**
 * 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;
}
项目:lib-poe    文件:StashLoader.java   
/**
 * 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);
}
项目:mazes_and_minotaurs    文件:CharacterSerializer.java   
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;
}
项目:mazes_and_minotaurs    文件:CharacterSerializer.java   
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;
}
项目:itstimetoact    文件:SampleGsonTimeToAct.java   
@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));
}
项目:itstimetoact    文件:GsonTest.java   
@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));
        }
    };
}
项目:zeppelin    文件:JupyterUtil.java   
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();
}
项目:android-urbanthings-sdk    文件:UrbanThingsGson.java   
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;
}
项目:android-urbanthings-sdk    文件:UrbanThingsGson.java   
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;
}
项目:myeslib    文件:SampleDomainGsonFactory.java   
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();

    }