Closed Xerosigma closed 10 years ago
Dart doesn't have enums yet. SkillType
in your example is just a way to mimic enum but it's not an enum. So, there is no way for this library to detect it and encode/decode properly.
Hi there,
The process of encoding/decoding an arbitrary Dart object is as follows.
toEncodable
Dart Object -----------> Json Object -----------> Json String
toObject
Dart Object <----------- Json Object <----------- Json String
In the current version, users cannot control the behavior of toEncodable
or toObject
. That prevents the library from working with DateTime
or simulated enums
. So, I'm working on a new version that allows users to customize toObject
and toEncodable
methods.
Now, you can make the library work with your enums as below.
class Enum {
final int _id;
const Enum._(this._id);
static const ONE = const Enum._(1);
static const TWO = const Enum._(2);
}
//--------------
customToObjects[Enum] = (int input) {
if (input == 1) return Enum.ONE;
if (input == 2) return Enum.TWO;
throw new ArgumentError('Unknown enum value [$input]');
};
customToEncodables[Enum] = (Enum input) => input._id;
//--------------
assert(encode(Enum.ONE) == '1');
assert(decode('1', type: Enum) == Enum.ONE);
Awesome, thanks for the update! :thumbsup:
The latest version has been published. Please try it out!
How would one encode and decode the below?
Given this example class:
My desired result is as follows: