/** * 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; }
/** * 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); }
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; }
@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)); }
@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)); } }; }
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(); }
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; }
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; }
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(); }