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

[feat] support user-defined method to serialize/deserialize struct #243

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

poor-circle
Copy link
Contributor

@poor-circle poor-circle commented Mar 5, 2024

example:

namespace my_space {
struct my_struct {
  int x, y, z;
  bool operator==(const my_struct& o) const {
    return x == o.x && y == o.y && z == o.z;
  }
};

template <bool Is_writing_escape, typename Stream>
inline void to_json_impl(Stream& s, const my_struct& t) {
  iguana::to_json<Is_writing_escape>(*(int(*)[3]) & t, s);
}

template <typename It>
IGUANA_INLINE void from_json_impl(my_struct& value, It&& it, It&& end) {
  iguana::from_json(*(int(*)[3]) & value, it, end);
}

}  // namespace my_space

struct nest {
  std::string name;
  my_space::my_struct value;
  bool operator==(const nest& o) const {
    return name == o.name && value == o.value;
  }
};

REFLECTION(nest, name, value);

void user_defined_struct_example() {
  {
    my_space::my_struct v{1, 2, 3}, v2;
    std::string s;
    iguana::to_json(v, s);
    std::cout << s << std::endl;
    iguana::from_json(v2, s);
    assert(v == v2);
  }
  {
    nest v{"Hi", {1, 2, 3}}, v2;
    std::string s;
    iguana::to_json(v, s);
    std::cout << s << std::endl;
    iguana::from_json(v2, s);
    assert(v == v2);
  }
}

@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2024

Codecov Report

Attention: Patch coverage is 21.56863% with 40 lines in your changes are missing coverage. Please review.

Project coverage is 44.67%. Comparing base (b00ba10) to head (ff76e0d).

Files Patch % Lines
test/test.cpp 0.00% 21 Missing ⚠️
example/json_example.cpp 0.00% 16 Missing ⚠️
example/xml_example.cpp 0.00% 2 Missing ⚠️
iguana/json_writer.hpp 83.33% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #243      +/-   ##
==========================================
- Coverage   44.91%   44.67%   -0.25%     
==========================================
  Files          46       46              
  Lines        5553     5592      +39     
==========================================
+ Hits         2494     2498       +4     
- Misses       3059     3094      +35     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@qicosmos qicosmos merged commit fa7cfde into qicosmos:master Mar 5, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants