Example usage for com.google.gson GsonBuilder registerTypeAdapter

List of usage examples for com.google.gson GsonBuilder registerTypeAdapter

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder registerTypeAdapter.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization.

Usage

From source file:com.gst.infrastructure.core.serialization.ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson.java

License:Apache License

public ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson() {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
    builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
    builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());

    this.gson = builder.create();
}

From source file:com.gst.infrastructure.core.serialization.ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson.java

License:Apache License

public ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson() {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
    builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
    builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
    builder.setPrettyPrinting();//from   w  w  w . j a  v a2s .  c o  m

    this.gson = builder.create();
}

From source file:com.gst.infrastructure.core.serialization.GoogleGsonSerializerHelper.java

License:Apache License

public Gson createGsonBuilder(final boolean prettyPrint) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
    builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
    builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
    if (prettyPrint) {
        builder.setPrettyPrinting();//from  w  w  w  . jav  a  2 s .  c  o m
    }
    return builder.create();
}

From source file:com.gst.infrastructure.core.serialization.GoogleGsonSerializerHelper.java

License:Apache License

public Gson createGsonBuilderForPartialResponseFiltering(final boolean prettyPrint,
        final Set<String> responseParameters) {

    final ExclusionStrategy strategy = new ParameterListInclusionStrategy(responseParameters);

    final GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
    builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
    builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
    builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
    if (prettyPrint) {
        builder.setPrettyPrinting();/*from  w ww  . j  ava 2 s.  c  o  m*/
    }
    return builder.create();
}

From source file:com.gst.infrastructure.core.serialization.GoogleGsonSerializerHelper.java

License:Apache License

public Gson createGsonBuilderWithParameterExclusionSerializationStrategy(final Set<String> supportedParameters,
        final boolean prettyPrint, final Set<String> responseParameters) {

    final Set<String> parameterNamesToSkip = new HashSet<>();

    if (!responseParameters.isEmpty()) {

        // strip out all known support parameters from expected response to
        // see if unsupported parameters requested for response.
        final Set<String> differentParametersDetectedSet = new HashSet<>(responseParameters);
        differentParametersDetectedSet.removeAll(supportedParameters);

        if (!differentParametersDetectedSet.isEmpty()) {
            throw new UnsupportedParameterException(new ArrayList<>(differentParametersDetectedSet));
        }//  w ww . j  av  a 2s .  co m

        parameterNamesToSkip.addAll(supportedParameters);
        parameterNamesToSkip.removeAll(responseParameters);
    }

    final ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);

    final GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
    builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
    builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
    builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
    if (prettyPrint) {
        builder.setPrettyPrinting();
    }
    return builder.create();
}

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  2s  .  c o m
}

From source file:com.hagreve.android.lib.HaGreveApi.java

License:Apache License

/**
 * Obtains the list of current strikes./*from  ww  w. j a va 2  s  .  com*/
 * @return Array of Strike objects
 */
public static Strike[] getStrikes() {
    Strike[] items = new Strike[0];

    String result = doGetRequest(BASE_URL + "/strikes");

    if (result != null) {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    return formatter.parse(json.getAsString());
                } catch (ParseException e) {
                    throw new JsonParseException(e.getMessage());
                }
            }

        });
        Gson gson = builder.create();
        items = gson.fromJson(result, Strike[].class);
    }

    return items;
}

From source file:com.hawk.lib.base.model.provider.ProviderModule.java

License:Open Source License

@Singleton
@Provides/*from   w w w.  j av a  2s.com*/
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.hbm.devices.scan.announce.AnnounceDeserializer.java

License:Open Source License

/**
 * Constructs a {@link AnnounceDeserializer} object.
 *//*w ww. ja v a 2 s.  c o m*/
public AnnounceDeserializer() {
    super();

    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonRpc.class, new JsonRpcDeserializer());
    builder.registerTypeAdapter(AnnounceParams.class, new AnnounceParamsDeserializer());
    Type serviceListType = new TypeToken<List<ServiceEntry>>() {
    }.getType();
    builder.registerTypeAdapter(serviceListType, new ServiceDeserializer());
    builder.registerTypeAdapter(Interface.class, new InterfaceDeserializer());

    //  builder.registerTypeAdapter(IPEntry.class, new IPv4Deserializer());
    gson = builder.create();

    this.announceCache = new AnnounceCache();
}

From source file:com.hbm.devices.scan.configure.ResponseDeserializer.java

License:Open Source License

/**
 * Constructs a {@link ResponseDeserializer} object.
 *///from  w ww . j  a  v a 2  s . c o  m
public ResponseDeserializer() {
    super();

    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonRpc.class, new JsonRpcDeserializer());
    gson = builder.create();
}