Created
March 24, 2015 19:02
-
-
Save lexicalunit/a64eea66c2c56b297329 to your computer and use it in GitHub Desktop.
Use libvariant to validate JSON schema against JSON data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// c++ -I/usr/local/include -L/usr/local/lib -lVariant schema_validate.cpp | |
#include <cstdio> | |
#include <iostream> | |
#include <iomanip> | |
#include <algorithm> | |
#include <fstream> | |
#include <sstream> | |
#include <iterator> | |
#include <memory> | |
#include <libgen.h> | |
#include <Variant/Schema.h> | |
#include <Variant/Variant.h> | |
#include <Variant/Parser.h> | |
#include <Variant/ParserInput.h> | |
using namespace libvariant; | |
using namespace std; | |
void dump(const string& filename, ostream& out) { | |
ifstream in(filename.c_str()); | |
in.unsetf(ios::skipws); | |
copy(istream_iterator<char>(in) | |
, istream_iterator<char>() | |
, ostream_iterator<char>(out)); | |
out << endl; | |
} | |
int main(int argc, const char * argv[]) { | |
if(argc < 3) { | |
cerr << "usage: " << basename((char*)argv[0]) << " <schema-file> <test-file>" << endl; | |
exit(EXIT_FAILURE); | |
} | |
const std::string schema_file = argv[1]; | |
const std::string test_file = argv[2]; | |
clog << "Schema:" << endl; | |
dump(schema_file, cout); | |
Variant schema; | |
try { | |
schema = DeserializeJSONFile(schema_file.c_str()); | |
} catch(const exception& e) { | |
cerr << e.what() << endl; | |
exit(EXIT_FAILURE); | |
} | |
clog << "Test:" << endl; | |
dump(test_file, cout); | |
Variant test; | |
try { | |
test = DeserializeJSONFile(test_file.c_str()); | |
} catch(const exception& e) { | |
cerr << e.what() << endl; | |
exit(EXIT_FAILURE); | |
} | |
SchemaResult r = SchemaValidate(schema, test); | |
if(r.Error()) { | |
cerr << r.PrettyPrintMessage() << endl; | |
} else { | |
cout << "validation success!" << endl; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment