Merge branch 'v1.1' of github.com:MountCloud/FireDog into v1.1

This commit is contained in:
MountCloud
2021-09-25 21:15:27 +08:00
+93 -2
View File
@@ -3,6 +3,7 @@
#include <iostream>
#include <string>
#include <iomanip>
#include "firedog.h"
#include "featurelibrary.h"
@@ -11,9 +12,13 @@
#include "stringutil.h"
#include "json/json-schema/json-schema.hpp"
using namespace std;
using namespace firedog;
using nlohmann::json;
using nlohmann::json_schema::json_validator;
void testOutPut() {
string hexstr = "hello";
@@ -53,13 +58,15 @@ void testMatcher() {
}
)";
const char* featureLibraryJson_error = "";
//bytes
const string bytes = "fire dog hello world.";
int ecode = NO_ERROR;
//step1: init feature library
FeatureLibrary featureLibrary = FeatureLibrary::createByJson(featureLibraryJson, &ecode);
FeatureLibrary featureLibrary = FeatureLibrary::createByJson(featureLibraryJson_error, &ecode);
if (ecode != NO_ERROR) {
cout << "feature library load faild";
@@ -126,9 +133,93 @@ void converter() {
hexData = NULL;
}
void testJsonSchema() {
const string person_schema_json = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "A person",
"properties": {
"name": {
"description": "Name",
"type": "string"
},
"age": {
"description": "Age of the person",
"type": "number",
"minimum": 2,
"maximum": 200
}
},
"required": [
"name",
"age"
],
"type": "object"
}
)";
json person_schema = json::parse(person_schema_json);
string schema = person_schema["$schema"];
json_validator validator; // create validator
try {
validator.set_root_schema(person_schema); // insert root-schema
}
catch (const std::exception& e) {
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
return;
}
json bad_person = { {"age", 42} };
json good_person = { {"name", "Albert"}, {"age", 42} };
/* json-parse the people - API of 1.0.0, default throwing error handler */
for (auto& person : { bad_person, good_person }) {
std::cout << "About to validate this person:\n"
<< std::setw(2) << person << std::endl;
try {
validator.validate(person); // validate the document - uses the default throwing error-handler
std::cout << "Validation succeeded\n";
}
catch (const std::exception& e) {
std::cerr << "Validation failed, here is why: " << e.what() << "\n";
}
}
/* json-parse the people - with custom error handler */
class custom_error_handler : public nlohmann::json_schema::basic_error_handler
{
void error(const nlohmann::json_pointer<nlohmann::basic_json<>>& pointer, const json& instance,
const std::string& message) override
{
nlohmann::json_schema::basic_error_handler::error(pointer, instance, message);
std::cerr << "ERROR: '" << pointer << "' - '" << instance << "': " << message << "\n";
}
};
for (auto& person : { bad_person, good_person }) {
std::cout << "About to validate this person:\n"
<< std::setw(2) << person << std::endl;
custom_error_handler err;
validator.validate(person, err); // validate the document
if (err)
std::cerr << "Validation failed\n";
else
std::cout << "Validation succeeded\n";
}
}
int main()
{
//testMatcher();
converter();
//converter();
testJsonSchema();
return 0;
}