Closed GoogleCodeExporter closed 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
Original comment by limpbizkit
on 6 Oct 2010 at 6:36
Original comment by inder123
on 3 Nov 2010 at 1:46
interested in this method
Original comment by taps...@gmail.com
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 oliver.s...@gmail.com
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
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
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
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
Original issue reported on code.google.com by
adamchuk
on 20 Oct 2009 at 3:47