mirror of
https://github.com/tristanpenman/valijson.git
synced 2024-12-14 02:57:45 +01:00
Merge pull request #107 from baylesj/readme
Minor tweaks to README.md file
This commit is contained in:
commit
84b67fa6fe
30
README.md
30
README.md
@ -15,7 +15,7 @@ The goal of this project is to support validation of all constraints available i
|
|||||||
The following code snippets show how you might implement a simple validator using RapidJson as the underlying JSON Parser.
|
The following code snippets show how you might implement a simple validator using RapidJson as the underlying JSON Parser.
|
||||||
|
|
||||||
Include the necessary headers:
|
Include the necessary headers:
|
||||||
|
```cpp
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
|
|
||||||
#include <valijson/adapters/rapidjson_adapter.hpp>
|
#include <valijson/adapters/rapidjson_adapter.hpp>
|
||||||
@ -23,16 +23,16 @@ Include the necessary headers:
|
|||||||
#include <valijson/schema.hpp>
|
#include <valijson/schema.hpp>
|
||||||
#include <valijson/schema_parser.hpp>
|
#include <valijson/schema_parser.hpp>
|
||||||
#include <valijson/validator.hpp>
|
#include <valijson/validator.hpp>
|
||||||
|
```
|
||||||
These are the classes that we'll be using:
|
These are the classes that we'll be using:
|
||||||
|
```cpp
|
||||||
using valijson::Schema;
|
using valijson::Schema;
|
||||||
using valijson::SchemaParser;
|
using valijson::SchemaParser;
|
||||||
using valijson::Validator;
|
using valijson::Validator;
|
||||||
using valijson::adapters::RapidJsonAdapter;
|
using valijson::adapters::RapidJsonAdapter;
|
||||||
|
```
|
||||||
We are going to use RapidJSON to load the schema and the target document:
|
We are going to use RapidJSON to load the schema and the target document:
|
||||||
|
```cpp
|
||||||
// Load JSON document using RapidJSON with Valijson helper function
|
// Load JSON document using RapidJSON with Valijson helper function
|
||||||
rapidjson::Document mySchemaDoc;
|
rapidjson::Document mySchemaDoc;
|
||||||
if (!valijson::utils::loadDocument("mySchema.json", mySchemaDoc)) {
|
if (!valijson::utils::loadDocument("mySchema.json", mySchemaDoc)) {
|
||||||
@ -44,21 +44,23 @@ We are going to use RapidJSON to load the schema and the target document:
|
|||||||
SchemaParser parser;
|
SchemaParser parser;
|
||||||
RapidJsonAdapter mySchemaAdapter(mySchemaDoc);
|
RapidJsonAdapter mySchemaAdapter(mySchemaDoc);
|
||||||
parser.populateSchema(mySchemaAdapter, mySchema);
|
parser.populateSchema(mySchemaAdapter, mySchema);
|
||||||
|
```
|
||||||
Load a document to validate:
|
Load a document to validate:
|
||||||
|
```cpp
|
||||||
rapidjson::Document myTargetDoc;
|
rapidjson::Document myTargetDoc;
|
||||||
if (!valijson::utils::loadDocument("myTarget.json", myTargetDoc)) {
|
if (!valijson::utils::loadDocument("myTarget.json", myTargetDoc)) {
|
||||||
throw std::runtime_error("Failed to load target document");
|
throw std::runtime_error("Failed to load target document");
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Validate a document:
|
Validate a document:
|
||||||
|
```cpp
|
||||||
Validator validator;
|
Validator validator;
|
||||||
RapidJsonAdapter myTargetAdapter(myTargetDoc);
|
RapidJsonAdapter myTargetAdapter(myTargetDoc);
|
||||||
if (!validator.validate(mySchema, myTargetAdapter, NULL)) {
|
if (!validator.validate(mySchema, myTargetAdapter, NULL)) {
|
||||||
throw std::runtime_error("Validation failed.");
|
throw std::runtime_error("Validation failed.");
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Note that Valijson's `SchemaParser` and `Validator` classes expect you to pass in a `RapidJsonAdapter` rather than a `rapidjson::Document`. This is due to the fact that `SchemaParser` and `Validator` are template classes that can be used with any of the JSON parsers supported by Valijson.
|
Note that Valijson's `SchemaParser` and `Validator` classes expect you to pass in a `RapidJsonAdapter` rather than a `rapidjson::Document`. This is due to the fact that `SchemaParser` and `Validator` are template classes that can be used with any of the JSON parsers supported by Valijson.
|
||||||
|
|
||||||
@ -67,7 +69,7 @@ Note that Valijson's `SchemaParser` and `Validator` classes expect you to pass i
|
|||||||
Valijson has been designed to safely manage, and eventually free, the memory that is allocated while parsing a schema or validating a document. When working with an externally loaded schema (i.e. one that is populated using the `SchemaParser` class) you can rely on RAII semantics.
|
Valijson has been designed to safely manage, and eventually free, the memory that is allocated while parsing a schema or validating a document. When working with an externally loaded schema (i.e. one that is populated using the `SchemaParser` class) you can rely on RAII semantics.
|
||||||
|
|
||||||
Things get more interesting when you build a schema using custom code, as illustrated in the following snippet. This code demonstrates how you would create a schema to verify that the value of a 'description' property (if present) is always a string:
|
Things get more interesting when you build a schema using custom code, as illustrated in the following snippet. This code demonstrates how you would create a schema to verify that the value of a 'description' property (if present) is always a string:
|
||||||
|
```cpp
|
||||||
{
|
{
|
||||||
// Root schema object that manages memory allocated for
|
// Root schema object that manages memory allocated for
|
||||||
// constraints or sub-schemas
|
// constraints or sub-schemas
|
||||||
@ -101,7 +103,7 @@ Things get more interesting when you build a schema using custom code, as illust
|
|||||||
|
|
||||||
// Root schema goes out of scope and all allocated memory is freed
|
// Root schema goes out of scope and all allocated memory is freed
|
||||||
}
|
}
|
||||||
|
```
|
||||||
## JSON References ##
|
## JSON References ##
|
||||||
|
|
||||||
The library includes support for local JSON References. Remote JSON References are supported only when the appropriate callback functions are provided.
|
The library includes support for local JSON References. Remote JSON References are supported only when the appropriate callback functions are provided.
|
||||||
@ -115,7 +117,7 @@ Valijson's' test suite currently contains several hand-crafted tests and uses th
|
|||||||
### cmake ###
|
### cmake ###
|
||||||
|
|
||||||
The examples and test suite can be built using cmake:
|
The examples and test suite can be built using cmake:
|
||||||
|
```bash
|
||||||
# Build examples and test suite
|
# Build examples and test suite
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
@ -124,7 +126,7 @@ The examples and test suite can be built using cmake:
|
|||||||
|
|
||||||
# Run test suite (from build directory)
|
# Run test suite (from build directory)
|
||||||
./test_suite
|
./test_suite
|
||||||
|
```
|
||||||
#### How to add this library to your cmake target ####
|
#### How to add this library to your cmake target ####
|
||||||
|
|
||||||
Download this repository into your project
|
Download this repository into your project
|
||||||
@ -215,12 +217,12 @@ The exceptions to this are boost, Poco and Qt5, which due to their size must be
|
|||||||
When using PicoJSON, it may be necessary to include the `picojson.h` before other headers to ensure that the appropriate macros have been enabled.
|
When using PicoJSON, it may be necessary to include the `picojson.h` before other headers to ensure that the appropriate macros have been enabled.
|
||||||
|
|
||||||
When building Valijson using CMake on Mac OS X, with Qt 5 installed via Homebrew, you may need to set `CMAKE_PREFIX_PATH` so that CMake can find your Qt installation, e.g:
|
When building Valijson using CMake on Mac OS X, with Qt 5 installed via Homebrew, you may need to set `CMAKE_PREFIX_PATH` so that CMake can find your Qt installation, e.g:
|
||||||
|
```bash
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix qt5)
|
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix qt5)
|
||||||
make
|
make
|
||||||
|
```
|
||||||
## License ##
|
## License ##
|
||||||
|
|
||||||
Valijson is licensed under the Simplified BSD License.
|
Valijson is licensed under the Simplified BSD License.
|
||||||
|
Loading…
Reference in New Issue
Block a user