Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
json-schema-validator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IOPSYS
json-schema-validator
Commits
ac72687a
Commit
ac72687a
authored
5 years ago
by
Jakob Olsson
Browse files
Options
Downloads
Patches
Plain Diff
add json-validator source...
parent
29304388
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/json-validator.cpp
+86
-0
86 additions, 0 deletions
src/json-validator.cpp
with
86 additions
and
0 deletions
src/json-validator.cpp
0 → 100644
+
86
−
0
View file @
ac72687a
#include
<iostream>
#include
<iomanip>
#include
<stdbool.h>
#include
<libubox/avl.h>
#include
<libubox/avl-cmp.h>
#include
<libubox/blobmsg.h>
#include
<libubox/blobmsg_json.h>
#include
<libubox/utils.h>
#include
<libubus.h>
#include
<glob.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<json-c/json.h>
#include
<json-schema.hpp>
#include
"schema.h"
#include
"json-validator.h"
using
nlohmann
::
json
;
using
nlohmann
::
json_uri
;
using
nlohmann
::
json_schema
::
json_validator
;
int
schema_validator_destroy
(
void
)
{
schema_flush_objects
();
return
0
;
}
int
schema_validator_init
(
void
)
{
schema_setup_json
();
return
0
;
}
bool
json_object_validate_schema_inject_definitions
(
struct
json_object
*
j_object
,
struct
json_object
*
definitions
,
struct
json_object
*
j_schema
)
{
json_object_object_add
(
j_object
,
"definitions"
,
definitions
);
return
json_object_validate_schema
(
j_object
,
j_schema
);
}
bool
json_object_validate_schema
(
struct
json_object
*
j_object
,
struct
json_object
*
j_schema
)
{
std
::
cout
<<
"hello"
<<
std
::
endl
;
const
char
*
sch_str
,
*
obj_str
;
obj_str
=
json_object_get_string
(
j_object
);
sch_str
=
json_object_get_string
(
j_schema
);
std
::
cout
<<
"obj_str "
<<
obj_str
<<
std
::
endl
;
std
::
cout
<<
"sch_str "
<<
sch_str
<<
std
::
endl
;
json
sch
=
json
::
parse
(
sch_str
);
json
obj
=
json
::
parse
(
obj_str
);
/* json-parse the schema */
json_validator
validator
;
// create validator
try
{
validator
.
set_root_schema
(
sch
);
// insert root-schema
}
catch
(
const
std
::
exception
&
e
)
{
std
::
cerr
<<
"Validation of schema failed, here is why: "
<<
e
.
what
()
<<
"
\n
"
;
return
0
;
}
/* json-parse the people - with custom error handler */
class
custom_error_handler
:
public
nlohmann
::
json_schema
::
basic_error_handler
{
void
error
(
const
nlohmann
::
json
::
json_pointer
&
ptr
,
const
json
&
instance
,
const
std
::
string
&
message
)
override
{
nlohmann
::
json_schema
::
basic_error_handler
::
error
(
ptr
,
instance
,
message
);
std
::
cerr
<<
"ERROR: '"
<<
ptr
<<
"' - '"
<<
instance
<<
"': "
<<
message
<<
"
\n
"
;
}
};
custom_error_handler
err
;
validator
.
validate
(
obj
,
err
);
// validate the document
if
(
err
)
return
0
;
return
1
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment