GsonBuilder b = new GsonBuilder(); ... b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY); ... Gson gson = b.create();
public class GsonUtils { private static final GsonBuilder gsonBuilder = new GsonBuilder() .setPrettyPrinting(); public static void registerType( RuntimeTypeAdapterFactory<?> adapter) { gsonBuilder.registerTypeAdapterFactory(adapter); } public static Gson getGson() { return gsonBuilder.create(); }
private static Gson createGson() { GsonBuilder gsonBuilder = new GsonBuilder(); // there are no longer auto-registed from class-path, but from here or if added manually. gsonBuilder.registerTypeAdapterFactory(new TypeAdapters()); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gsonBuilder.registerTypeAdapterFactory(factory); } return gsonBuilder.create(); }
/** * the fully configured gson instance. * @return the gson instanse */ @Value.Default public Gson gson() { GsonBuilder gsonBuilder = new GsonBuilder(); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gsonBuilder.registerTypeAdapterFactory(factory); } return gsonBuilder.create(); }
private static Gson createGsonForModules() { return new GsonBuilder() .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()) .registerTypeAdapterFactory(new UriTypeAdapterFactory()) .setPrettyPrinting().create(); }
public TextureInfoFormat() { super("texinfo"); gson = new GsonBuilder().registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()).create(); }
public static <T> Gson getGson(Class<T> clazz) { Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonInterfaceAdapter(clazz)).create(); return gson; } }
private static Gson createGson() { return new GsonBuilder() .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()) .registerTypeAdapterFactory(new UriTypeAdapterFactory()) .registerTypeAdapter(Version.class, new VersionTypeAdapter()) .registerTypeAdapter(Name.class, new NameTypeAdapter()) .setPrettyPrinting() .create(); }
public BlockFamilyDefinitionFormat(AssetManager assetManager) { super("block"); this.assetManager = assetManager; gson = new GsonBuilder() .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()) .registerTypeAdapterFactory(new AssetTypeAdapterFactory(assetManager)) .registerTypeAdapter(BlockFamilyDefinitionData.class, new BlockFamilyDefinitionDataHandler()) .registerTypeAdapter(Vector3f.class, new Vector3fTypeAdapter()) .registerTypeAdapter(Vector4f.class, new Vector4fTypeAdapter()) .registerTypeAdapter(Class.class, new BlockFamilyHandler()) .create(); }
/** * Create a {@link GsonBuilder} which uses type handlers loaded from the given * {@link TypeSerializationLibrary} and complies with Terasology JSON serialization rules. * * @param typeSerializationLibrary The {@link TypeSerializationLibrary} to load type handler * definitions from */ public static GsonBuilder createGsonBuilderWithTypeSerializationLibrary(TypeSerializationLibrary typeSerializationLibrary) { TypeAdapterFactory typeAdapterFactory = new GsonTypeSerializationLibraryAdapterFactory(typeSerializationLibrary); return createDefaultGsonBuilder() .registerTypeAdapterFactory(typeAdapterFactory); }
private static Gson createGson() { GsonBuilder gsonBuilder = new GsonBuilder(); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gsonBuilder.registerTypeAdapterFactory(factory); } return gsonBuilder.create(); } }
public UISkinFormat() { super("skin"); gson = new GsonBuilder() .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()) .registerTypeAdapter(Font.class, new AssetTypeAdapter<>(Font.class)) .registerTypeAdapter(UISkinData.class, new UISkinTypeAdapter()) .registerTypeAdapter(TextureRegion.class, new TextureRegionTypeAdapter()) .registerTypeAdapterFactory(new GsonTypeHandlerAdapterFactory() { { addTypeHandler(Color.class, new ColorTypeHandler()); } }) .registerTypeAdapter(Optional.class, new OptionalTextureRegionTypeAdapter()) .create(); }
protected static Gson createGson() { return new GsonBuilder() .registerTypeAdapter(Name.class, new NameTypeAdapter()) .registerTypeAdapter(Version.class, new VersionTypeAdapter()) .registerTypeAdapter(BindsConfig.class, new BindsConfig.Handler()) .registerTypeAdapter(SetMultimap.class, new SetMultimapTypeAdapter<>(Input.class)) .registerTypeAdapter(SecurityConfig.class, new SecurityConfig.Handler()) .registerTypeAdapter(Input.class, new InputHandler()) .registerTypeAdapter(Resolution.class, new ResolutionHandler()) //.registerTypeAdapter(UniverseConfig.class, new UniverseConfig.Handler()) .registerTypeAdapter(PixelFormat.class, new PixelFormatHandler()) .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()) .registerTypeAdapterFactory(new UriTypeAdapterFactory()) .setPrettyPrinting().create(); }
public static Gson build() { RuntimeTypeAdapterFactory<WebsocketMessage> typeAdapter = RuntimeTypeAdapterFactory.of(WebsocketMessage.class) .registerSubtype(Handshake.class) .registerSubtype(LoginResponse.class) .registerSubtype(Ping.class); return new GsonBuilder() .registerTypeAdapterFactory(typeAdapter) .create(); } }
private static com.google.gson.Gson createGson() { GsonBuilder gson = new GsonBuilder(); // this one is no longer auto-registered gson.registerTypeAdapterFactory(new TypeAdapters()); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gson.registerTypeAdapterFactory(factory); } // register custom serializer for polymorphic Holder final HolderJsonSerializer custom = new HolderJsonSerializer(); gson.registerTypeAdapter(Holder.class, custom); gson.registerTypeAdapter(ImmutableHolder.class, custom); return gson.create(); }
@Inject public QueryParser(FeatureProcessor processingChain, QueryPluginFactory pluginFactory) { m_processingChain = processingChain; m_pluginFactory = pluginFactory; m_descriptorMap = new HashMap<>(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory()); gsonBuilder.registerTypeAdapter(TimeUnit.class, new TimeUnitDeserializer()); gsonBuilder.registerTypeAdapter(TrimAggregator.Trim.class, new TrimDeserializer()); gsonBuilder.registerTypeAdapter(FilterAggregator.FilterOperation.class, new FilterOperationDeserializer()); gsonBuilder.registerTypeAdapter(DateTimeZone.class, new DateTimeZoneDeserializer()); gsonBuilder.registerTypeAdapter(Metric.class, new MetricDeserializer()); gsonBuilder.registerTypeAdapter(SetMultimap.class, new SetMultimapDeserializer()); gsonBuilder.registerTypeAdapter(RelativeTime.class, new RelativeTimeSerializer()); gsonBuilder.registerTypeAdapter(SetMultimap.class, new SetMultimapSerializer()); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); m_gson = gsonBuilder.create(); }
/** * Create a {@link GsonBuilder} which uses the given type handlers and complies with Terasology * JSON serialization rules. * * @param typeHandlerEntries The type handlers to use during serialization. */ @SuppressWarnings("unchecked") public static GsonBuilder createGsonBuilderWithTypeHandlers(TypeHandlerEntry<?>... typeHandlerEntries) { GsonTypeHandlerAdapterFactory typeAdapterFactory = new GsonTypeHandlerAdapterFactory(); for (TypeHandlerEntry typeHandlerEntry : typeHandlerEntries) { typeAdapterFactory.addTypeHandler(typeHandlerEntry); } return createDefaultGsonBuilder() .registerTypeAdapterFactory(typeAdapterFactory); } }
public UIData load(JsonElement element, Locale otherLocale) throws IOException { NUIManager nuiManager = CoreRegistry.get(NUIManager.class); TranslationSystem translationSystem = CoreRegistry.get(TranslationSystem.class); TypeSerializationLibrary library = new TypeSerializationLibrary(CoreRegistry.get(TypeSerializationLibrary.class)); library.add(UISkin.class, new AssetTypeHandler<>(UISkin.class)); library.add(Border.class, new BorderTypeHandler()); GsonBuilder gsonBuilder = new GsonBuilder() .registerTypeAdapterFactory(new GsonTypeSerializationLibraryAdapterFactory(library)) .registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()) .registerTypeAdapter(UIData.class, new UIDataTypeAdapter()) .registerTypeHierarchyAdapter(UIWidget.class, new UIWidgetTypeAdapter(nuiManager)); // override the String TypeAdapter from the serialization library gsonBuilder.registerTypeAdapter(String.class, new I18nStringTypeAdapter(translationSystem, otherLocale)); Gson gson = gsonBuilder.create(); return gson.fromJson(element, UIData.class); }
@Before @SuppressWarnings("unchecked") public void setUp() throws Exception { MongoDatabase db = mock(MongoDatabase.class); MongoCollection<Entity> collection = mock(MongoCollection.class); when(db.getCollection(anyString(), any(Class.class))).thenReturn(collection); when(collection.withCodecRegistry(any(CodecRegistry.class))).thenReturn(collection); RepositorySetup setup = RepositorySetup.builder().database(db) .executor(MoreExecutors.newDirectExecutorService()) .gson(new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersEntity()).create()) .build(); this.repository = new EntityRepository(setup); FindIterable<Entity> iterable = mock(FindIterable.class); when(collection.find(any(Bson.class))).thenReturn(iterable); MongoCursor<Entity> cursor = mock(MongoCursor.class); when(iterable.iterator()).thenReturn(cursor); this.cursor = cursor; }
public static void main(String... args) { MongoClient client = new MongoClient("localhost"); RepositorySetup setup = RepositorySetup.builder() .database(client.getDatabase("test")) .executor(MoreExecutors.listeningDecorator(Executors.newCachedThreadPool())) .gson(new GsonBuilder() .registerTypeAdapterFactory(new GsonAdaptersEnt()) .create()) .build(); EntRepository repository = new EntRepository(setup); EntRepository.Criteria where = repository.criteria() .uuid("8b7a881c-6ccb-4ada-8f6a-60cc99e6aa20") .actionIn("BAN", "IPBAN"); Criteria or = where.expiresAbsent() .or() .with(where) .expiresGreaterThan(TimeInstant.of(1467364749679L)); System.out.println(or); repository.find(or).fetchAll().getUnchecked(); } }