Skip to content

Commit

Permalink
[struct_json]fix for deserialize optional string (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 10, 2024
1 parent 71cbe2e commit d3bb163
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/ylt/standalone/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
17 changes: 17 additions & 0 deletions src/struct_json/examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ void test_escape_serialize() {
#endif
}

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

void test_optional() {
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);
assert(v.name == v1.name);
}

int main() {
person p{"tom", 20};
std::string str;
Expand All @@ -115,4 +131,5 @@ int main() {
use_smart_pointer();
test_escape_serialize();
test_user_defined_struct();
test_optional();
}

0 comments on commit d3bb163

Please sign in to comment.