-
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
Support handlers for unknown properties, useful for error handling #188
Comments
Original comment by
|
Original comment by |
Original comment by |
Original comment by
|
Original comment by |
Original comment by |
Original comment by |
Original comment by |
Original comment by
|
Original comment by |
Original comment by |
I submitted pull request #660 for this |
Any updates on this issue? looking forward for the fix... |
Is there a workaround for this issue which we can do until a fix is provided? |
I manage to workaround this issue using a class similar to the following:
|
Thanks @ikus060. I was able to adapt your approach so I can log error messages on unrecognized properties, rather than silently ignoring them as is done by default. |
The solution is a bit ugly, but it work... |
Do we have this fix as part of the GSON library now ? |
Nice work-around but I will have to adapt it to be compatible with my IdExclusionStrategy that tells gson to ignore the id field. Gson now throws errors wherever it sees the id in JSON. My intention is to ignore ids on serialization, and by design it would be compatible because the TypeAdaptor for this example is only implemented for gets / deserialization. However ExclusionStrategies are deserialization/serialization-agnostic - work on both. EDIT: ok, I have adapted the work-around for us with ExclusionStrategies by building two Gson objects, one for serializing, one for deserializing. |
Any updates on this issue? It was bumped to "Critical" priority three years ago. |
Up |
1 similar comment
Up |
wow, this is astounding... definitely moving away from gson. why is this basic feature not present? |
I'm looking for an alternative to gson, this problem should have been fixed in the last 10 years |
Still not fixed???? |
up |
Imagine not resolving a critial-labeled issue in 11 years
Oh right... |
This is why I've been moving away from google libraries. They have a solid
history of abandoning projects and ignoring issues with them.
…On Sat, May 15, 2021 at 10:11 AM Luca ***@***.***> wrote:
Imagine not resolving a critial-labeled issue in 11 years
Original issue reported on code.google.com by ***@***.*** on 22
Jan 2010 at 10:13
- Added labels: *Priority-Critical*
Oh right...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#188 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABZTL5SKWU4YBZHFLESM2S3TNZ6HPANCNFSM4BI4KC7Q>
.
|
This would be especially great if you are parsing a huge chunk of JSON and you are unsure if you have defined all fields. |
I will check the adapter to see if I can make it work, really strange this has been around for so long |
Is anyone else here leveraging @ikus060 's example and has upgraded to Gson 2.10. It seems that:
now throws a |
The Field f = delegate.getClass().getSuperclass().getDeclaredField("boundFields"); We 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: Field f = findField(delegateAdapter.getClass(), "boundFields");
...
private static Field findField(Class<?> startingClass, String fieldName)
throws NoSuchFieldException {
for (Class<?> c = startingClass; c != null; c = c.getSuperclass()) {
try {
return c.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
// OK: continue with superclasses
}
}
throw new NoSuchFieldException(fieldName + " starting from " + startingClass.getName());
} 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. |
@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. |
up |
Original issue reported on code.google.com by
[email protected]
on 22 Jan 2010 at 10:13Attachments:
The text was updated successfully, but these errors were encountered: