-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read and write Json properties using methods (ie. getters & setters) #232
Comments
This would be very helpful when working with JavaFX properties, e.g. |
GSon is useless for me without this feature. Wanted to use it for client server communication together with a GWT app while using a common Data interface. The point is, that the server already has it's very own object model and likes only to provide a view for the json serialisation which should look like the data required inside the GWT app. Would have worked perfectly if gson had supported getter/setter like serialisation. |
I have the same requirement, I want Gson to use setters for setting the field instead of reflection. With current Gson implementation, thrift objects can't be deserialized "effectively". Other JSON & XML deserializing libraries (like Jackson, CastorXML) call setters, and hence the objects formed are compatible with thrift. For now, I am using Gson by making changes in it for calling setter methods instead of using reflection. From the thread above, I see that you don't want to give native support in Gson for this feature. For 99% POJO classes current implementation works I agree. But, having as an optional feature shouldn't harm anyone I guess. |
@rajsrivastav1919 Would be great for someone to fork Gson with support for this. As we understand the solution better, we will consider adding it. |
Hi, let me add to this and outline our teams desired use case for this feature: We came to use gson as the couchdb java driver uses gson. Every POJO you want to store is serialized by gson. What we would like to do is add a generic interface that contains the standard fields (i.e. their getters) that every object in our db needs (Think revision, last updated, stuff like this). Unfortunately this is not possible with gson not being able to serialize based on getter/setter methods. |
Please make this a priority. I have a case where the base object has a property that subclasses override and do not want to permit incoming data to change. I find it ironic that getters/setters are rendered useless with Gson. These things do not work:
OK with Java. Not OK with Gson:
Ignored by Gson.
Also ignored by Gson. There is no elegant work-around to protect the data from overwrite by HUGE BUMP! If anyone forks this please come back here and comment. I'd personally be happy with a getter/setter only implementation because the security holes the current Gson introduces is pretty significant in today's rapidly growing JSON based API universe. You have no idea what data is coming in from external consumers of your API, and you have no natural OO way to protect internal data. |
@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. |
Is it possible to have @rajsrivastav1919 's implementation into the code base? Besides, it might also be useful if |
I need this feature too. Any updates on this. I see an open pull request but there hasn't been much improvement on that either. |
@TheLoneKing I have not made any updates there because there hasn't been any decision to include it. In case it is decided to approve these changes, I will make the changes as per the latest code at that time. |
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:
|
Is this still in progress...??? |
I also require this feature. |
Original issue reported on code.google.com by
Germanattanasio
on 7 Sep 2010 at 2:04The text was updated successfully, but these errors were encountered: