Skip to content

Commit

Permalink
update README[no ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Jul 30, 2023
1 parent ef1069d commit b19a5a6
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,57 @@ We can slove the problem1 easily with c++17:
//now you can operate the file
```

### how to handle the enum type as strings?

By default, Iguana handle enum type as number type during serialization and deserialization.
To handle the enum type as strings during serialization and deserialization with Iguana, we need to define a full specialization template in the "iguana" namespace. This template is a struct that contains an array with the underlying numbers corresponding to the enum type.
For example, if we have the following enum type:

```c++
enum class Status { STOP = 10, START };
```

And we want to handle the enum type as strings when parsing JSON:

```c++
std::string str = R"(
{
"a": "START",
"b": "STOP",
}
)";
```

To do this, we define the full specialization template in the "iguana" namespace:

```c++
namespace iguana {
template <> struct enum_value<Status> {
constexpr static std::array<int, 2> value = {10, 11};
};
} // namespace iguana
```

Once this is done, we can continue writing the rest of the code as usual.

```c++
struct enum_t {
Status a;
Status b;
};
REFLECTION(enum_t, a, b);

// deserialization
enum_t e;
iguana::from_json(e, str);
// serialization
enum_t e;
e.a = Status::START;
e.b = Status::STOP;
std::string ss;
iguana::to_json(e ss);
```

### Full sources:


Expand Down

0 comments on commit b19a5a6

Please sign in to comment.