List of usage examples for com.google.gson GsonBuilder registerTypeAdapterFactory
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory)
From source file:com.guusto.GraphAdapterBuilder.java
License:Apache License
public void registerOn(final GsonBuilder gsonBuilder) { final Factory factory = new Factory(instanceCreators); gsonBuilder.registerTypeAdapterFactory(factory); for (final Map.Entry<Type, InstanceCreator<?>> entry : instanceCreators.entrySet()) { gsonBuilder.registerTypeAdapter(entry.getKey(), factory); }//from w w w . j av a 2 s . com }
From source file:com.hawk.lib.base.model.provider.ProviderModule.java
License:Open Source License
@Singleton @Provides/*w ww . j ava2 s .c o m*/ Gson provideGson(final GsonConfig config) { final GsonBuilder builder = new GsonBuilder(); if (config.autoGsonTypeAdapterFactory() != null) { builder.registerTypeAdapterFactory(config.autoGsonTypeAdapterFactory()); } return builder .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeJsonConverter(config.dateTimeFormatter())) .setDateFormat(config.dateFormatString()).setPrettyPrinting().create(); }
From source file:com.ibm.g11n.pipeline.client.impl.ServiceClientImpl.java
License:Open Source License
/** * Creates a new Gson object//from ww w . ja va 2 s. c o m * * @param className A class name used for serialization/deserialization. * <p>Note: This implementation does not use this argument * for now. If we need different kinds of type adapters * depending on class, the implementation might be updated * to set up appropriate set of type adapters. * @return A Gson object */ private static Gson createGson(String className) { GsonBuilder builder = new GsonBuilder(); // ISO8601 date format support builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); builder.registerTypeAdapter(TranslationStatus.class, new TranslationStatusAdapter()); builder.registerTypeAdapter(new TypeToken<EnumMap<TranslationStatus, Integer>>() { }.getType(), new EnumMapInstanceCreator<TranslationStatus, Integer>(TranslationStatus.class)); builder.registerTypeAdapterFactory(new NullMapValueTypeAdapterFactory()); return builder.create(); }
From source file:com.javacreed.examples.gson.part2.Main.java
License:Apache License
public static void main(final String[] args) throws IOException { // Configure GSON final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder(); dtafBuilder.add(Book.class, new BookTypeAdapter()); final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build()); gsonBuilder.setPrettyPrinting();//from w w w. j a v a 2 s . com final Gson gson = gsonBuilder.create(); final Book book = new Book(); book.setAuthors(new Author[] { new Author(1, "Joshua Bloch") }); book.setTitle("Effective Java"); book.setIsbn("978-0321356680"); final String json = gson.toJson(book); System.out.println(json); }
From source file:com.javacreed.examples.gson.part3.Main.java
License:Apache License
public static void main(final String[] args) throws IOException { // Configure GSON final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder(); dtafBuilder.add(A.class, new ATypeAdapter()); final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build()); gsonBuilder.setPrettyPrinting();//from w w w .j ava 2 s .co m final Gson gson = gsonBuilder.create(); final B b = new B(); final String json = gson.toJson(b); System.out.println(json); }
From source file:com.javacreed.examples.gson.part4.Main.java
License:Apache License
public static void main(final String[] args) throws IOException { final Class<?> type = new TypeToken<List<A>>() { }.getRawType();//from w w w . java 2s . com // Configure GSON final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder(); dtafBuilder.add(A.class, new ATypeAdapter()); dtafBuilder.add(type, new ListATypeAdapter()); final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build()); gsonBuilder.setPrettyPrinting(); final Gson gson = gsonBuilder.create(); final List<B> list = Arrays.asList(new B()); final String json = gson.toJson(list, type); System.out.println(json); }
From source file:com.julauncher.workers.InstanceInstaller.java
License:Open Source License
public InstanceInstaller(String instanceName, Pack pack, PackVersion version, boolean isReinstall, boolean isServer, String shareCode, boolean showModsChooser) { this.instanceName = instanceName; this.pack = pack; this.version = version; this.isReinstall = isReinstall; this.isServer = isServer; this.shareCode = shareCode; this.showModsChooser = showModsChooser; if (isServer) { serverLibraries = new ArrayList<File>(); }//from w w w .j a v a 2s. com GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory()); builder.registerTypeAdapter(Date.class, new DateTypeAdapter()); builder.registerTypeAdapter(File.class, new FileTypeAdapter()); builder.setPrettyPrinting(); this.gson = builder.create(); }
From source file:com.pkuhelper.model.base.ModelBaseProviderModule.java
License:Open Source License
@Singleton @Provides/* w w w . j a v a 2 s . c o m*/ Gson provideGson(final GsonConfig config) { final GsonBuilder builder = new GsonBuilder(); if (config.autoGsonTypeAdapterFactory() != null) { builder.registerTypeAdapterFactory(config.autoGsonTypeAdapterFactory()); } if (config.fieldNamingPolicy() != null) { builder.setFieldNamingPolicy(config.fieldNamingPolicy()); } return builder .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeJsonConverter(config.dateTimeFormatter())) .setDateFormat(config.dateFormatString()).setPrettyPrinting().create(); }
From source file:com.scvngr.levelup.core.model.factory.json.GsonModelFactory.java
License:Apache License
/** * Constructs a new factory.//from w w w .ja v a 2s . c om * * @param typeKey the key which the object to parse can be nested under. It will usually be the * name of the object's type: * * <pre> * { "typeKey": { "field1": "test" } } * </pre> * * When requesting a single object or a list of objects, the object will be nested under * this type key. * @param type the type of object to load * @param wrapped if true, the input JSON must be wrapped with a JSON object that has a typeKey, * as mentioned above. */ public GsonModelFactory(@NonNull final String typeKey, @NonNull final Class<T> type, final boolean wrapped) { mType = type; mTypeKey = typeKey; final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); gsonBuilder.registerTypeAdapter(MonetaryValue.class, new MonetaryValueTypeAdapter()); gsonBuilder.registerTypeAdapter(Uri.class, new UriTypeAdapter()); gsonBuilder.registerTypeAdapterFactory(new RequiredFieldTypeAdapterFactory()); if (wrapped) { gsonBuilder.registerTypeAdapterFactory(new WrappedModelTypeAdapterFactory(type, typeKey)); } onBuildFactory(gsonBuilder); mGson = NullUtils.nonNullContract(gsonBuilder.create()); }
From source file:com.thirstygoat.kiqo.persistence.GraphAdapterBuilder.java
License:Apache License
public void registerOn(GsonBuilder gsonBuilder) { final Factory factory = new Factory(instanceCreators); gsonBuilder.registerTypeAdapterFactory(factory); for (final Map.Entry<Type, InstanceCreator<?>> entry : instanceCreators.entrySet()) { gsonBuilder.registerTypeAdapter(entry.getKey(), factory); }//from w ww . j a va 2s . com }