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

Provide an @Required annotation to indicate that a field must be present #61

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

Comments

@GoogleCodeExporter
Copy link

See discussion at http://groups.google.com/group/google-
gson/browse_thread/thread/6cfdcc56742357da

During deserialization, Gson should throw a JsonParseException if it finds 
that a field marked with @Required is not present. 

During serialization, Gson should always write out a field marked @Required 
even if it is null. 

Original issue reported on code.google.com by inder123 on 17 Oct 2008 at 10:31

@GoogleCodeExporter
Copy link
Author

Original comment by inder123 on 27 Mar 2009 at 7:36

  • Added labels: Milestone-Release1.4

@GoogleCodeExporter
Copy link
Author

deferred to a future release

Original comment by inder123 on 29 Sep 2009 at 9:12

  • Added labels: Milestone-Release1.5
  • Removed labels: Milestone-Release1.4

@GoogleCodeExporter
Copy link
Author

It seems that @Required makes only sense in combination with !SerializeNulls.
On the other hand, @Optional would make sense in combination with SerializeNull.

Original comment by [email protected] on 18 Sep 2010 at 8:36

@GoogleCodeExporter
Copy link
Author

Original comment by inder123 on 1 Nov 2010 at 10:30

  • Removed labels: Milestone-Release1.5

@GoogleCodeExporter
Copy link
Author

Original comment by inder123 on 3 Nov 2010 at 12:27

  • Added labels: Milestone-Undefined

@GoogleCodeExporter
Copy link
Author

http://code.google.com/p/google-gson/source/detail?r=1210 provides an 
experimental way to achieve this.

Original comment by inder123 on 11 Oct 2012 at 6:51

  • Changed state: Started

@GoogleCodeExporter
Copy link
Author

Are there any current best practices for working around this? With the current 
implementation, I never have confidence that my deserialized objects have 
proper values and end up having to null-check all of my object's data before 
using it.

Suggestions like http://stackoverflow.com/a/14245807/128948 look promising, but 
trying to see what the other options are.

Original comment by [email protected] on 11 Feb 2014 at 10:18

@GoogleCodeExporter
Copy link
Author

I'm also looking for a way to throw an Exception if a mandatory field is not in 
the Json String. 

Original comment by [email protected] on 28 Aug 2014 at 9:22

@GoogleCodeExporter
Copy link
Author

Count me as +1. Catching NPE much later in some distant unrelated places is 
quite ugly. I have to build my custom workarounds for this, but it would be so 
much better to have proper support provided by the lib.

Original comment by [email protected] on 10 Dec 2014 at 12:46

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