discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

merge method for JsonObject #166

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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 inno...@gmail.com on 6 May 2010 at 3:00

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago
interested in this method

Original comment by taps...@gmail.com on 23 Sep 2011 at 4:49

GoogleCodeExporter commented 9 years ago
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 oliver.s...@gmail.com on 3 Jan 2012 at 12:11

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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 scotti.s...@gmail.com on 11 Sep 2014 at 3:01

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

Original comment by eirir...@gmail.com on 8 Dec 2014 at 11:56

GoogleCodeExporter commented 9 years ago
Sebas' suggestion seems spot on to me and would be extremely useful.

Original comment by christop...@gmail.com on 27 Feb 2015 at 5:55