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.kodokux.github.api.GithubApiUtil.java

License:Apache License

private static Gson initGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder.create();
}

From source file:com.kotcrab.vis.editor.module.editor.GsonModule.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/* w w w .  j av a  2s.  c o m*/
public void init() {
    ClassJsonSerializer classSerializer;

    GsonBuilder builder = new GsonBuilder().setPrettyPrinting()
            .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.STATIC)
            .registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(Project.class, "@class")
                    .registerSubtype(ProjectLibGDX.class).registerSubtype(ProjectGeneric.class))
            .registerTypeAdapter(Array.class, new ArrayJsonSerializer())
            .registerTypeAdapter(IntArray.class, new IntArrayJsonSerializer())
            .registerTypeAdapter(IntMap.class, new IntMapJsonSerializer())
            .registerTypeAdapter(ObjectMap.class, new ObjectMapJsonSerializer())
            .registerTypeAdapter(Class.class,
                    classSerializer = new ClassJsonSerializer(Thread.currentThread().getContextClassLoader()))
            .registerTypeAdapter(AssetReference.class, new AssetComponentSerializer());

    VisGsonBuilder visBuilder = new VisGsonBuilder(builder);
    for (GsonConfigurator configurator : extensionStorage.getGsonConfigurators()) {
        configurator.configure(visBuilder);
    }

    gson = builder.create();

    EditorJsonTags.registerTags(new GsonTagRegistrar(classSerializer));
}

From source file:com.kurento.kmf.content.jsonrpc.GsonUtils.java

License:Open Source License

/**
 * Gson object accessor (getter)./*from   w w  w  .j a v  a2  s .com*/
 * 
 * @return son object
 */
public static Gson getGson() {
    if (gson != null) {
        return gson;
    }

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonRpcRequest.class, new JsonRpcRequestDeserializer());

    builder.registerTypeAdapter(JsonRpcResponse.class, new JsonRpcResponseDeserializer());

    gson = builder.create();

    return gson;
}

From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java

License:Open Source License

/**
 * Gson object accessor (getter)./*from ww w . java  2 s . c o  m*/
 * 
 * @return son object
 */
public static Gson getGson() {
    if (gson != null) {
        return gson;
    }

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Request.class, new JsonRpcRequestDeserializer());

    builder.registerTypeAdapter(Response.class, new JsonRpcResponseDeserializer());

    builder.registerTypeAdapter(Props.class, new JsonPropsAdapter());

    gson = builder.create();

    return gson;
}

From source file:com.l.notel.notel.org.redpin.android.json.GsonFactory.java

License:Open Source License

/**
 * Gets a configured {@link Gson} instance
 * /*from w  w  w.j ava  2  s .  co  m*/
 * @return {@link Gson} instance
 */
public synchronized static Gson getGsonInstance() {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();

        // needed to get proper sub type after deserialization
        builder.registerTypeAdapter(org.redpin.base.core.Fingerprint.class, new BaseFingerprintTypeAdapter());
        builder.registerTypeAdapter(org.redpin.base.core.Location.class, new BaseLocationTypeAdapter());
        builder.registerTypeAdapter(org.redpin.base.core.Map.class, new BaseMapTypeAdapter());
        builder.registerTypeAdapter(org.redpin.base.core.Measurement.class, new BaseMeasurementTypeAdapter());

        // needed in order to deserialize proper the measurement vectors
        builder.registerTypeAdapter(Measurement.class, new MeasurementTypeAdapter());

        gson = builder.create();

    }

    return gson;
}

From source file:com.lib.lapp.net.utils.JSONUtils.java

License:Apache License

/**
 *  {@code JSON} ??//  ww w  .j av  a2  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 (TextUtils.isEmpty(json)) {
        return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (TextUtils.isEmpty(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, token.getType());
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.lib.lapp.net.utils.JSONUtils.java

License:Apache License

/**
 *  {@code JSON} ??<strong>?? {@code JavaBean}
 * </strong>//from   ww  w .jav  a2 s  .  c  om
 *
 * @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 (TextUtils.isEmpty(json)) {
        return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (TextUtils.isEmpty(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    Gson gson = builder.create();
    try {
        return gson.fromJson(json, clazz);
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.lib.lapp.net.utils.JSONUtils.java

License:Apache License

/**
 * ?{@code GsonBuilder} ???? {@code JSON} ?
 * <p/>/*from  w w w. j  a va2s.c om*/
 * ????{@code JavaBean}  <code>"{}"</code>
 * ? <code>"[]"</code> 
 *
 * @param target     
 * @param targetType 
 * @param builder    ?{@code Gson} 
 * @return  {@code JSON} ?
 * @since 1.1
 */
public static String toJson(Object target, Type targetType, GsonBuilder builder) {
    if (target == null)
        return EMPTY_JSON;
    Gson gson = null;
    if (builder == null) {
        gson = new Gson();
    } else {
        gson = builder.create();
    }
    String result = EMPTY_JSON;
    try {
        if (targetType == null) {
            result = gson.toJson(target);
        } else {
            result = gson.toJson(target, targetType);
        }
    } catch (Exception ex) {
        if (target instanceof Collection<?> || target instanceof Iterator<?> || target instanceof Enumeration<?>
                || target.getClass().isArray()) {
            result = EMPTY_JSON_ARRAY;
        }
    }
    return result;
}

From source file:com.liferay.mobile.sdk.json.JSONParser.java

License:Open Source License

protected synchronized static Gson gson() {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();

        for (Entry<Type, Object> entry : adapters.entrySet()) {
            builder.registerTypeAdapter(entry.getKey(), entry.getValue());
        }//w w  w . j  a va2  s.c  o m

        gson = builder.create();
    }

    return gson;
}

From source file:com.liferay.music.portlet.util.MusicFiles.java

License:Open Source License

public static Gson getGson() {
    FieldNamingStrategy customPolicy = new FieldNamingStrategy() {

        @Override/*from  w w  w  .  ja  v a 2s .  c  o m*/
        public String translateName(Field field) {
            String name = field.getName();

            return name.substring(1);
        }
    };

    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setFieldNamingStrategy(customPolicy);

    return gsonBuilder.create();
}