Skip to content

Commit

Permalink
Merge pull request #339 from CBookShu/json_optstr_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 9, 2024
2 parents 9aca78e + 9d882b2 commit 4ea64b7
Show file tree
Hide file tree
Showing 2 changed files with 17 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
15 changes: 15 additions & 0 deletions test/test_json_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ 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"; // optional<string> begin with 'n'
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 4ea64b7

Please sign in to comment.