(This feature was released in v1.1.0)
JSON Schema is a draft standard for describing the format of JSON data. The schema itself is also JSON data. By validating a JSON structure with JSON Schema, your code can safely access the DOM without manually checking types, or whether a key exists, etc. It can also ensure that the serialized JSON conform to a specified schema.
First of all, you need to parse a JSON Schema into
Document, and then compile the
Document into a
Secondly, construct a
SchemaValidator with the
SchemaDocument. It is similar to a
Writer in the sense of handling SAX events. So, you can use
document.Accept(validator) to validate a document, and then check the validity.
SchemaDocmentcan be referenced by multiple
SchemaValidators. It will not be modified by
SchemaValidatormay be reused to validate multiple documents. To run it for other documents, call
Unlike most JSON Schema validator implementations, RapidJSON provides a SAX-based schema validator. Therefore, you can parse a JSON from a stream while validating it on the fly. If the validator encounters a JSON value that invalidates the supplied schema, the parsing will be terminated immediately. This design is especially useful for parsing large JSON files.
For using DOM in parsing,
Document needs some preparation and finalizing tasks, in addition to receiving SAX events, thus it needs some work to route the reader, validator and the document.
SchemaValidatingReader is a helper class that doing such work.
For using SAX in parsing, it is much simpler. If it only need to validate the JSON without further processing, it is simply:
This is exactly the method used in the schemavalidator example. The distinct advantage is low memory usage, no matter how big the JSON was (the memory usage depends on the complexity of the schema).
If you need to handle the SAX events further, then you need to use the template class
GenericSchemaValidator to set the output handler of the validator:
It is also possible to do validation during serializing. This can ensure the result JSON is valid according to the JSON schema.
Of course, if your application only needs SAX-style serialization, it can simply send SAX events to
SchemaValidator instead of
JSON Schema supports `$ref` keyword, which is a JSON pointer referencing to a local or remote schema. Local pointer is prefixed with
#, while remote pointer is an relative or absolute URI. For example:
SchemaDocument does not know how to resolve such URI, it needs a user-provided
IRemoteSchemaDocumentProvider instance to do so.
RapidJSON passed 262 out of 263 tests in JSON Schema Test Suite (Json Schema draft 4).
The failed test is "changed scope ref invalid" of "change resolution scope" in
refRemote.json. It is due to that
id schema keyword and URI combining function are not implemented.
format schema keyword for string values is ignored, since it is not required by the specification.
The schema keyword
patternProperties uses regular expression to match the required pattern.
RapidJSON implemented a simple NFA regular expression engine, which is used by default. It supports the following syntax.
|Zero or one|
|Zero or more|
|One or more|
|Exactly 3 times|
|At least 3 times|
|3 to 5 times|
|At the beginning|
|At the end|
|Character class range|
|Character class combination|
|Negated character classes|
|Negated character class range|
|Form feed (U+000C)|
|Line feed (U+000A)|
|Carriage return (U+000D)|
|Vertical tab (U+000B)|
For C++11 compiler, it is also possible to use the
std::regex by defining
RAPIDJSON_SCHEMA_USE_STDREGEX=1. If your schemas do not need
patternProperties, you can set both macros to zero to disable this feature, which will reduce some code size.
On a Mac Book Pro (2.8 GHz Intel Core i7), the following results are collected.
|Validator||Relative speed||Number of test runs per second|
|`ajv`||100%||19770 (± 1.31%)|
|`is-my-json-valid`||70%||13835 (± 2.84%)|
|`jsen`||57.7%||11411 (± 1.27%)|
|`schemasaurus`||26%||5145 (± 1.62%)|
|`themis`||19.9%||3935 (± 2.69%)|
|`z-schema`||7%||1388 (± 0.84%)|
|`jsck`||3.1%||606 (± 2.84%)|
|`jsonschema`||0.9%||185 (± 1.01%)|
|`skeemas`||0.8%||154 (± 0.79%)|
|tv4||0.5%||93 (± 0.94%)|
|`jayschema`||0.1%||21 (± 1.14%)|