[gson-codereviews:1217]: Re: Issue 166 in google-gson: merge method for JsonObject

80 views
Skip to first unread message

googl...@googlecode.com

unread,
May 6, 2010, 11:01:08 AM5/6/10
to google-gson...@googlegroups.com

Comment #1 on issue 166 by i...@innomos.com: merge method for JsonObject
http://code.google.com/p/google-gson/issues/detail?id=166

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)

--
You received this message because you are subscribed to the Google Groups "google-gson-codereviews" group.
To post to this group, send email to google-gson...@googlegroups.com.
To unsubscribe from this group, send email to google-gson-coder...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson-codereviews?hl=en.

googl...@googlecode.com

unread,
Oct 6, 2010, 3:09:54 PM10/6/10
to google-gson...@googlegroups.com
Updates:
Labels: -Type-Defect Type-Enhancement

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.)

googl...@googlecode.com

unread,
Nov 2, 2010, 10:04:53 PM11/2/10
to google-gson...@googlegroups.com
Updates:
Labels: Milestone-Undefined

Comment #3 on issue 166 by inder123: merge method for JsonObject

googl...@googlecode.com

unread,
Sep 23, 2011, 12:50:09 PM9/23/11
to google-gson...@googlegroups.com

Comment #4 on issue 166 by taps...@gmail.com: merge method for JsonObject
http://code.google.com/p/google-gson/issues/detail?id=166

interested in this method

googl...@googlecode.com

unread,
Jan 3, 2012, 7:12:01 AM1/3/12
to google-gson...@googlegroups.com

Comment #5 on issue 166 by oliver.s...@gmail.com: merge method for
JsonObject
http://code.google.com/p/google-gson/issues/detail?id=166

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

googl...@googlecode.com

unread,
Jan 3, 2012, 10:11:03 AM1/3/12
to google-gson...@googlegroups.com
Updates:
Status: WontFix

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.

googl...@googlecode.com

unread,
Sep 11, 2014, 11:01:43 AM9/11/14
to google-gson...@googlegroups.com

Comment #7 on issue 166 by scotti.s...@gmail.com: merge method for
JsonObject
https://code.google.com/p/google-gson/issues/detail?id=166

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(jsonObject1,
jsonObject2).build();

new
GsonMerger().setConflictResolutionStrategy(myResolutionStrategy).merge(jsonObject1).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.-

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

googl...@googlecode.com

unread,
Dec 8, 2014, 6:56:09 AM12/8/14
to google-gson...@googlegroups.com

Comment #8 on issue 166 by eirir...@gmail.com: merge method for JsonObject
https://code.google.com/p/google-gson/issues/detail?id=166

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

googl...@googlecode.com

unread,
Feb 27, 2015, 12:55:42 PM2/27/15
to google-gson...@googlegroups.com

Comment #9 on issue 166 by christop...@gmail.com: merge method for
JsonObject
https://code.google.com/p/google-gson/issues/detail?id=166

Sebas' suggestion seems spot on to me and would be extremely useful.
Reply all
Reply to author
Forward
0 new messages