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.

Read and write Json properties using methods (ie. getters & setters)

See original GitHub issue
if your object has an int getId(); method. Maybe you want to have a 

@Field("id")
public int getId();

When the Json is generated the getId() method is called and the result is 
placed in "id"
I know that I could have a int field in my class, but, in my case the Id is 
generate by Hibernate once the object is saved.

Original issue reported on code.google.com by Germanattanasio on 7 Sep 2010 at 2:04

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:14
  • Comments:32

github_iconTop GitHub Comments

4reactions
rajsrivastav1919commented, Aug 3, 2017

@erik777 Here is an implementation for the same if you want to use getter/setter methods. I’ve added a flag to tell Gson whether to use getter/setter methods or work in the default way.

Though I agree with @inder123 that ideally Gson should be defaulting to using reflection to set fields. Otherwise, it won’t be a pure Gson serialisation/deserialisation library (which it is meant for). An object serialised via Gson should exactly give the same object when deserialised with it, which will not be always true if using getter/setter methods. Hence, only getter/setter implementation is not suitable here. Still, I would appreciate if Gson at least decides to include flag based approach in their code, because in some or other case, its useful.

1reaction
mattbushellcommented, Jul 29, 2019

I need this feature; i know i could use a TypeAdapter, but this option would be more succinct

Usecase normalising data I do not control:

public class SomeResponse {
	@SerializedName("SomeObject")
	Map<Integer, SomeObject> idSomeObjectMap;

	public void setIdSomeObjectMap(Map<Integer, SomeObject> idSomeObjectMap) {
		this.idSomeObjectMap = idSomeObjectMap;

		//need to put ids on objects
		idSomeObjectMap.entrySet().stream().forEach(me -> me.getValue().setSomeObjectID(me.getKey()));
	}

	public static class SomeObject {
		Integer someObjectsID;

		@SerializedName("N")
		String someObjectsName;

		public void setSomeObjectID(Integer someObjectID) {
			this.someObjectsID = someObjectID;
		}
	}
}

Response for example:

"SomeObject": {
      "173": {
        "N": "SomeObject has a name"
      },
      "246": {
        "N": "SomeObject also has a name"
      }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a Model From JSON Response | by Mercy Jemosop
The model class should have a getter and setter methods. You can generate them manually or use annotations i.e @Getter and @Setter or...
Read more >
REST Assured Tutorial 38 – How Getter & Setter methods ...
Below is a POJO class with some private properties and their public accessors i.e. getter and setter methods. I have put a print...
Read more >
Significance of Getters and Setters in Java - Baeldung
Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class.
Read more >
Getter and Setter in Python - GeeksforGeeks
We use getters & setters to add validation logic around getting and setting a value. To avoid direct access of a class field...
Read more >
Getting rid of Getters and Setters in your POJO
We all have read in Java books about encapsulation of fields in Java class and also when ever you code you are asked...
Read more >

github_iconTop Related Medium Post

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