github stephenberry/glaze v5.3.0

latest releases: v8.3.0, v8.2.0, v8.1.0...
15 months ago

New read_constraint Wrapper

Adds a read_constraint wrapper that enables complex constraints to be defined within a glz::meta or using member functions. Parsing is short circuited upon violating a constraint and a nicely formatted error can be produced with a custom error message.

Example constraint violation outputs:

1:11: constraint_violated
   {"age": -1, "name": "Victor"}
             ^ Age out of range
1:35: constraint_violated
   {"age": 10, "name": "Abra Cadabra"}
                                     ^ Name is too long

Constraint code:

struct constrained_object
{
   int age{};
   std::string name{};
};

template <>
struct glz::meta<constrained_object>
{
   using T = constrained_object;
   static constexpr auto limit_age = [](const T&, int age) {
      return (age >= 0 && age <= 120);
   };
   
   static constexpr auto limit_name = [](const T&, const std::string& name) {
      return name.size() <= 8;
   };
   
   static constexpr auto value = object("age", read_constraint<&T::age, limit_age, "Age out of range">, //
                                        "name", read_constraint<&T::name, limit_name, "Name is too long">);
};

Improvements

Fixes

  • Requires checks for custom_read and custom_write for objects by @stephenberry in #1756
    • Glaze will now respect custom_read and custom_write booleans within glz::meta for object types

Full Changelog: v5.2.1...v5.3.0

Don't miss a new glaze release

NewReleases is sending notifications on new releases.