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 Mar 19, 2015 · 22 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

@rahul-ramanujam
Copy link

Does Gson have this feature yet ?

@preetamp
Copy link

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/

@tir38
Copy link

tir38 commented Jul 28, 2016

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

@nzackoya
Copy link

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

@naturalwarren
Copy link
Contributor

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

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

@arbitur
Copy link

arbitur commented Feb 22, 2019

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...

@Darkhax
Copy link

Darkhax commented Feb 28, 2020

I would love to see this feature implemented as well.

@charlesganza
Copy link

To anyone still frustrated with the lack of required fields option in Gson, you can use Rave. It solves this problem nicely if you want to avoid NPEs from unreliable APIs.

@michele2021
Copy link

is this still not available??

@viltit
Copy link

viltit commented Dec 16, 2021

seems not. Really annoying.

@grishka
Copy link

grishka commented Dec 19, 2021

Well, you can use my fork if you really need this. They seemingly aren't interested in merging that pull request.

@paulorb
Copy link

paulorb commented Apr 7, 2023

Any news on this?

@Marcono1234
Copy link
Collaborator

Related to #1005; if there was a general "missing field strategy" you could at least easily implement a @Required annotation yourself.

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