schemavalidator.cpp
Go to the documentation of this file.
1 // Schema Validator example
2 
3 // The example validates JSON text from stdin with a JSON schema specified in the argument.
4 
5 #include "rapidjson/error/en.h"
7 #include "rapidjson/schema.h"
9 
10 using namespace rapidjson;
11 
12 int main(int argc, char *argv[]) {
13  if (argc != 2) {
14  fprintf(stderr, "Usage: schemavalidator schema.json < input.json\n");
15  return EXIT_FAILURE;
16  }
17 
18  // Read a JSON schema from file into Document
19  Document d;
20  char buffer[4096];
21 
22  {
23  FILE *fp = fopen(argv[1], "r");
24  if (!fp) {
25  printf("Schema file '%s' not found\n", argv[1]);
26  return -1;
27  }
28  FileReadStream fs(fp, buffer, sizeof(buffer));
29  d.ParseStream(fs);
30  if (d.HasParseError()) {
31  fprintf(stderr, "Schema file '%s' is not a valid JSON\n", argv[1]);
32  fprintf(stderr, "Error(offset %u): %s\n",
33  static_cast<unsigned>(d.GetErrorOffset()),
35  fclose(fp);
36  return EXIT_FAILURE;
37  }
38  fclose(fp);
39  }
40 
41  // Then convert the Document into SchemaDocument
42  SchemaDocument sd(d);
43 
44  // Use reader to parse the JSON in stdin, and forward SAX events to validator
45  SchemaValidator validator(sd);
46  Reader reader;
47  FileReadStream is(stdin, buffer, sizeof(buffer));
48  if (!reader.Parse(is, validator) && reader.GetParseErrorCode() != kParseErrorTermination) {
49  // Schema validator error would cause kParseErrorTermination, which will handle it in next step.
50  fprintf(stderr, "Input is not a valid JSON\n");
51  fprintf(stderr, "Error(offset %u): %s\n",
52  static_cast<unsigned>(reader.GetErrorOffset()),
54  }
55 
56  // Check the validation result
57  if (validator.IsValid()) {
58  printf("Input JSON is valid.\n");
59  return EXIT_SUCCESS;
60  }
61  else {
62  printf("Input JSON is invalid.\n");
63  StringBuffer sb;
64  validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
65  fprintf(stderr, "Invalid schema: %s\n", sb.GetString());
66  fprintf(stderr, "Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());
67  sb.Clear();
68  validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
69  fprintf(stderr, "Invalid document: %s\n", sb.GetString());
70  return EXIT_FAILURE;
71  }
72 }
d
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:558
Parsing was terminated.
Definition: error.h:88
const Ch * GetString() const
Definition: stringbuffer.h:73
JSON schema document.
Definition: fwd.h:136
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition: reader.h:684
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Definition: document.h:2362
PointerType GetInvalidSchemaPointer() const
Gets the JSON pointer pointed to the invalid schema.
Definition: schema.h:1652
int main(int argc, char *argv[])
main RapidJSON namespace
PointerType GetInvalidDocumentPointer() const
Gets the JSON pointer pointed to the invalid value.
Definition: schema.h:1662
virtual bool IsValid() const
Checks whether the current state is valid.
Definition: schema.h:1649
File byte stream for input using fread().
const Ch * GetInvalidSchemaKeyword() const
Gets the keyword of invalid schema.
Definition: schema.h:1657
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: document.h:2365
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: reader.h:687
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition: en.h:36
JSON Schema Validator.
Definition: fwd.h:145
bool HasParseError() const
Whether a parse error has occured in the last parsing.
Definition: document.h:2359
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:2230


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09