stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.22k stars 121 forks source link

Shorthand Way to Serialize/Deserialize Json To/From Struct? #27

Closed gurbaaz27 closed 2 years ago

gurbaaz27 commented 2 years ago

Hi! I wanted to know if glaze has some shorthand way to serialize/deserialize a json to/from struct, like the one nlohmann json provides with NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE and NLOHMANN_DEFINE_TYPE_INTRUSIVE macros. To quote their solution, image

If not yet, are there any plans to implement a similar or better thing? Since otherwise basic serialization/deserialization of deeply nested structs become a big pain.

mwalcott3 commented 2 years ago

No, this doesn't exist currently. Yes, this will probably be added soon. It's really simple to add some convenience macros and prevents quite a bit of duplication.

stephenberry commented 2 years ago

Shorthand macros have been added. Thanks for raising this issue.

Documentation has been added to the readme. Here's the example:

In order to use these macros you must include the header: glaze/core/macros.hpp

struct macro_t {
   double x = 5.0;
   std::string y = "yay!";
   int z = 55;
};

GLZ_META(macro_t, x, y, z);

struct local_macro_t {
   double x = 5.0;
   std::string y = "yay!";
   int z = 55;

   GLZ_LOCAL_META(local_macro_t, x, y, z);
};