Read and write Json properties using methods (ie. getters & setters)
See original GitHub issueif 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:
- Created 9 years ago
- Reactions:14
- Comments:32
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
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:
Response for example: