I'd like to make use of Dart's new (experimental) enum
feature instead of using stacks of static const Strings, but what's the best way to serialize/deserialize enum
variables using JSON? I've made it work this way, but surely there's a better solution:
enum Status {
none,
running,
stopped,
paused
}
Status status1 = Status.stopped;
Status status2 = Status.none;
String json = JSON.encode(status1.index);
print(json); // prints 2
int index = JSON.decode(json);
status2 = Status.values[index];
print(status2); // prints Status.stopped
If you serialize using the index, you can get locked into keeping your enums in the same order forever, so I'd much prefer to use some kind of String form. Anyone figured this out?