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.
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.
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.
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.
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:
Other JSON Schema Validator projects can be found on github or bitbucket, among them:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With