Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json optstr reader #339

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading