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.textcontrol.reportingcloud.ReportingCloud.java

/**
 * Creates a new ReportingCloud wrapper instance.
 *
 * @param username The username./*ww  w  . j  a  va2  s .c o m*/
 * @param password The password.
 * @param baseUrl The API base URL.
 */
public ReportingCloud(String username, String password, String baseUrl) {
    _username = username;
    _password = password;
    _baseUrl = baseUrl;
    // Remove possible slash at the end of base url
    if (_baseUrl.endsWith("/"))
        _baseUrl = _baseUrl.substring(0, _baseUrl.length() - 2);
    _version = DEFAULT_VERSION;

    // Register custom JSON deserializers and create json parser instance
    GsonBuilder gb = new GsonBuilder();
    gb.registerTypeAdapter(Template.class, new TemplateDeserializer());
    gb.registerTypeAdapter(AccountSettings.class, new AccountSettingsDeserializer());
    gb.registerTypeAdapter(MergeSettings.class, new MergeSettingsSerializer());
    gb.registerTypeAdapter(MergeBody.class, new MergeBodySerializer());
    gb.registerTypeAdapter(TemplateInfo.class, new TemplateInfoDeserializer());
    gb.registerTypeAdapter(FindAndReplaceBody.class, new FindAndReplaceBodySerializer());
    gb.serializeNulls();
    _gson = gb.create();
}

From source file:com.thoughtworks.go.server.web.JsonRenderer.java

License:Apache License

private static Gson gsonBuilder(final GoRequestContext requestContext) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonUrl.class, (JsonSerializer<JsonUrl>) (src, typeOfSrc, context) -> {
        if (requestContext == null) {
            return new JsonPrimitive(src.getUrl());
        } else {/*from  ww w .ja v  a2 s .c  o  m*/
            return new JsonPrimitive(requestContext.getFullRequestPath() + src.getUrl());
        }
    });

    builder.registerTypeHierarchyAdapter(MessageSourceResolvable.class,
            (JsonSerializer<MessageSourceResolvable>) (src, typeOfSrc, context) -> {
                if (requestContext == null) {
                    return new JsonPrimitive(src.getDefaultMessage());
                } else {
                    return new JsonPrimitive(requestContext.getMessage(src));
                }
            });

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

From source file:com.trustly.api.SignedAPI.java

License:Open Source License

/**
 * Sends given request to Trustly./* w  ww  .  j  a  v  a2s  .c  o m*/
 * @param request Request to send to Trustly API
 * @return Response generated from the request.
 */
public Response sendRequest(final Request request) {
    final GsonBuilder gsonBuilder = new GsonBuilder();

    if (request.getMethod() == Method.VIEW_AUTOMATIC_SETTLEMENT_DETAILS_CSV) {
        gsonBuilder.serializeNulls();
    }

    final Gson gson = gsonBuilder.create();

    signatureHandler.insertCredentials(request);
    signatureHandler.signRequest(request);

    final String jsonResponse = newHttpPost(gson.toJson(request, Request.class));

    return handleJsonResponse(jsonResponse, request.getUUID());
}

From source file:com.vsthost.rnd.jpsolver.data.Value.java

License:Apache License

/**
 * Returns the JSON representation of the value.
 *
 * TODO: Implement a toJson (class, serializer) method for user-defined classes implementing Value interface.
 *
 * @return The JSON representation of the value.
 */// ww  w .  j  a v a  2 s  . co  m
public default String toJson() {
    // Initialize the Json builder:
    GsonBuilder gsonBuilder = new GsonBuilder();

    // Register serializers for scalar values:
    gsonBuilder.registerTypeAdapter(BooleanValue.class, new ScalarValueSerializer());
    gsonBuilder.registerTypeAdapter(NumericValue.class, new ScalarValueSerializer());
    gsonBuilder.registerTypeAdapter(TextValue.class, new ScalarValueSerializer());

    // We want nulls to be serialized:
    gsonBuilder.serializeNulls();

    // Done, get the JSON:
    return gsonBuilder.create().toJson(this.get());
}

From source file:edu.data.AlgorithmResults.java

public String toJson() {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.serializeNulls().create();
    String json = gson.toJson(this);
    return json;//from  w  ww .  j ava 2  s .c om
}

From source file:gov.whitehouse.utils.GsonUtils.java

License:MIT License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls whether nulls should be serialized
 * @return created gson, never null// ww w .  j  av a2  s.c o  m
 */
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:io.weba.processor.flink.event.gson.factory.EventFactory.java

License:Open Source License

public Gson create() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Id.class, new IdSerializer());
    gsonBuilder.registerTypeAdapter(Type.class, new TypeSerializer());
    gsonBuilder.registerTypeAdapter(Visitor.class, new VisitorSerializer());
    gsonBuilder.registerTypeAdapter(Session.class, new SessionSerializer());
    gsonBuilder.registerTypeAdapter(Map.class, new MapSerializer());
    gsonBuilder.registerTypeAdapter(Payload.class, new MapSerializer());
    gsonBuilder.serializeNulls();
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    gsonBuilder.enableComplexMapKeySerialization();

    return gsonBuilder.create();
}

From source file:mediasearch.twitter.TwitterService.java

private ArrayList<TwitterTweet> parseJsonIntoTweets(JSONObject object) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(String.class, new NullJsonStringConverter());
    gsonBuilder.serializeNulls();
    Gson gson = gsonBuilder.create();/*from   w w w .ja v a  2s .  c  o m*/

    JSONArray array = (JSONArray) object.get("statuses");
    for (int i = 0; i < array.size(); i++) {
        JSONObject jSONObject = (JSONObject) array.get(i);
        TwitterTweet tweet = gson.fromJson(jSONObject.toString(), TwitterTweet.class);
        ret.add(tweet);
    }

    return ret;
}

From source file:org.apache.camel.component.gson.GsonDataFormat.java

License:Apache License

@Override
protected void doStart() throws Exception {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();
        if (exclusionStrategies != null && !exclusionStrategies.isEmpty()) {
            ExclusionStrategy[] strategies = exclusionStrategies
                    .toArray(new ExclusionStrategy[exclusionStrategies.size()]);
            builder.setExclusionStrategies(strategies);
        }/*from   www  .  j  a  va  2  s .c  om*/
        if (longSerializationPolicy != null) {
            builder.setLongSerializationPolicy(longSerializationPolicy);
        }
        if (fieldNamingPolicy != null) {
            builder.setFieldNamingPolicy(fieldNamingPolicy);
        }
        if (fieldNamingStrategy != null) {
            builder.setFieldNamingStrategy(fieldNamingStrategy);
        }
        if (serializeNulls != null && serializeNulls) {
            builder.serializeNulls();
        }
        if (prettyPrinting != null && prettyPrinting) {
            builder.setPrettyPrinting();
        }
        if (dateFormatPattern != null) {
            builder.setDateFormat(dateFormatPattern);
        }
        gson = builder.create();
    }
}

From source file:org.eclipse.egit.github.core.client.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls/*from w  ww .j a va  2  s. c om*/
 *            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();
}