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

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

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 306
  • Comments: 22 (1 by maintainers)

Most upvoted comments

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

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…

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

I would love to see this feature implemented as well.

Does Gson have this feature yet ?

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 cherva...@gmail.com on 10 Dec 2014 at 12:46

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

Original comment by dinisnun...@gmail.com on 28 Aug 2014 at 9:22