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.hybris.datahub.outbound.utils.CommonUtils.java

License:Open Source License

/**
 * @param whetherExposeAnnotation/*from  w  w w.  j a  v a2  s.c o m*/
 * @param dateFormat
 * @return Gson instance
 */
public static Gson getGsonByBuilder(final boolean whetherExposeAnnotation, String dateFormat) {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    if (whetherExposeAnnotation) {
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    }

    gsonBuilder.enableComplexMapKeySerialization();

    if (StringUtils.isEmpty(dateFormat)) {
        dateFormat = "yyyy-MM-dd HH:mm:ss";
    }
    gsonBuilder.serializeNulls().setDateFormat(dateFormat);
    gsonBuilder.setVersion(1.0);
    //      gsonBuilder.disableHtmlEscaping();
    return gsonBuilder.create();
}

From source file:com.indicator_engine.model.admin.GLAOperationsJSONObj.java

License:Open Source License

static Gson createGsonFromBuilder(ExclusionStrategy exs) {
    GsonBuilder gsonbuilder = new GsonBuilder();
    gsonbuilder.setExclusionStrategies(exs);
    return gsonbuilder.serializeNulls().create();
}

From source file:com.it.xushuai.baseapp.core.client.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls/*  w  w w.  ja  va 2 s  . co  m*/
 *            whether nulls should be serialized
 *
 * @return created gson, never null
 */
public static final Gson createGson(final boolean serializeNulls) {
    final GsonBuilder builder = new GsonBuilder();
    //      builder.registerTypeAdapter(Date.class, new DateFormatter());
    //      builder.registerTypeAdapter(Event.class, new EventFormatter());
    builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
    if (serializeNulls)
        builder.serializeNulls();
    return builder.create();
}

From source file:com.jd.survey.service.util.JsonHelperService.java

License:Open Source License

public String serializeSurveyDefinition(SurveyDefinition surveyDefinition) {
    try {// w w  w . j a v  a2s .c  o m
        GsonBuilder gsonBuilder = new GsonBuilder();
        //set up the fields to skip in the serialization
        gsonBuilder = gsonBuilder.setExclusionStrategies(new ExclusionStrategy() {
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }

            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                boolean skip = (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("department"))
                        || (f.getDeclaringClass() == SurveyDefinition.class && f.getName().equals("users"))
                        || (f.getDeclaringClass() == SurveyDefinitionPage.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == SurveyDefinitionPage.class
                                && f.getName().equals("surveyDefinition"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("page"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("optionsList"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("rowLabelsList"))
                        || (f.getDeclaringClass() == Question.class && f.getName().equals("columnLabelsList"))
                        || (f.getDeclaringClass() == QuestionOption.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == QuestionOption.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == QuestionOption.class && f.getName().equals("question"))
                        || (f.getDeclaringClass() == QuestionRowLabel.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == QuestionRowLabel.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == QuestionRowLabel.class && f.getName().equals("question"))
                        || (f.getDeclaringClass() == QuestionColumnLabel.class && f.getName().equals("id"))
                        || (f.getDeclaringClass() == QuestionColumnLabel.class && f.getName().equals("version"))
                        || (f.getDeclaringClass() == QuestionColumnLabel.class
                                && f.getName().equals("question"));
                return skip;
            }

        });

        //de-proxy the object
        gsonBuilder.registerTypeHierarchyAdapter(HibernateProxy.class, new HibernateProxySerializer());
        Hibernate.initialize(surveyDefinition);
        if (surveyDefinition instanceof HibernateProxy) {
            surveyDefinition = (SurveyDefinition) ((HibernateProxy) surveyDefinition)
                    .getHibernateLazyInitializer().getImplementation();
        }
        Gson gson = gsonBuilder.serializeNulls().create();
        return gson.toJson(surveyDefinition);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}

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

License:Apache License

/**
 * ????? {@code JSON} ?/*from  ww w  .  jav a  2 s  .c o  m*/
 * <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} ??// w ww.  j av  a2  s .co  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:com.microsoft.aad.test.todoapi.ToDoActivity.java

License:Open Source License

/**
 * Register gson serializer for long and date type
 * /*from   w  ww.j a v a2 s .c  om*/
 * @return
 */
public static GsonBuilder createServiceGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();

    // Register custom date serializer/deserializer
    gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
    LongSerializer longSerializer = new LongSerializer();
    gsonBuilder.registerTypeAdapter(Long.class, longSerializer);
    gsonBuilder.registerTypeAdapter(long.class, longSerializer);
    gsonBuilder.serializeNulls();
    return gsonBuilder;
}

From source file:com.mzs.httpcore.client.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link com.google.gson.Gson} configuration
 *
 * @param serializeNulls/* w w  w  .  j a  va 2s . co m*/
 *            whether nulls should be serialized
 *
 * @return created gson, never null
 */
public static final Gson createGson(final boolean serializeNulls) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateFormatter());
    builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
    if (serializeNulls)
        builder.serializeNulls();
    return builder.create();
}

From source file:com.softwarementors.extjs.djn.gson.DefaultGsonBuilderConfigurator.java

License:Open Source License

public void configure(GsonBuilder builder, GlobalConfiguration configuration) {
    assert builder != null;
    assert configuration != null;

    if (configuration.getDebug()) {
        builder.setPrettyPrinting();// ww  w. j  ava  2 s  .  co  m
    }
    builder.serializeNulls();
    builder.disableHtmlEscaping();

    builder.registerTypeAdapter(Date.class, new DateDeserializer());
    builder.registerTypeAdapter(Date.class, new DateSerializer());
}

From source file:com.textcontrol.reportingcloud.gson.MergeBodySerializer.java

@Override
public JsonElement serialize(MergeBody src, Type typeOfSrc, JsonSerializationContext context) {
    GsonBuilder gb = new GsonBuilder();
    gb.registerTypeAdapter(MergeSettings.class, new MergeSettingsSerializer());
    gb.serializeNulls();
    Gson gson = gb.create();//from ww  w.ja  va2 s  .  c o  m

    JsonObject result = new JsonObject();
    byte[] templateData = src.getTemplate();
    String templateDataB64 = null;
    if (templateData != null) {
        templateDataB64 = Base64.getEncoder().encodeToString(templateData);
    }

    result.addProperty("template", templateDataB64);
    result.add("mergeSettings", gson.toJsonTree(src.getMergeSettings()));
    result.add("mergeData", gson.toJsonTree(src.getMergeData()));
    return result;
}