Skip to content

Commit

Permalink
修复解析optional<string> 遇到 'n' 开头时异常问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CBookShu committed Dec 9, 2024
1 parent 9aca78e commit 0d78b11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,6 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
template <typename U, typename It, std::enable_if_t<optional_v<U>, int> = 0>
IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
skip_ws(it, end);
if (it < end && *it == '"')
IGUANA_LIKELY { ++it; }
using T = std::remove_reference_t<U>;
if (it == end)
IGUANA_UNLIKELY { throw std::runtime_error("Unexexpected eof"); }
Expand All @@ -469,6 +467,8 @@ IGUANA_INLINE void from_json_impl(U &value, It &&it, It &&end) {
using value_type = typename T::value_type;
value_type t;
if constexpr (string_v<value_type> || string_view_v<value_type>) {
if (it < end && *it == '"')
IGUANA_LIKELY { ++it; }
from_json_impl<true>(t, it, end);
}
else {
Expand Down
16 changes: 16 additions & 0 deletions test/test_json_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ TEST_CASE("test instruments.json") {
}
}

struct test_optstr_reader_null {
std::optional<std::string> name;
};
YLT_REFL(test_optstr_reader_null, name);
TEST_CASE("test_optstr_reader") {

test_optstr_reader_null v;
v.name = "name"; // n¿ªÍ·µÄoptional ×Ö·û´®
std::string json;
iguana::to_json(v, json);

test_optstr_reader_null v1;
iguana::from_json(v1, json);
CHECK(v.name == v1.name);
}

// doctest comments
// 'function' : must be 'attribute' - see issue #182
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007) int main(int argc, char **argv) {
Expand Down

0 comments on commit 0d78b11

Please sign in to comment.