question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Populate existing object

See original GitHub issue
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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
mbalickcommented, Sep 12, 2019

I’ve been looking for this functionality for years. @inder123 said “We are still searching for an elegant proposal.” What’s wrong with something like gson.fromJson(existingObject, json) ? I don’t see why it would not cover the same use cases as gson.fromJson(json).

5reactions
inder123commented, Mar 11, 2016

You can implement this using InstanceCreators.

final Foo existing;
InstanceCreator<Foo> creator = new InstanceCreator<Foo>() {
  public Foo createInstance(Type type) { return existing; }
}

Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class, creator).create();
Foo value = gson.fromJson(jsonString, Foo.class);
// value should be same as existing
Read more comments on GitHub >

github_iconTop Results From Across the Web

Net Core 3.0 JsonSerializer populate existing object
//To populate an existing variable we will do so, we will create a variable with the pre existing data object ; //After this...
Read more >
Populate an Object - Json.NET
This sample populates an existing object instance with values from JSON.
Read more >
C# - Populate an existing object with JSON - MAKOLYTE
To populate this existing object with JSON, you can use the Newtonsoft method JsonConvert.PopulateObject():. using Newtonsoft.
Read more >
Will JsonSerializer support parsing to existing object? #29538
Json supporting the equivalent of PopulateObject I have to jump through hoops with temporary instances and deep copies. I have it working for ......
Read more >
C# – .Net Core 3.0 JsonSerializer populate existing object
Net Core 3.0 JsonSerializer populate existing object. asp.net-coreasp.net-core-3.0c++razor-pagessystem.text.json. I'm preparing a migration from ASP.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found