Example usage for com.google.gson GsonBuilder setVersion

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

Introduction

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

Prototype

public GsonBuilder setVersion(double ignoreVersionsAfter) 

Source Link

Document

Configures Gson to enable versioning support.

Usage

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

License:Apache License

/**
 * ????? {@code JSON} ?//ww  w .jav  a2  s .  c  om
 * <p/>
 * <strong>???? <code>"{}"</code> ?
 * <code>"[]"</code> </strong>
 *
 * @param target                      
 * @param targetType                  
 * @param isSerializeNulls            ?? {@code null} 
 * @param version                     ?
 * @param datePattern                 ??
 * @param excludesFieldsWithoutExpose ? {@literal @Expose} 
 * @return  {@code JSON} ?
 * @since 1.0
 */
public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null)
        return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls)
        builder.serializeNulls();
    if (version != null)
        builder.setVersion(version.doubleValue());
    if (TextUtils.isEmpty(datePattern))
        datePattern = DEFAULT_DATE_PATTERN;
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    return toJson(target, targetType, builder);
}

From source file:com.logisticsShop.utils.JsonUtil.java

License:Apache License

/**
 * ????? {@code JSON} ??//from w w w  .  j  a  va  2  s .  c  o m
 * <p />
 * <b>??????<code>"{}"</code>???<code>"[]"</code> </b>
 * 
 * @param target
 *            ?
 * @param targetType
 *            ?
 * @param isSerializeNulls
 *            ???{@code null} ?
 * @param version
 *            ??
 * @param datePattern
 *            ???
 * @param excludesFieldsWithoutExpose
 *            ??{@literal @Expose} ?
 * @return ?{@code JSON} ??
 * @since 1.0
 */
public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null)
        return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls)
        builder.serializeNulls();
    if (version != null)
        builder.setVersion(version.doubleValue());
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    return toJson(target, targetType, builder);
}

From source file:org.apache.cloudstack.framework.serializer.JsonMessageSerializer.java

License:Apache License

public JsonMessageSerializer() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setVersion(1.5);
    _gson = gsonBuilder.create();
}

From source file:org.foxbpm.web.common.util.JSONUtil.java

License:Apache License

private static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null)
        return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls)
        builder.serializeNulls();/*from ww w .j a  v  a  2 s.  c om*/
    if (version != null)
        builder.setVersion(version.doubleValue());
    // if (StringUtil.isEmpty(datePattern))
    // datePattern = DEFAULT_DATE_PATTERN;
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    String result = null;
    Gson gson = builder.create();
    try {
        if (targetType != null) {
            result = gson.toJson(target, targetType);
        } else {
            result = gson.toJson(target);
        }
    } catch (Exception ex) {
        if (target instanceof Collection || target instanceof Iterator || target instanceof Enumeration
                || target.getClass().isArray()) {
            result = EMPTY_JSON_ARRAY;
        } else
            result = EMPTY_JSON;
    }
    return result;
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

/**
 * @return the gson//from   w w w  .  jav a  2 s .c  o  m
 */
public Gson getGson() {
    if (gson == null) {
        GsonBuilder gbuilder = new GsonBuilder();
        if (serializeNulls) {
            gbuilder.serializeNulls();
        }
        gbuilder.setDateFormat(datePattern);
        if (prettyPrinting) {
            gbuilder.setPrettyPrinting();
        }
        if (excludeFieldsWithoutExposeAnnotation) {
            gbuilder.excludeFieldsWithoutExposeAnnotation();
        }
        if (enableComplexMapKey) {
            gbuilder.enableComplexMapKeySerialization();
        }
        if (version != null) {
            gbuilder.setVersion(version);
        }
        gson = gbuilder.create();
    }
    return gson;
}