Example usage for com.google.gson GsonBuilder serializeNulls

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

Introduction

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

Prototype

boolean serializeNulls

To view the source code for com.google.gson GsonBuilder serializeNulls.

Click Source Link

Usage

From source file:com.expensify.testframework.SubmitResultContainer.java

License:Microsoft Public License

public static String RawStringify(ResultBase result) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();//  w  ww. ja v  a  2s. co  m
    gsonBuilder.serializeNulls();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    gsonBuilder.disableHtmlEscaping();
    gsonBuilder.registerTypeHierarchyAdapter(ResultBase.class, new ResultSerializer());
    String resultJson = gson.toJson(result);
    resultJson = hackResultJsonOrder(resultJson);
    return resultJson;
}

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

License:Apache License

/**
 * ????? {@code JSON} ?// ww w  . j  av  a2s .com
 * <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 (Tools.isEmpty(datePattern))
        datePattern = DEFAULT_DATE_PATTERN;
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    return toJson(target, targetType, builder);
}

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

License:Apache License

public static String toString(Object obj) {

    GsonBuilder builder = new GsonBuilder();
    builder.serializeNulls();
    builder.disableHtmlEscaping();//  w  ww  .  ja  v a2s. c  om
    builder.setPrettyPrinting();
    Gson gson = builder.create();
    return gson.toJson(obj);
}

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

License:Apache License

/**
 * // ww  w.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.goforer.base.model.BaseModel.java

License:Apache License

public static GsonBuilder gsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    builder.registerTypeAdapter(ImageMap.class, new ImageMap.ImageMapDeserializer());
    builder.registerTypeAdapter(ImageMap.class, new ImageMap.ImageMapSerializer());
    builder.serializeNulls();
    return builder;
}

From source file:com.google.android.mobly.snippet.bundled.utils.JsonSerializer.java

License:Apache License

public JsonSerializer() {
    GsonBuilder builder = new GsonBuilder();
    mGson = builder.serializeNulls().excludeFieldsWithModifiers(Modifier.STATIC)
            .enableComplexMapKeySerialization().disableInnerClassSerialization().create();
}

From source file:com.google.wave.api.impl.GsonFactory.java

License:Apache License

/**
 * Creates a {@link Gson} instance, with additional type adapters for these
 * types://  w  ww. j  av  a 2s. c o m
 * <ul>
 *   <li>{@link EventMessageBundle}</li>
 *   <li>{@link OperationRequest}</li>
 *   <li>{@link Element}</li>
 *   <li>{@link JsonRpcResponse}</li>
 * </ul>
 *
 * @param opNamespace prefix that should be prepended to the operation during
 *     serialization.
 * @return an instance of {@link Gson} with pre-registered type adapters.
 */
public Gson create(String opNamespace) {
    ElementGsonAdaptor elementGsonAdaptor = new ElementGsonAdaptor();
    GsonBuilder builder = new GsonBuilder().setExclusionStrategies(new NonSerializableExclusionStrategy())
            .registerTypeAdapter(EventMessageBundle.class, new EventMessageBundleGsonAdaptor())
            .registerTypeAdapter(OperationRequest.class, new OperationRequestGsonAdaptor(opNamespace))
            .registerTypeAdapter(Element.class, elementGsonAdaptor)
            .registerTypeAdapter(Attachment.class, elementGsonAdaptor)
            .registerTypeAdapter(JsonRpcResponse.class, new JsonRpcResponseGsonAdaptor())
            .registerTypeAdapter(Annotation.class, new AnnotationInstanceCreator())
            .registerTypeAdapter(Range.class, new RangeInstanceCreator())
            .registerTypeAdapter(BlipThread.class, new ThreadInstanceCreator())
            .registerTypeAdapter(SearchResult.class, new SearchResultInstanceCreator())
            .registerTypeAdapter(SearchResult.Digest.class, new SearchResultDigestInstanceCreator())
            .registerTypeAdapter(WaveletId.class, new WaveletIdInstanceCreator())
            .registerTypeAdapter(RawAttachmentData.class, new AttachmentDataInstanceCreator())
            .registerTypeAdapter(byte[].class, new ByteArrayGsonAdaptor());

    // Register custom type adapters.
    for (Entry<Type, Object> entry : customTypeAdapters.entrySet()) {
        builder.registerTypeAdapter(entry.getKey(), entry.getValue());
    }

    return builder.serializeNulls().create();
}

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

License:Apache License

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

    this.gson = builder.create();
}

From source file:com.haulmont.cuba.core.app.serialization.EntitySerialization.java

License:Apache License

protected Gson createGsonForSerialization(@Nullable View view, EntitySerializationOption... options) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (ArrayUtils.contains(options, EntitySerializationOption.PRETTY_PRINT)) {
        gsonBuilder.setPrettyPrinting();
    }/*ww  w .ja v  a 2  s.c  om*/
    gsonBuilder.registerTypeHierarchyAdapter(Entity.class, new EntitySerializer(view, options))
            .registerTypeHierarchyAdapter(Date.class, new DateSerializer()).create();
    if (ArrayUtils.contains(options, EntitySerializationOption.SERIALIZE_NULLS)) {
        gsonBuilder.serializeNulls();
    }
    return gsonBuilder.create();
}

From source file:com.hmwg.utils.GSONUtils.java

License:Apache License

/**
 * ????? {@code JSON} ?//from w  w w  . ja v a2 s  .com
 * <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 (isBlankString(datePattern)) {
        datePattern = DEFAULT_DATE_PATTERN;
    }
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose) {
        builder.excludeFieldsWithoutExposeAnnotation();
    }
    return toJson(target, targetType, builder);
}