Skip to content

Commit e936b07

Browse files
committed
Add error message for json read
1 parent c06d3d5 commit e936b07

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

include/rfl/json/read.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ auto read(const InputVarType& _obj) {
3232
template <class T, class... Ps>
3333
Result<internal::wrap_in_rfl_array_t<T>> read(
3434
const std::string_view _json_str, const yyjson_read_flag _flag = 0) {
35-
yyjson_doc* doc = yyjson_read(_json_str.data(), _json_str.size(), _flag);
35+
if (_flag & YYJSON_READ_INSITU) {
36+
return error("YYJSON_READ_INSITU is not supported");
37+
}
38+
yyjson_read_err err;
39+
// According to yyjson's doc, it's safe castaway constness as long as
40+
// YYJSON_READ_INSITU is not set
41+
yyjson_doc* doc = yyjson_read_opts(const_cast<char*>(_json_str.data()),
42+
_json_str.size(), _flag, NULL, &err);
3643
if (!doc) {
37-
return error("Could not parse document");
44+
return error("Could not parse document: " + std::string(err.msg));
3845
}
3946
yyjson_val* root = yyjson_doc_get_root(doc);
4047
const auto r = Reader();

tests/json/test_error_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TEST(json, test_decode_error_without_exception) {
4343

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

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

4949
} // namespace test_error_messages

0 commit comments

Comments
 (0)