Add error message for json read (#461)

This commit is contained in:
Lixin Wei
2025-07-16 09:23:34 +02:00
committed by GitHub
parent 18feb02099
commit 238a1975da
2 changed files with 13 additions and 6 deletions
+9 -2
View File
@@ -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));
}
yyjson_val* root = yyjson_doc_get_root(doc);
const auto r = Reader();
+4 -4
View File
@@ -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