Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
28e6a40f
Commit
28e6a40f
authored
Jan 31, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change indentation to space
parent
48e0675e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
48 deletions
+48
-48
schemavalidator.cpp
example/schemavalidator/schemavalidator.cpp
+48
-48
No files found.
example/schemavalidator/schemavalidator.cpp
View file @
28e6a40f
...
...
@@ -10,56 +10,56 @@
using
namespace
rapidjson
;
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: schemavalidator schema.json < input.json
\n
"
);
return
EXIT_FAILURE
;
}
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: schemavalidator schema.json < input.json
\n
"
);
return
EXIT_FAILURE
;
}
// Read a JSON schema from file into Document
Document
d
;
char
buffer
[
4096
];
// Read a JSON schema from file into Document
Document
d
;
char
buffer
[
4096
];
{
FILE
*
fp
=
fopen
(
argv
[
1
],
"r"
);
if
(
!
fp
)
{
printf
(
"Schema file '%s' not found
\n
"
,
argv
[
1
]);
return
-
1
;
}
FileReadStream
fs
(
fp
,
buffer
,
sizeof
(
buffer
));
d
.
ParseStream
(
fs
);
if
(
d
.
HasParseError
())
{
fprintf
(
stderr
,
"Schema file '%s' is not a valid JSON
\n
"
,
argv
[
1
]);
fprintf
(
stderr
,
"Error(offset %u): %s
\n
"
,
static_cast
<
unsigned
>
(
d
.
GetErrorOffset
()),
GetParseError_En
(
d
.
GetParseError
()));
fclose
(
fp
);
return
EXIT_FAILURE
;
}
fclose
(
fp
);
}
// Then convert the Document into SchemaDocument
SchemaDocument
sd
(
d
);
{
FILE
*
fp
=
fopen
(
argv
[
1
],
"r"
);
if
(
!
fp
)
{
printf
(
"Schema file '%s' not found
\n
"
,
argv
[
1
]);
return
-
1
;
}
FileReadStream
fs
(
fp
,
buffer
,
sizeof
(
buffer
));
d
.
ParseStream
(
fs
);
if
(
d
.
HasParseError
())
{
fprintf
(
stderr
,
"Schema file '%s' is not a valid JSON
\n
"
,
argv
[
1
]);
fprintf
(
stderr
,
"Error(offset %u): %s
\n
"
,
static_cast
<
unsigned
>
(
d
.
GetErrorOffset
()),
GetParseError_En
(
d
.
GetParseError
()));
fclose
(
fp
);
return
EXIT_FAILURE
;
}
fclose
(
fp
);
}
// Then convert the Document into SchemaDocument
SchemaDocument
sd
(
d
);
// Use reader to parse the JSON in stdin, and forward SAX events to validator
SchemaValidator
validator
(
sd
);
Reader
reader
;
FileReadStream
is
(
stdin
,
buffer
,
sizeof
(
buffer
));
if
(
!
reader
.
Parse
(
is
,
validator
)
&&
reader
.
GetParseErrorCode
()
!=
kParseErrorTermination
)
{
// Schema validator error would cause kParseErrorTermination, which will handle it in next step.
fprintf
(
stderr
,
"Input is not a valid JSON
\n
"
);
fprintf
(
stderr
,
"Error(offset %u): %s
\n
"
,
static_cast
<
unsigned
>
(
reader
.
GetErrorOffset
()),
GetParseError_En
(
reader
.
GetParseErrorCode
()));
}
// Use reader to parse the JSON in stdin, and forward SAX events to validator
SchemaValidator
validator
(
sd
);
Reader
reader
;
FileReadStream
is
(
stdin
,
buffer
,
sizeof
(
buffer
));
if
(
!
reader
.
Parse
(
is
,
validator
)
&&
reader
.
GetParseErrorCode
()
!=
kParseErrorTermination
)
{
// Schema validator error would cause kParseErrorTermination, which will handle it in next step.
fprintf
(
stderr
,
"Input is not a valid JSON
\n
"
);
fprintf
(
stderr
,
"Error(offset %u): %s
\n
"
,
static_cast
<
unsigned
>
(
reader
.
GetErrorOffset
()),
GetParseError_En
(
reader
.
GetParseErrorCode
()));
}
// Check the validation result
if
(
validator
.
IsValid
())
{
printf
(
"Input JSON is valid.
\n
"
);
return
EXIT_SUCCESS
;
}
else
{
printf
(
"Input JSON is invalid.
\n
"
);
// Check the validation result
if
(
validator
.
IsValid
())
{
printf
(
"Input JSON is valid.
\n
"
);
return
EXIT_SUCCESS
;
}
else
{
printf
(
"Input JSON is invalid.
\n
"
);
StringBuffer
sb
;
validator
.
GetInvalidSchemaPointer
().
StringifyUriFragment
(
sb
);
fprintf
(
stderr
,
"Invalid schema: %s
\n
"
,
sb
.
GetString
());
...
...
@@ -67,6 +67,6 @@ int main(int argc, char *argv[]) {
sb
.
Clear
();
validator
.
GetInvalidDocumentPointer
().
StringifyUriFragment
(
sb
);
fprintf
(
stderr
,
"Invalid document: %s
\n
"
,
sb
.
GetString
());
return
EXIT_FAILURE
;
}
return
EXIT_FAILURE
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment