1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// Tests for skip_null_members_on_read option in BEVE format
#include "glaze/glaze.hpp"
#include "ut/ut.hpp"
using namespace ut;
struct simple_struct
{
std::string name = "default";
int age = 0;
double score = 0.0;
};
template <>
struct glz::meta<simple_struct>
{
using T = simple_struct;
static constexpr auto value = glz::object("name", &T::name, "age", &T::age, "score", &T::score);
};
// Struct with optional fields for writing null values
struct optional_struct
{
std::optional<std::string> name;
std::optional<int> age;
std::optional<double> score;
};
template <>
struct glz::meta<optional_struct>
{
using T = optional_struct;
static constexpr auto value = glz::object("name", &T::name, "age", &T::age, "score", &T::score);
};
// Custom options with skip_null_members_on_read enabled for BEVE
struct opts_skip_null_beve : glz::opts
{
uint32_t format = glz::BEVE;
bool skip_null_members_on_read = true;
};
suite skip_null_on_read_beve_tests = [] {
"skip null fields"_test = [] {
constexpr opts_skip_null_beve opts{};
simple_struct obj{};
obj.name = "original";
obj.age = 25;
obj.score = 100.0;
// Write with null values using optional struct
optional_struct temp;
temp.name = std::nullopt;
temp.age = 30;
temp.score = std::nullopt;
std::string buffer;
expect(!glz::write_beve(temp, buffer));
auto ec = glz::read<opts>(obj, buffer);
expect(!ec);
expect(obj.name == "original"); // Null was skipped
expect(obj.age == 30);
expect(obj.score == 100.0); // Null was skipped
};
"skip all null fields"_test = [] {
constexpr opts_skip_null_beve opts{};
simple_struct obj{};
obj.name = "original";
obj.age = 25;
obj.score = 100.0;
optional_struct temp;
temp.name = std::nullopt;
temp.age = std::nullopt;
temp.score = std::nullopt;
std::string buffer;
expect(!glz::write_beve(temp, buffer));
auto ec = glz::read<opts>(obj, buffer);
expect(!ec);
// All fields retain original values
expect(obj.name == "original");
expect(obj.age == 25);
expect(obj.score == 100.0);
};
};
int main() {}
|