7

Qt support for XML is very strong, up to and including support for XML schema validation.

Qt Support for JSON appears to be less extensive. Nothing I can find seems to confirm or deny support for json-schema or any other kind of Json schema validation in Qt.

Is there a sound way to validate json in Qt / C++?

Edit: to be clear, this question is centered on Json schema validation, not just confirming if an arbitrary document is valid Json.

3
  • related: stackoverflow.com/q/4676171/1764 check out my answer about valijson: stackoverflow.com/a/35101282/1764
    – ansgri
    Jan 30, 2016 at 11:38
  • "Qt support for XML is very strong, up to and including support for XML schema validation" Starting with Qt6 this is not true anymore. The entire qtxmlpatterns component was dropped. Dec 27, 2021 at 1:36
  • Just realized that qtxml module also is kind of deprecated. Qt 6.2 documentation says "Qt XML will no longer receive additional features" which effectively deprecates that module as well. They recommend XML stream classes which are a nightmare to use in safe way, especially without schema validator. Dec 27, 2021 at 1:51

4 Answers 4

5

Qt 5.8 still don't seem to have JSON Schema validation... But you can find a bunch of other interesting libraries.

4 C/C++ libraries are listed on JSON Schema website:

  • wjelement mentionned by @Boris is optimized for performance and is used in email production environment by Messaging Architects. It is a C library but a C++ wrapper is also available (wjelement-cpp).
  • valijson is a header-only Schema Validator that may be use with other JSON parser.
  • json-schema-validator is based on nlohmann's modern c++ JSON parser which have nice features as a good interaction with STL containers. But this validator seems less mature.

Other JSON Schema Validator projects can be found on github or bitbucket, among them:

  • jv_json devoted to embedded applications.
  • libvariant that can also handle YAML and PLIST formats.
3

Indeed, it seems there are no Qt support for JSON Schema validation, even in Qt 5.1. Since writing your own would be very time-consuming, I would suggest:

  • If you as developer would have provided the Schema, then do not validate by using a JSON Schema validation, but instead perform a hard-coded validation of your parsed JSON (i.e., manually check that the required fields are present, are of the correct type, and are within the specified bounds)

  • If handling external JSON Schema is necessary (i.e., the Schema is not known in advance, possibly user-defined), then do not use Qt but an independent C/C++ JSON validator, like WJElement (the one linked on the json-schema website)

4
  • This is weird that they don't have a JSON validation since QML is more or less a JSON dialect... Sep 5, 2013 at 8:54
  • @Jean Note that Qt does have JSON validation, even with nice explanation of which parsing error was encountered. What it doesn't has is validation against a JSON Schema, which is something much more involved than just parsing errors. Sep 5, 2013 at 9:12
  • @Boris, looks these are my options. I come from an XML background so I'm more accustomed to thinking that way. Seems like schema validation is not one of JSON's strengths. json-shcema itself seems to be someway stalled out with its latest draft standard having exipred this month (json-schema.org/latest/json-schema-validation.html).
    – ford
    Sep 5, 2013 at 21:37
  • 1
    @ford To me the strength of JSON, in the same spirit as JavaScript in general, is to be very flexible with loose types, so you can do very "sloppy" programming (not in a pejorative sense) which makes it efficient to program. This really contrast with C++, where you want it efficient to execute. So schema validation does not really fit in this spirit, and I guess is not used a lot. Sep 5, 2013 at 22:13
0

It exists http://qjson.sourceforge.net/ which does not include schema validation either.

It also exist Qtjsonstream in qtplayground, which includes some schema validation, but I haven't use it so I don't know anything else about it.

-2

I'll suggest you to yse qt-json it has simple vlidation like valid or not.

bool ok;
QtJson::JsonObject result = QtJson::parse(json, ok).toMap();
1
  • 1
    Qt 5.x also have this, and even better. The OP is not interested if the JSON has a valid syntax, but if it validates upon a given JSON Schema, which is something completely different. See the link provided by the OP to see what a JSON Schema is. Sep 5, 2013 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.