Example usage for com.google.gson GsonBuilder create

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

Introduction

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

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.flipkart.batching.gson.GsonSerializationStrategy.java

License:Open Source License

@Override
public void build() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(getDataRuntimeTypeAdapter());
    gsonBuilder.registerTypeAdapterFactory(getBatchRuntimeTypeAdapter());
    gsonBuilder.registerTypeAdapterFactory(new BatchingTypeAdapterFactory());

    TagDataTypeAdapter tagDataTypeAdapter = new TagDataTypeAdapter();
    registerDataSubTypeAdapters(EventData.class, new EventDataTypeAdapter());
    registerDataSubTypeAdapters(TagData.class, tagDataTypeAdapter);
    registerBatchSubTypeAdapters(TagBatch.class, new TagBatchTypeAdapter<>(tagDataTypeAdapter));

    gson = gsonBuilder.create();

    //Register Built in types
    registerBatchSubTypeAdapters(SizeBatch.class, new SizeBatchTypeAdapter<>(getDataTypeAdapter()));
    registerBatchSubTypeAdapters(BatchImpl.class, new BatchImplTypeAdapter<>(getDataTypeAdapter()));
    registerBatchSubTypeAdapters(SizeTimeBatch.class, new SizeTimeBatchTypeAdapter<>(getDataTypeAdapter()));
    registerBatchSubTypeAdapters(TimeBatch.class, new TimeBatchTypeAdapter<>(getDataTypeAdapter()));
}

From source file:com.framework.util.JSONUtil.java

License:Apache License

/**
 *  {@code JSON} ??//ww  w.jav a  2  s  .c  o m
 * 
 * @param <T> ??
 * @param json  {@code JSON} 
 * @param token {@code com.google.gson.reflect.TypeToken} 
 * @param datePattern ??
 * @return  {@code JSON} 
 * @since 1.0
 */
public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) {
    if (Tools.isEmpty(json)) {
        return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (Tools.isEmpty(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, token.getType());
    } catch (Exception ex) {
        LOGGER.error(json + " ? " + token.getRawType().getName() + " !", ex);
        return null;
    }
}

From source file:com.framework.util.JSONUtil.java

License:Apache License

/**
 *  {@code JSON} ??<strong>?? {@code JavaBean} </strong>
 * /*from   www. ja v  a 2s  .  com*/
 * @param <T> ??
 * @param json  {@code JSON} 
 * @param clazz ??
 * @param datePattern ??
 * @return  {@code JSON} 
 * @since 1.0
 */
public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {
    if (Tools.isEmpty(json)) {
        return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (Tools.isEmpty(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, clazz);
    } catch (Exception ex) {
        LOGGER.error(json + " ? " + clazz.getName() + " !", ex);
        return null;
    }
}

From source file:com.fullmeadalchemist.mustwatch.core.JSONResourceReader.java

License:Apache License

/**
 * Build an object from the specified JSON resource using Gson.
 *
 * @param type The type of the object to build.
 * @return An object of type T, with member fields populated using Gson.
 *//*from   w  ww  .j av  a2 s . c om*/
public <T> T constructUsingGson(Class<T> type) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Type volumeType = new TypeToken<Quantity<Volume>>() {
    }.getType();
    JsonDeserializer<Quantity<Volume>> volumeDeserializer = new VolumeDeserializer();
    gsonBuilder.registerTypeAdapter(volumeType, volumeDeserializer);

    Type massType = new TypeToken<Quantity<Mass>>() {
    }.getType();
    JsonDeserializer<Quantity<Mass>> massDeserializer = new MassDeserializer();
    gsonBuilder.registerTypeAdapter(massType, massDeserializer);

    Gson gson = gsonBuilder.create();

    return gson.fromJson(jsonString, type);
}

From source file:com.g3net.tool.ObjectUtils.java

License:Apache License

public static String toString(Object obj) {

    GsonBuilder builder = new GsonBuilder();
    builder.serializeNulls();// w ww .  j a va 2 s.  c om
    builder.disableHtmlEscaping();
    builder.setPrettyPrinting();
    Gson gson = builder.create();
    return gson.toJson(obj);
}

From source file:com.g3net.tool.ObjectUtils.java

License:Apache License

/**
 * //from   www.j a  v  a 2 s. c  om
 * @param obj
 * @param type 
 *   Type typeOfT = new com.google.gson.reflect.TypeToken<Collection<Foo>>(){}.getType();
 * @return
 */
public static String toString(Object obj, Type type) {

    GsonBuilder builder = new GsonBuilder();
    builder.serializeNulls();
    builder.disableHtmlEscaping();
    builder.setPrettyPrinting();
    Gson gson = builder.create();
    return gson.toJson(obj, type);
}

From source file:com.gazbert.ada.adapter.BitstampExchangeAdapter.java

License:Open Source License

private void initGson() {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new BitstampDateDeserializer());
    gson = gsonBuilder.create();
}

From source file:com.gazbert.ada.adapter.BtceExchangeAdapter.java

License:Open Source License

private void initGson() {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(BtceOpenOrders.class, new OpenOrdersDeserializer());
    gson = gsonBuilder.create();
}

From source file:com.gazbert.ada.adapter.CryptsyExchangeAdapter.java

License:Open Source License

private void initGson() {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
    gsonBuilder.registerTypeAdapter(CryptsyBalances.class, new BalancesDeserializer());
    gson = gsonBuilder.create();
}

From source file:com.gazbert.bxbot.exchanges.BitfinexExchangeAdapter.java

License:Open Source License

/**
 * Initialises the GSON layer.//from  ww w  .ja v a 2s  .com
 */
private void initGson() {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gson = gsonBuilder.create();
}