Skip to content
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

Open
GoogleCodeExporter opened this issue Apr 28, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 22 Jan 2010 at 10:13

Attachments:

@GoogleCodeExporter
Copy link
Author

I think a reasonable fix for this would be to permit the user to register a 
handler to be notified whenever an unknown property is encountered. Perhaps 
this:
  interface UnknownFieldHandler {
    void handle(JsonObject object, String name, JsonElement value);
  }

Then we could build in a few implementations: one that ignores unknown fields, 
one that logs unknown fields, and one that throws on unknown fields. Users 
could configure their GSON to be as strict or forgiving as necessary.

Original comment by limpbizkit on 3 Nov 2010 at 4:53

  • Changed title: Support handlers for unknown properties, useful for error handling
  • Added labels: Milestone-Release1.7, Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

Issue 262 has been merged into this issue.

Original comment by limpbizkit on 30 Dec 2011 at 6:38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant