All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gsonfire.builders.JsonObjectBuilder Maven / Gradle / Ivy

Go to download

A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!

There is a newer version: 1.9.0
Show newest version
package io.gsonfire.builders;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import io.gsonfire.util.JsonUtils;

import java.util.Map;

/**
 * Created by julio on 8/18/16.
 */
public final class JsonObjectBuilder implements JsonElementBuilder {

    private final JsonObject object = new JsonObject();

    public JsonObjectBuilder() {

    }

    public JsonObjectBuilder set(String property, String value) {
        object.addProperty(property, value);
        return this;
    }

    public JsonObjectBuilder set(String property, Number value) {
        object.addProperty(property, value);
        return this;
    }
    public JsonObjectBuilder set(String property, Boolean value) {
        object.addProperty(property, value);
        return this;
    }

    public JsonObjectBuilder set(String property, JsonElement value) {
        object.add(property, value);
        return this;
    }

    public JsonObjectBuilder set(String property, JsonElementBuilder builder) {
        object.add(property, builder.build());
        return this;
    }

    public JsonObjectBuilder setNull(String property) {
        object.add(property, JsonNull.INSTANCE);
        return this;
    }

    /**
     * Copies all the property/values from #jsonObject into the json object being built by this builder
     * @param jsonObject
     * @return
     */
    public JsonObjectBuilder merge(JsonObject jsonObject) {
        for(Map.Entry entry: JsonUtils.deepCopy(jsonObject).getAsJsonObject().entrySet()) {
            object.add(entry.getKey(), entry.getValue());
        }
        return this;
    }

    public JsonObject build() {
        return JsonUtils.deepCopy(object).getAsJsonObject();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy