mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 22:36:19 +08:00
Add error message for json read (#461)
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user