json files validation with jansson

380 views
Skip to first unread message

Christophe Paris

unread,
Nov 19, 2011, 2:58:53 PM11/19/11
to jansso...@googlegroups.com
Hi,

I'm using json in my configuration file and I'm trying to do some validation when my application start up.
The new "unpack" feature looked like a good candidate to have a nice error report with the line number, but I've found a few caveats.
In my file some elements are optional, unfortunately this seems out of the scope of "unpack", so I first do a validation with mandatory elements only.
Then I validate optional elements by hand or with some smaller "unpack" validation.
The problem is that the error reported by unpack is related to the element and not to the root of the document as I would have expected.

Any ideas on how to do this in a better way ?

Regards,
Christophe.


Petri Lehtinen

unread,
Nov 20, 2011, 6:11:51 AM11/20/11
to jansso...@googlegroups.com
Christophe Paris wrote:
> The problem is that the error reported by unpack is related to the element and
> not to the root of the document as I would have expected.

I don't understand. What did you get and what did you expect?

> Any ideas on how to do this in a better way ?

json_unpack() is too limited for complex validating. JSON Schema is
better, but I'm afraid there are no C libraries for it available.

Petri

Christophe Paris

unread,
Nov 20, 2011, 3:42:06 PM11/20/11
to jansso...@googlegroups.com
On Sunday, November 20, 2011 12:11:51 PM UTC+1, Petri Lehtinen wrote:
Christophe Paris wrote:
> The problem is that the error reported by unpack is related to the element and
> not to the root of the document as I would have expected.

I don't understand. What did you get and what did you expect?

OK let's take this simple example file :
{
  "mandatory": "foo",
  "optional": {
    "mandatoryInOptional": 123
  }
}

So first I do a validation of mandatory elements with js_unpack()
In this example there is only "mandatory" so I will use :
js_unpack_ex(rootNode, &error, "{s:s} ", JSON_VALIDATE_ONLY, "mandatory")

Then I get by hand the "optional" node and I use js_unpack() on it to validate mandatory elements in it :
js_unpack_ex(optionalNode, &error, "{s:d} ", JSON_VALIDATE_ONLY, "mandatoryInOptional")

And at this point if there is a validation error, for example I put a string instead of an integer, the error will refer to line 1 of optionalNode.
This is true but I expected the error would be relative to the root node of the file so that would be line 4.

 

> Any ideas on how to do this in a better way ?

json_unpack() is too limited for complex validating. JSON Schema is
better, but I'm afraid there are no C libraries for it available.



Yes, it's a shame ;-)

Thanks,
Christophe

Petri Lehtinen

unread,
Nov 21, 2011, 2:08:46 AM11/21/11
to jansso...@googlegroups.com
Christophe Paris wrote:
> OK let's take this simple example file :
> {
>   "mandatory": "foo",
>   "optional": {
>     "mandatoryInOptional": 123
>   }
> }
>
> So first I do a validation of mandatory elements with js_unpack()
> In this example there is only "mandatory" so I will use :
> js_unpack_ex(rootNode, &error, "{s:s} ", JSON_VALIDATE_ONLY, "mandatory")

>
> Then I get by hand the "optional" node and I use js_unpack() on it to validate
> mandatory elements in it :
> js_unpack_ex(optionalNode, &error, "{s:d} ", JSON_VALIDATE_ONLY,

> "mandatoryInOptional")
>
> And at this point if there is a validation error, for example I put a string
> instead of an integer, the error will refer to line 1 of optionalNode.
> This is true but I expected the error would be relative to the root node of the
> file so that would be line 4.

The line and column error information refer to the format string, not
to the JSON value. In this case, the format string is "{s:d}". The
line number will almost always be 1, unless your format string
contains \n characters. There are no line numbers or anything like
that associated with a JSON value after it has been successfully
parsed by json_load() or a friend.

Petri

Reply all
Reply to author
Forward
0 new messages