Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions include/rfl/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ auto read(const InputVarType& _obj) {
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(
const std::string_view _json_str, const yyjson_read_flag _flag = 0) {
yyjson_doc* doc = yyjson_read(_json_str.data(), _json_str.size(), _flag);
if (_flag & YYJSON_READ_INSITU) {
return error("YYJSON_READ_INSITU is not supported");
}
yyjson_read_err err;
// According to yyjson's doc, it's safe castaway constness as long as
// YYJSON_READ_INSITU is not set
yyjson_doc* doc = yyjson_read_opts(const_cast<char*>(_json_str.data()),
_json_str.size(), _flag, NULL, &err);
if (!doc) {
return error("Could not parse document");
return error("Could not parse document: " + std::string(err.msg));
}
Comment thread
lixin-wei marked this conversation as resolved.
yyjson_val* root = yyjson_doc_get_root(doc);
const auto r = Reader();
Expand Down
8 changes: 4 additions & 4 deletions tests/json/test_error_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ TEST(json, test_decode_error_without_exception) {
R"({"firstName":"Homer";"lastName":"Simpson";"birthday":"1987-04-19"})";

rfl::Result<Person> result = rfl::error("result didn't get set");
EXPECT_NO_THROW({
result = rfl::json::read<Person>(faulty_string);
});
EXPECT_NO_THROW({ result = rfl::json::read<Person>(faulty_string); });

EXPECT_TRUE(!result.has_value() && true);

EXPECT_EQ(result.error().what(), "Could not parse document");
EXPECT_EQ(result.error().what(),
"Could not parse document: unexpected character, expected a comma "
"or a closing brace");
}

} // namespace test_error_messages
Loading