dungnn / google-gson

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

Populate existing object #431

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It would be great is gson supported the population of existing objects. There 
was a post a while back alluding to this behavior, but it doesn't exist now:

http://groups.google.com/group/google-gson/browse_thread/thread/ab97b754c1f3c53a
?pli=1

The rationale is that this would prevent users from having to create 
TypeAdapters or InstanceCreators for every object that requires custom 
construction, particularly since gson doesn't support the notion of a global 
InstanceCreator at the moment.

Original issue reported on code.google.com by pacesysj...@gmail.com on 8 Apr 2012 at 5:12

GoogleCodeExporter commented 9 years ago
Note: This is an enhancement (didn't see a setting for this).

Original comment by pacesysj...@gmail.com on 8 Apr 2012 at 5:13

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 11 Apr 2012 at 8:33

GoogleCodeExporter commented 9 years ago
It would be nice if it will be supported :)

http://stackoverflow.com/questions/12277765/it-is-possible-to-fill-current-objec
t-with-json-values-from-json

Original comment by KKoce...@gmail.com on 12 Feb 2013 at 9:03

GoogleCodeExporter commented 9 years ago
what is the plan for this enhancement? is it on the roadmap?

Original comment by dominik.mengelt on 12 Nov 2013 at 1:34

GoogleCodeExporter commented 9 years ago
I really like to see this enhancement. I am using the library in my Android 
app. I have a ViewPager with existing items, when a page becomes visible, I 
pull additional items remotely and I would like to populate the additional 
fields in the existing items.

Original comment by tasoman...@gmail.com on 26 Nov 2013 at 9:50

GoogleCodeExporter commented 9 years ago
Any update on this?
Any chance that it will ever be implemented? any body have a nice work around?
I'm starting a new project and can choose to use Jackson vs GSON, and I'm 
deeply interested in the ability to update existing instance rather than always 
creating new instance.
I'm thinking of creating a new object and then merging to the existing one 
using reflexion but I would rather have this handle at the deserialization 
layer.

Original comment by hugues.b...@gmail.com on 13 Jan 2015 at 10:26

GoogleCodeExporter commented 9 years ago
See Inderjeet Singh's answer in this thread: 
https://groups.google.com/d/msg/google-gson/q5e3VMHzxTo/DOivwaGC2XoJ

I use the InstanceCreator solution and it works very well. It is far from ideal 
as it requires creating a new Gson instance every time, but it works quite well.

Here is my function :

    public static <T> void populate(GsonBuilder builder, JsonObject json, Class<T> type, T into) {
        builder.registerTypeAdapter(type, new InstanceCreator<T>() {
            @Override public T createInstance(Type t) { return into; }
        }).create().fromJson(json, type);
    }

Original comment by timelza...@gmail.com on 15 Jan 2015 at 2:51