Gson: Provide an @Required annotation to indicate that a field must be present

Created on 19 Mar 2015  路  16Comments  路  Source: google/gson

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

Milestone-Undefined Priority-Medium Type-Enhancement auto-migrated

Most helpful comment

Please use this thumbs up button to show your support. Every time someone replies with +1 we _all_ get a (useless) email.
screen shot 2016-07-28 at 12 03 59 pm

All 16 comments

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

  • Added labels: Milestone-Release1.4
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
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

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

  • Removed labels: Milestone-Release1.5

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

  • Added labels: Milestone-Undefined
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
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

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

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

Does Gson have this feature yet ?

I am able to achieve this using this approach. This is for serialization (fail Java to Json if some @required attribute is null)
http://stackoverflow.com/questions/35587252/java-to-json-validation-using-gson/

Please use this thumbs up button to show your support. Every time someone replies with +1 we _all_ get a (useless) email.
screen shot 2016-07-28 at 12 03 59 pm

Seems that no one cares this library. This issue should be marked as critical.

There's a library that's can help with this:

https://github.com/uber-common/rave

Still waiting for this feature... I want my Gson deserializing to throw an exception when a JSON doesnt have a parameter which my object do have and is either marked as @Required or not marked as @Optional. This is a big dealbreaker that Gson doesn't have this feature...

I would love to see this feature implemented as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kramer picture kramer  路  26Comments

kdehairy picture kdehairy  路  43Comments

RobMans426 picture RobMans426  路  20Comments

JakeWharton picture JakeWharton  路  39Comments

GoogleCodeExporter picture GoogleCodeExporter  路  25Comments