How to serialize integer into string and then deserialize back from string to integer #4350
-
What I have is enum class Type {
A,
B,
C
}; And I want to serialize it to json but instead of using int (which is default for enum class), I want it to save to a string, so Was able to do it successfully by implementing void to_json(json &j, const Type &t) {
switch (t) {
case Type::A:
j = "A";
case Type::B:
.... etc But when I try to deserialize it back from string to Type, I get an exception saying that "type must be integer, but is string". Deserialization method follows same principle, so: void from_json(const json &j, Type &t) {
if (json == "A") {
t = Type::A;
) else if (json == "B") {
..... etc I was hoping it would solve the problem of converting between int and string but it doesnt. Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
There's a macro you can use to serialize enums. Check here for the documentation. |
Beta Was this translation helpful? Give feedback.
There's a macro you can use to serialize enums. Check here for the documentation.