gson: Support handlers for unknown properties, useful for error handling
What steps will reproduce the problem?
1. create a JSON string containing extra attributes
2. invoke Gson.fromJson supplying a class with fewer elements
3. GSON successfully instantiates the class without protesting about the
existence of extra attributes in the string.
What is the expected output? What do you see instead?
1. define class A containing two fields: name and surname
2. define class B containing only one field: name
3. transform an instance of class A to Json string and use the Json string
to create an instance of class B.
GSON doesn't complain. Even if this is the intended behavior, shouldn't
there be an option to enforce stricter parsing?
What version of the product are you using? On what operating system?
gson-1.4
Please provide any additional information below.
Original issue reported on code.google.com by mperdik...@gmail.com
on 22 Jan 2010 at 10:13
Attachments:
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 10
- Comments: 33 (1 by maintainers)
I manage to workaround this issue using a class similar to the following:
Do we have this fix as part of the GSON library now ?
Up
Any updates on this issue? It was bumped to “Critical” priority three years ago.
I’m looking for an alternative to gson, this problem should have been fixed in the last 10 years
The
boundFields
field is now in the superclass of the class returned bydelegate.getClass()
. So the code should work with 2.10 if you change that line toWe had to fix a couple of occurrences of the same problem in Google’s internal codebase. I changed the code to this slightly more robust version:
That should work with both pre- and post-2.10 versions.
Although Gson is in maintenance mode, this particular issue seems important enough that we should have some formal support for it instead of requiring people to use reflective hacks like this.
Imagine not resolving a critial-labeled issue in 11 years
Oh right…
Up
@eamonnmcmanus Thank you for your follow-up! I agree it would be preferable to not have to use reflection to get at these fields, and it would be great if there were a more formal mechanism.
Still not fixed??? This seems to be a pattern with google tools, develop something 80% to 90% and then walk away from it 😦