mirror of
https://github.com/tristanpenman/valijson.git
synced 2024-12-12 18:20:27 +01:00
Remove unnecessary indentation from code snippets in readme
This commit is contained in:
parent
7d4ea9088d
commit
f97013928d
108
README.md
108
README.md
@ -14,48 +14,48 @@ The following code snippets show how you might implement a simple validator usin
|
|||||||
|
|
||||||
Include the necessary headers:
|
Include the necessary headers:
|
||||||
```cpp
|
```cpp
|
||||||
#include <valijson/adapters/rapidjson_adapter.hpp>
|
#include <valijson/adapters/rapidjson_adapter.hpp>
|
||||||
#include <valijson/utils/rapidjson_utils.hpp>
|
#include <valijson/utils/rapidjson_utils.hpp>
|
||||||
#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
|
```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
|
```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)) {
|
||||||
throw std::runtime_error("Failed to load schema document");
|
throw std::runtime_error("Failed to load schema document");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse JSON schema content using valijson
|
// Parse JSON schema content using valijson
|
||||||
Schema mySchema;
|
Schema mySchema;
|
||||||
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
|
```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
|
```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.
|
||||||
@ -89,7 +89,7 @@ Valijson has been designed to safely manage, and eventually free, the memory tha
|
|||||||
|
|
||||||
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
|
```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
|
||||||
Schema schema;
|
Schema schema;
|
||||||
@ -121,7 +121,7 @@ Things get more interesting when you build a schema using custom code, as illust
|
|||||||
schema.addConstraint(propertiesConstraint);
|
schema.addConstraint(propertiesConstraint);
|
||||||
|
|
||||||
// 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 ##
|
||||||
|
|
||||||
@ -137,14 +137,14 @@ Valijson's' test suite currently contains several hand-crafted tests and uses th
|
|||||||
|
|
||||||
The examples and test suite can be built using cmake:
|
The examples and test suite can be built using cmake:
|
||||||
```bash
|
```bash
|
||||||
# Build examples and test suite
|
# Build examples and test suite
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -Dvalijson_BUILD_TESTS=ON -Dvalijson_BUILD_EXAMPLES=ON
|
cmake .. -Dvalijson_BUILD_TESTS=ON -Dvalijson_BUILD_EXAMPLES=ON
|
||||||
make
|
make
|
||||||
|
|
||||||
# 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 ##
|
||||||
|
|
||||||
@ -179,19 +179,19 @@ target_link_libraries(your-executable ValiJSON::valijson)
|
|||||||
|
|
||||||
It is possible to install headers by running cmake's install command from the build tree. Once Valijson is installed, use it from other CMake projects using `find_package(Valijson)` in your CMakeLists.txt.
|
It is possible to install headers by running cmake's install command from the build tree. Once Valijson is installed, use it from other CMake projects using `find_package(Valijson)` in your CMakeLists.txt.
|
||||||
```bash
|
```bash
|
||||||
# Install Valijson
|
# Install Valijson
|
||||||
git clone --depth=1 git@github.com:tristanpenman/valijson.git
|
git clone --depth=1 git@github.com:tristanpenman/valijson.git
|
||||||
cd valijson
|
cd valijson
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake ..
|
cmake ..
|
||||||
cmake --install .
|
cmake --install .
|
||||||
```
|
```
|
||||||
```cmake
|
```cmake
|
||||||
# Import installed valijson and link it to your executable
|
# Import installed valijson and link it to your executable
|
||||||
find_package(valijson REQUIRED)
|
find_package(valijson REQUIRED)
|
||||||
add_executable(executable main.cpp)
|
add_executable(executable main.cpp)
|
||||||
target_link_libraries(executable valijson)
|
target_link_libraries(executable valijson)
|
||||||
```
|
```
|
||||||
### Xcode ###
|
### Xcode ###
|
||||||
|
|
||||||
@ -220,11 +220,11 @@ Support for JSON References is in development. It is mostly working, however som
|
|||||||
An example application based on Qt is also included under [inspector](./inspector). It can be used to experiment with JSON Schemas and target documents. JSON Inspector is a self-contained CMake project, so it must be built separately:
|
An example application based on Qt is also included under [inspector](./inspector). It can be used to experiment with JSON Schemas and target documents. JSON Inspector is a self-contained CMake project, so it must be built separately:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd inspector
|
cd inspector
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake ..
|
cmake ..
|
||||||
make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
Schemas and target documents can be loaded from file or entered manually. Content is parsed dynamically, so you get rapid feedback.
|
Schemas and target documents can be loaded from file or entered manually. Content is parsed dynamically, so you get rapid feedback.
|
||||||
@ -287,10 +287,10 @@ When using PicoJSON, it may be necessary to include the `picojson.h` before othe
|
|||||||
|
|
||||||
When building Valijson using CMake on macOS, 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 macOS, 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
|
```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 ##
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user