Integrate json-schema-validator with Spring and Jackson

3,343 views
Skip to first unread message

Aravind

unread,
Jun 11, 2013, 2:00:38 PM6/11/13
to json-schem...@googlegroups.com
Good morning.
Do I find any working examples for integrating json-schema-validator with Spring and Jackson?
I am using Spring org.springframework.http.converter.json.MappingJacksonHttpMessageConverter to serialize the JSON payload to Java object.
I would like to have some validations kicking in during the process.
Example: 
{
  "name" : "foo",
  "name" : "bar",
  "city" : "some city"
}

In the above payload "name" is duplicated. I like the validator to kick in to reject the request.
Right now what I see is that Jackson simply ignores duplicates and puts "name" as "bar" in the class that is bound to the JSON payload.

Thanks,

Francis Galiegue

unread,
Jun 11, 2013, 2:19:17 PM6/11/13
to Aravind, json-schem...@googlegroups.com
On Tue, Jun 11, 2013 at 8:00 PM, Aravind <aravind...@gmail.com> wrote:
> Good morning.
> Do I find any working examples for integrating json-schema-validator with
> Spring and Jackson?

I do not quite understand what you mean by "integrating with Jackson"
here? Jackson is a core component of this library... Can you
elaborate?

As to Spring, there is no proper integration per se, but you can
certainly use json-schema-core's processor capabilities to plug your
original inputs into Spring eventually. As I do not use Spring, I
cannot say that I have explored this path, however...

> I am using Spring
> org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
> to serialize the JSON payload to Java object.
> I would like to have some validations kicking in during the process.
> Example:
> {
> "name" : "foo",
> "name" : "bar",
> "city" : "some city"
> }
>
> In the above payload "name" is duplicated. I like the validator to kick in
> to reject the request.
> Right now what I see is that Jackson simply ignores duplicates and puts
> "name" as "bar" in the class that is bound to the JSON payload.
>

Ah. Duplicated keys in JSON data itself. Quite the scenario...

The problem at this stage is that I use Jackson's parsing as is; and
in this case, Jackson complies to the RFC, and the RFC does not say
much. It only says that there SHOULD NOT be duplicate keys in JSON
objects; it does not specify at all how a parser should behave when a
duplicate key is found...

Jackson has, very recently, added an option to its parser to reject
duplicate keys; I am quite inclined to upgrade the library set
requirement to at least a version with this option available, but what
about backwards compatibility?

It is quite some ordeal to add such a constraint, and I fully agree
that it should be there from the start. If I may ask, what version of
-validator/-core you using?

Regards,
--
Francis Galiegue, fgal...@gmail.com
JSON Schema in Java: http://json-schema-validator.herokuapp.com

Aravind

unread,
Jun 11, 2013, 2:35:03 PM6/11/13
to json-schem...@googlegroups.com
Hi, Francis,
I am using Jackson 1.9.6 and for Spring I am using 3.1.0-RELEASE

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.6</version>
</dependency>

To be honest, I haven't even tried the json-schema-validator library.
I was googling for JSON Java validator and ran into the json-schema-validator github page.

Before I wanted to give it a shot, I thought of asking this question first and you promptly responded.

You mentioned that "Jackson has, very recently, added an option to its parser to reject 
duplicate keys; I am quite inclined to upgrade the library set 
requirement to at least a version with this option available". 

Do you know the version of Jackson that supports this feature?
I would like to try using the latest version of Jackson with the latest json-schema-validator sometime this week.

By the way, do you have any samples for integrating json-schema-validator with Spring and Jackson?

Thanks for your time!

Aravind

unread,
Jun 11, 2013, 2:36:37 PM6/11/13
to json-schem...@googlegroups.com, Aravind
I think you already mentioned about Spring integration. Never mind.

Aravind

unread,
Jun 11, 2013, 3:15:19 PM6/11/13
to json-schem...@googlegroups.com, Aravind
I just found this: https://github.com/FasterXML/jackson-databind/commit/3955abb9198e2fddad65c157610e62e301a0c4a7

This is something I am looking for. If you scroll down to the page, you see a test case where the test JSON payload having duplicate keys is validated using:
DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY

Francis Galiegue

unread,
Jun 11, 2013, 3:18:58 PM6/11/13
to Aravind, json-schem...@googlegroups.com
Hello,

On Tue, Jun 11, 2013 at 8:35 PM, Aravind <aravind...@gmail.com> wrote:
> Hi, Francis,
> I am using Jackson 1.9.6 and for Spring I am using 3.1.0-RELEASE
>

Uh. I don't know for Spring, but as far as Jackson is concerned, on a
computing scale history, 1.9.6 is somewhere between Renaissance and
the Secession war...

> <dependency>
> <groupId>org.codehaus.jackson</groupId>
> <artifactId>jackson-mapper-asl</artifactId>
> <version>1.9.6</version>
> </dependency>
>

Try and update to:

<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.1</version>

as a dependency; do things still work for you?

Seriously. 1.9.x has been obsolete for a long time.

> To be honest, I haven't even tried the json-schema-validator library.
> I was googling for JSON Java validator and ran into the
> json-schema-validator github page.
>
> Before I wanted to give it a shot, I thought of asking this question first
> and you promptly responded.
>

OK, some background on what JSON Schema is.

JSON Schema is several things; one of its core component is JSON value
validation, for whatever value (any JSON primitive type, including
null; strings, numbers, booleans, arrays, objects). The
json-schema-validator library can validate any JSON value given a
valid schema. The json-schema-core library has two things:

* it embeds all things necessary for JSON Schema _syntax_ validation,
which is why json-schema-validator depends on it;
* it has a processor system which allows you to chain together
anything you can think of (for instance, json value --> validated json
value against a schema --> POJO).

> You mentioned that "Jackson has, very recently, added an option to its
> parser to reject
> duplicate keys; I am quite inclined to upgrade the library set
> requirement to at least a version with this option available".
>

Jackson's ability to barf on duplicate keys is quite recent; it is not
officially released yet...

> Do you know the version of Jackson that supports this feature?

See right above.

> I would like to try using the latest version of Jackson with the latest
> json-schema-validator sometime this week.
>

PLEASE NOTE: as stated in the README for both projects, when the
middle number is even, this is still a beta version; while it is
functional (I ensure it is so, I am not the kind to leeway on
testing), the API is not definitive yet; if you want a stable API, go
with 2.0.x of -validator (with 1.0.x of -core). I am a few weeks away
from making this 2.2.0/1.2.0, but not there yet...

> By the way, do you have any samples for integrating json-schema-validator
> with Spring and Jackson?
>

With Spring, I have nothing at the moment; but to be really honest, I
don't really know what you actually _expect_ to do with Spring ;) As
to Jackson, I use it daily, but maybe not the way you think (I mostly
use its tree model).

All in all, I am interested in your use case. Can you elaborate on
your usage scenario?

To conclude, I encourage you to have a look at the site in my
signature; see the "About this site" page. You'll find links to two
interesting projects there: JJSchema (generate a JSON Schema from a
POJO) and jsonschema2pojo (the reverse, as its name says).

I intend to plunge deeper into these projects when I get time; right
now however, I am side tracked by two other projects ;) Self
promotion:

https://github.com/fge/msg-simple
https://github.com/fge/uri-template

Have fun,

Francis Galiegue

unread,
Jun 11, 2013, 3:23:30 PM6/11/13
to Aravind, json-schem...@googlegroups.com
On Tue, Jun 11, 2013 at 9:15 PM, Aravind <aravind...@gmail.com> wrote:
> I just found this:
> https://github.com/FasterXML/jackson-databind/commit/3955abb9198e2fddad65c157610e62e301a0c4a7
>
> This is something I am looking for. If you scroll down to the page, you see
> a test case where the test JSON payload having duplicate keys is validated
> using:
> DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY
>

I know ;) I am looking forward for this commit to be upstream...
Reply all
Reply to author
Forward
0 new messages