Support serializing Enum values instead of names #660
Answered
by
danielaparker
sarahlydia9868
asked this question in
Ideas
-
|
I propose adding a trait or macro similar to enum class PacketCompression : uint32_t {
NONE = 199_u32,
TEXTURE,
GENERAL,
ALL,
};
JSONCONS_ENUM_VALUE_TRAITS(PacketCompression, NONE, TEXTURE, GENERAL, ALL);Output: (PacketCompression = NONE) |
Beta Was this translation helpful? Give feedback.
Answered by
danielaparker
Nov 22, 2025
Replies: 1 comment
-
|
This should be default behaviour now on master, #include <jsoncons/json.hpp>
#include <iostream>
enum class PacketCompression {
NONE = 199,
TEXTURE,
GENERAL,
ALL
};
struct Foo
{
PacketCompression value;
};
JSONCONS_ALL_MEMBER_TRAITS(Foo,value)
int main()
{
try
{
auto j = jsoncons::json::parse(R"({"value":199})");
auto foo = j.as<Foo>();
std::cout << "(1) " << (int)foo.value << "\n";
std::string buffer;
jsoncons::encode_json(foo, buffer);
std::cout << "(2) " << buffer << "\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
}Output: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sarahlydia9868
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should be default behaviour now on master,
Output: