Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What support is there for json schema in Qt / C++ / C

Tags:

json

schema

qt

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.

like image 945
ford Avatar asked Sep 04 '13 22:09

ford


People also ask

Does Qt support JSON?

Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.

What can you do with JSON Schema?

JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data.

What is required in JSON Schema?

In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”. In Python, "objects" are analogous to the dict type. An important difference, however, is that while Python dictionaries may use anything hashable as a key, in JSON all the keys must be strings.


2 Answers

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.
like image 159
jvtrudel Avatar answered Sep 27 '22 17:09

jvtrudel


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)

like image 31
Boris Dalstein Avatar answered Sep 27 '22 18:09

Boris Dalstein