google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.38k stars 4.29k forks source link

Validate JSon again Json Schema file #783

Open sebawagner opened 8 years ago

sebawagner commented 8 years ago

It is quite important that you can quickly validate that a given Json string complies with a required JSON schema. You would not want to built your own custom for-each loop and check that each attribute is there or not.

If you for example build an API or similar these days you can use Rest specifications like RAML. Doing so has a lot of advantages, for example code generation, easy to understand for 3rd parties or self-explaining with built in examples. However it all relies on being able to quickly validate a Json string against a schema definition.

Unfortunately there is no good Java Json Schema validator. Neither in Gson nor in any 3rd party library. It would be really useful if this is part of the Gson framework.

sebawagner commented 8 years ago

Btw I really like this lib, since when it started as a GoogleCode project many moons ago.

inder123 commented 8 years ago

Thanks for the input. Would love to accept any contributions in this regard.

abatista75 commented 7 years ago

I spent a lot of time testing several json parsers and validators, but none of them really satisfied a simple need: a fast json parser with a json schema validator to avoid several file parsing (json use + json validation), but also to make it easier for the developers by avoiding the use of several libraries (which involves validation, maintenance, learning curve...).

SamuSoft commented 2 years ago

This is a feature which I'd really like in GSON, but this issue is quite old. Me and a team of 4 other programmers would like to take a look at this, and hopefully have a PR ready in 2-3 weeks. Would it ok for us to do a take on it?

Marcono1234 commented 2 years ago

hopefully have a PR ready in 2-3 weeks. Would it ok for us to do a take on it?

Gson is currently in maintenance mode (unfortunately this is not mentioned in the README yet, but it is mentioned in this issue template). It would therefore be good to get some feedback from the maintainers (@eamonnmcmanus) before you start with this, to avoid 'wasting' your effort with this.

Maybe it would be useful to implement this in a generic way to support marking fields as required in general (a feature which Gson is currently missing), which would allow you to build a schema validator on top of it, but would allow other users to build annotation based required property support. Possibly something like RequiredFieldPredicate with boolean isRequired(Class<?> runtimeClass, Field f).

The main question here might also be whether this is about merely validating JSON data against a schema, or parsing it as well. Because for parsing I assume it would be easier to use annotations on the model class to express constraints, instead of having to maintain a separate schema file and keep these two in sync. Would be good if you could describe your usecase a bit.

There is also the project https://github.com/joelittlejohn/jsonschema2pojo which supports (basic?) code generation for Gson based on a JSON schema. I assume besides the missing / duplicate property detection not supported by Gson, a lot could be achieved by using Gson's @JsonAdapter on the fields (and classes?) and using custom type adapters / type adapter factories which perform the validation. Those type adapters / type adapter factories could be for example provided through a separate library from the code generation project, which is included by users at compile time and runtime. The question would be then however, how efficient the usage of @JsonAdapter with all the wrapping of the default adapters is.


Note that I am not a member of this project.