Skip to content

Commit

Permalink
By default throws on unknown keys
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Oct 17, 2022
1 parent db61efd commit ffc74e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/glaze/core/opts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace glz
uint32_t format = json;
bool comments = false; // write out comments
bool opening_handled = false; // is whitespace and the opening character handled
bool error_on_unknown_keys = true;
};

template <opts Opts>
Expand Down
7 changes: 6 additions & 1 deletion include/glaze/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,12 @@ namespace glz
member_it->second);
}
else [[unlikely]] {
skip_object_value(it, end);
if constexpr (Opts.error_on_unknown_keys) {
throw std::runtime_error("Unknown key: " + std::string(key));
}
else {
skip_object_value(it, end);
}
}
}
else {
Expand Down
7 changes: 7 additions & 0 deletions tests/json_test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ void user_types() {

//Should skip invalid keys
expect(nothrow([&] {
//glaze::read_json(obj,"{/**/ \"b\":\"fox\", \"c\":7.7/**/, \"d\": {\"a\": \"}\"} //\n /**/, \"a\":322}");
glz::read<glz::opts{.error_on_unknown_keys = false}>(obj,
R"({/**/ "b":"fox", "c":7.7/**/, "d": {"a": "}"} //
/**/, "a":322})");
}));

expect(throws([&] {
//glaze::read_json(obj,"{/**/ \"b\":\"fox\", \"c\":7.7/**/, \"d\": {\"a\": \"}\"} //\n /**/, \"a\":322}");
glz::read_json(obj,
R"({/**/ "b":"fox", "c":7.7/**/, "d": {"a": "}"} //
Expand Down

0 comments on commit ffc74e8

Please sign in to comment.