Comment #2 on issue 166 by limpbizkit: merge method for JsonObject
http://code.google.com/p/google-gson/issues/detail?id=166
(No comment was entered for this change.)
Comment #3 on issue 166 by inder123: merge method for JsonObject
interested in this method
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
Comment #6 on issue 166 by limpbiz...@gmail.com: merge method for JsonObject
http://code.google.com/p/google-gson/issues/detail?id=166
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.