Gson: merge method for JsonObject

Created on 19 Mar 2015  路  10Comments  路  Source: google/gson

What about new method(s) merge for JsonObject... it would be something
simmilar to JsonArray.addAll

void    merge(JsonObject value) 
void    merge(Object value) 

Original issue reported on code.google.com by adamchuk on 20 Oct 2009 at 3:47

Milestone-Undefined Priority-Medium Type-Enhancement auto-migrated

Most helpful comment

Hello,

I know it's an old issue to discuss, but I still believe it could be a fine 
feature to have. 
I'm thinking something about this:

new 
GsonMerger().setConflictResolutionStrategy(myResolutionStrategy).merge(jsonObjec
t1, jsonObject2).build();

new 
GsonMerger().setConflictResolutionStrategy(myResolutionStrategy).merge(jsonObjec
t1).merge(jsonObject2).build();

where 

interface ConflictResolutionStrategy {
      void handleError(JsonElement parent, JsonElement oldElement, JsonElement newElement);
}

Following limpbizkit's comment, available options could be:

DefaultConflictResolutionStrategy - throws IllegalArgumentException if name 
conflict [DEFAULT]
AppendElementConflictResolutionStrategy - appends it to the same "bucket"
NonNullElementConflictResolutionStrategy - replaces the previous element only 
if null
NewerElementConflictResolutionStrategy - always replaces the previous element

Why do I refloat this? Because all the examples I saw they were with shallow 
json trees, just one depth-level. It comes a little bit more complex with 
greater json graphs. A single-line utility would be perfect.

Thanks,

Sebas.-

Original comment by [email protected] on 11 Sep 2014 at 3:01

All 10 comments

I don't know if I correctly understand this request. I would like gson to have a
merge function that adds all values from an jsonstring to an existing object 
but not
changing the values that don't exist in the json string. 

something like: 

gson.merge(String updatedValuesAsJson, class of the object, originalObject)

Original comment by [email protected] on 6 May 2010 at 3:00

Original comment by limpbizkit on 6 Oct 2010 at 6:36

  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

Original comment by inder123 on 3 Nov 2010 at 1:46

  • Added labels: Milestone-Undefined
interested in this method

Original comment by [email protected] on 23 Sep 2011 at 4:49

I would also like to see this. It would help me when dealing with more complex 
serialization issues.

Imagine the rather simple example:

{id: 42} and {value: "answer"}

Merging these two JsonElements to {id: 42, value: "answer"} is hard with the 
current API

Original comment by [email protected] on 3 Jan 2012 at 12:11

It's pretty straightforward to write your own static merge method:
  public static merge(JsonObject a, JsonObject b)

The best part about writing your own method is that you get to define your own 
semantics when there's a conflict. There's lots of strategies that Gson could 
do, but application developers will know best.

A: { "name": "Jesse" }
B: { "name": "Inder" }
Conflict resolution: throw IllegalArgumentException

A: { "name": "Jesse", "pets": "Butters" }
B: { "name": "Jesse", "pets": "McFly" }
Conflict resolution: create an array
   { "name": "Jesse", "pets": [ "Butters", "McFly" ] }

A: { "name": "Jesse", "city": null }
B: { "name": "Jesse", "city": "Waterloo" }
Conflict resolution: prefer non-null over null
   { "name": "Jesse", "city": "Waterloo" }

For this reason I'd prefer we not implement a merge() method that assumes we'll 
get the behavior developers want.

Original comment by limpbizkit on 3 Jan 2012 at 3:10

  • Changed state: WontFix
Hello,

I know it's an old issue to discuss, but I still believe it could be a fine 
feature to have. 
I'm thinking something about this:

new 
GsonMerger().setConflictResolutionStrategy(myResolutionStrategy).merge(jsonObjec
t1, jsonObject2).build();

new 
GsonMerger().setConflictResolutionStrategy(myResolutionStrategy).merge(jsonObjec
t1).merge(jsonObject2).build();

where 

interface ConflictResolutionStrategy {
      void handleError(JsonElement parent, JsonElement oldElement, JsonElement newElement);
}

Following limpbizkit's comment, available options could be:

DefaultConflictResolutionStrategy - throws IllegalArgumentException if name 
conflict [DEFAULT]
AppendElementConflictResolutionStrategy - appends it to the same "bucket"
NonNullElementConflictResolutionStrategy - replaces the previous element only 
if null
NewerElementConflictResolutionStrategy - always replaces the previous element

Why do I refloat this? Because all the examples I saw they were with shallow 
json trees, just one depth-level. It comes a little bit more complex with 
greater json graphs. A single-line utility would be perfect.

Thanks,

Sebas.-

Original comment by [email protected] on 11 Sep 2014 at 3:01

This is needed, please revoke the resolution "won't fix" and have a look at 
Sebas suggestion above.

Original comment by [email protected] on 8 Dec 2014 at 11:56

Sebas' suggestion seems spot on to me and would be extremely useful.

Original comment by [email protected] on 27 Feb 2015 at 5:55

plus one to Sebas' suggestion ! to do a deep copy is non-trivial and more scope for bugs. the strategies suggested could handle this, and one could always plug-in their own strategy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

priyankajagtap18 picture priyankajagtap18  路  14Comments

LucianWang picture LucianWang  路  42Comments

cayhorstmann picture cayhorstmann  路  13Comments

JakeWharton picture JakeWharton  路  39Comments

RobMans426 picture RobMans426  路  20Comments