Gson to json dynamically add map to object

I have an object that represents an event that I would like to serialize to json using gson or another library if that would work easier.

I want to add the following field type to json:

private Map<String, String> additionalProperties;

      

And also in another case:

private Map<String, Object> additionalProperties;

      

But if I add additional properties to the Event object and create the Gson in the usual way:

Gson gson = BUILDER.create();
String json = gson.toJson(event);

      

It will look like this:

additional_properties: {"value1":1,"value2":"abc"}

      

I would just like to add the following form to the event object:

{"value1":1,"value2":"abc"}

      

Here's an example of the output - the additional properties added are the 'z' object and the advertiser of the object:

{"organisationid":"2345612ß","projectid":"12345678",
"place":{"placeId":"2345","last_place":"123-3"},
"advertiser":{"advertiserId":"2345a","code":"a123-3"},
"user":{"isY":false,"isHere":false,"isBuyer":false},
"x":{"identifier":"SHDG-28CHD"},
"z":{"identifier":"abcSHDG-28CHD"},
"event_type":"x_depart"}

      

This is what it looks like now:

{"organisationid":"2345612ß","projectid":"12345678",
"place":{"placeId":"2345","last_place":"123-3"},
additionalproperty: {"advertiser":{"advertiserId":"2345a","code":"a123-3"},
"user":{"isY":false,"isHere":false,"isBuyer":false},
"x":{"identifier":"SHDG-28CHD"},
additionalproperty: {"z":{"identifier":"abcSHDG-28CHD"}},
"event_type":"x_depart"}

      

+3


source to share


2 answers


The best way to solve this is to create a framework to add properties dynamically, but without that you can add additional properties to the Event object (including @JsonIgnore so it is not part of the final json), create a new JSONObject from the additional properties, and combine it into a JSONObject event before serialization to json. Thus, additional properties are added dynamically to the resulting Event output.

In the Event class:

@JsonIgnore
    private Map<String, Object> additionalProperties;

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperties(Map<String, Object> additionalProperties) {
        this.additionalProperties = additionalProperties;
    }

      

Function to combine two JSONObjects:



public static JSONObject mergeObjects(JSONObject source, JSONObject target) throws JSONException {
    for (String key: JSONObject.getNames(source)) {
            Object value = source.get(key);
            if (!target.has(key)) {
                // new value for "key":
                target.put(key, value);
            } else {
                // existing value for "key" - recursively deep merge:
                if (value instanceof JSONObject) {
                    JSONObject valueJson = (JSONObject)value;
                    deepMerge(valueJson, target.getJSONObject(key));
                } else {
                    target.put(key, value);
                }
            }
    }
    return target;
}

      

Combining two objects:

 String jsonAdd = mapper.writeValueAsString(additional);
 String jsonEvent = mapper.writeValueAsString(event);

 JSONObject jsonAddObj = new JSONObject(jsonAdd);
 JSONObject JsonEventObj = new JSONObject(jsonEvent);
 JSONObject finalJson = Merge.deepMerge(jsonAddObj, JsonEventObj);

      

+2


source


If your class Event

is something like:

class Event {
    private Map<String, Object> additionalProperties;
}

      

and you call;

String json = gson.toJson(event);

      

Expected and Actual Output:

{"additionalProperties":{"value1":1,"value2":"abc"}}

      



If you want an output like:

{"value1":1,"value2":"abc"}

      

You may call:

String json = gson.toJson(event.additionalProperties);

      

I just would like to add the following form to the event object:

{"value1":1,"value2":"abc"}

      

So it won't be a valid json variable if you add that value directly to the json object. You better try if the json value you want is valid or not, http://jsonviewer.stack.hu/

0


source







All Articles