Skip to content

Problem (de-)serializing an enum with value 0 if the | operator is overloaded #640

@florian-n

Description

@florian-n

Hello,
I am unfortunately running into an issue with the newest version of the reflect-cpp lib.

Assume I have something like this:

enum class TestEnum : uint64_t
{
	None = 0,
	Hello = 1 << 0,
	World = 1 << 1,
	HelloWorld = Hello | World
};
struct SomeClass
{
	TestEnum e;
	TestEnum f;
	TestEnum g;
};

void Foo()
{
	SomeClass t{.e = TestEnum::None, .f = TestEnum::Hello, .g = TestEnum::HelloWorld};
	const auto jsonString = rfl::json::write(t);
	const auto t2 = rfl::json::read<SomeClass>(jsonString).value();
}

^Works fine. The object will be serialized to {"e":"None","f":"Hello","g":"HelloWorld"} and back as expected.

The issue arises once I overload the | operator:

static constexpr TestEnum operator|(TestEnum a, TestEnum b) noexcept { 
	return static_cast<TestEnum>(static_cast<uint64_t>(a) | static_cast<uint64_t>(b)); 
}

This changes the serialization to {"e":"","f":"Hello","g":"Hello|World"}. The "Hello|World" part is expected, as per your documentation.

The issue is that what was previously "None" was now serialized to the empty string "", eventually failing the de-serialization step with the error "Failed to parse field 'e': Invalid enum value: ''. Must be one of [None, Hello, World].".

I am unsure what the expected behavior should be. I can see all variants "None", "" and "0" make sense. Imo the bigger problem is the fact that the immediate de-serialization fails.

I would be very thankful for your help!
Best, Florian

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions