discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

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

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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 commented 9 years ago

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

GoogleCodeExporter commented 9 years ago
deferred to a future release

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

GoogleCodeExporter commented 9 years ago
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 philipp....@gmail.com on 18 Sep 2010 at 8:36

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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 Matthew....@gmail.com on 11 Feb 2014 at 10:18

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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