mirror of
https://github.com/tristanpenman/valijson.git
synced 2024-12-12 10:13:51 +01:00
Add picojson example
This commit is contained in:
parent
138c3785ef
commit
cd9258c241
@ -237,6 +237,10 @@ if(valijson_BUILD_EXAMPLES)
|
|||||||
examples/json_pointers.cpp
|
examples/json_pointers.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(picojson_format_test
|
||||||
|
examples/picojson_format_test.cpp
|
||||||
|
)
|
||||||
|
|
||||||
add_executable(remote_resolution_local_file
|
add_executable(remote_resolution_local_file
|
||||||
examples/remote_resolution_local_file.cpp
|
examples/remote_resolution_local_file.cpp
|
||||||
)
|
)
|
||||||
|
85
examples/picojson_format_test.cpp
Normal file
85
examples/picojson_format_test.cpp
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#define PICOJSON_USE_INT64
|
||||||
|
#include "picojson.h"
|
||||||
|
|
||||||
|
#include "valijson/adapters/picojson_adapter.hpp"
|
||||||
|
#include "valijson/validation_results.hpp"
|
||||||
|
#include "valijson/schema_parser.hpp"
|
||||||
|
#include "valijson/validator.hpp"
|
||||||
|
|
||||||
|
constexpr auto schemaStr = R"JSON({
|
||||||
|
"additionalItems": false,
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"format": "date-time",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"format": "date-time",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"maxItems": 2,
|
||||||
|
"minItems": 2,
|
||||||
|
"type": "array"
|
||||||
|
})JSON";
|
||||||
|
|
||||||
|
constexpr auto targetStr = R"JSON([
|
||||||
|
["um 12", "um 12"],
|
||||||
|
["2023-07-18T14:46:22Z"],
|
||||||
|
["2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z"]
|
||||||
|
])JSON";
|
||||||
|
|
||||||
|
picojson::value Parse(std::string_view serialized, picojson::value def)
|
||||||
|
{
|
||||||
|
picojson::value v;
|
||||||
|
auto first = serialized.data();
|
||||||
|
auto last = first + serialized.size();
|
||||||
|
auto err = picojson::parse(v, first, last);
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
auto validatorSchema = std::make_shared<valijson::Schema>();
|
||||||
|
{
|
||||||
|
auto schemaJson = Parse(schemaStr, picojson::value{});
|
||||||
|
auto schemaAdapter = valijson::adapters::PicoJsonAdapter(schemaJson);
|
||||||
|
valijson::SchemaParser parser;
|
||||||
|
parser.populateSchema(schemaAdapter, *validatorSchema);
|
||||||
|
std::cout << "Schema:" << std::endl << schemaStr << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto targetJson = Parse(targetStr, picojson::value{});
|
||||||
|
auto targetAdapter = valijson::adapters::PicoJsonAdapter(targetJson);
|
||||||
|
std::cout << "Target:" << std::endl << targetStr << std::endl;
|
||||||
|
|
||||||
|
valijson::ValidationResults results;
|
||||||
|
auto validator = valijson::Validator();
|
||||||
|
[[maybe_unused]] auto isValid = validator.validate(
|
||||||
|
*validatorSchema,
|
||||||
|
targetAdapter,
|
||||||
|
&results);
|
||||||
|
|
||||||
|
std::cout << "Is valid: " << (isValid ? "YES" : "NO") << std::endl;
|
||||||
|
|
||||||
|
valijson::ValidationResults::Error error;
|
||||||
|
unsigned int errorNum = 1;
|
||||||
|
while (results.popError(error)) {
|
||||||
|
std::cerr << "Error #" << errorNum << std::endl;
|
||||||
|
std::cerr << " ";
|
||||||
|
for (const std::string &contextElement : error.context) {
|
||||||
|
std::cerr << contextElement << " ";
|
||||||
|
}
|
||||||
|
std::cerr << std::endl;
|
||||||
|
std::cerr << " - " << error.description << std::endl;
|
||||||
|
++errorNum;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user