github stephenberry/glaze v1.5.0
1.5.0

latest releases: v8.4.0, v8.3.0, v8.2.0...
2 years ago

New Custom Read/Write Support

Below is an example using the new glz::custom<Read, Write> wrapper:

struct custom_encoding
{
   uint64_t x{};
   std::string y{};
   std::array<uint32_t, 3> z{};
   
   void read_x(const std::string& s) {
      x = std::stoi(s);
   }
   
   uint64_t write_x() {
      return x;
   }
   
   void read_y(const std::string& s) {
      y = "hello" + s;
   }
   
   auto& write_z() {
      z[0] = 5;
      return z;
   }
};

template <>
struct glz::meta<custom_encoding>
{
   using T = custom_encoding;
   static constexpr auto value = object("x", custom<&T::read_x, &T::write_x>, //
                                        "y", custom<&T::read_y, &T::y>, //
                                        "z", custom<&T::z, &T::write_z>);
};

suite custom_encoding_test = [] {
   "custom_reading"_test = [] {
      custom_encoding obj{};
      std::string s = R"({"x":"3","y":"world","z":[1,2,3]})";
      expect(!glz::read_json(obj, s));
      expect(obj.x == 3);
      expect(obj.y == "helloworld");
      expect(obj.z == std::array<uint32_t, 3>{1, 2, 3});
   };
   
   "custom_writing"_test = [] {
      custom_encoding obj{};
      std::string s = R"({"x":"3","y":"world","z":[1,2,3]})";
      expect(!glz::read_json(obj, s));
      std::string out{};
      glz::write_json(obj, out);
      expect(out == R"({"x":3,"y":"helloworld","z":[5,2,3]})");
   };
};

Fixes:

  • Support for rvalue pointers within registration macros. e.g. [](auto& self) { return self.unique_ptr.get(); }

Deprecations:

  • Removed invoke_update as the implementation needs to be rethought and improved

Don't miss a new glaze release

NewReleases is sending notifications on new releases.