Open gthb opened 5 years ago
I understand it's perhaps not the most intuitive thing ever, but in the meantime, for your exact example, you have to think with the runtime code here, i.e. that your enum is in fact a string
at runtime. As such, you should specify String
as the member constructor (which is the same as the string
primitive for TypedJSON):
enum Color { red="RED", green="GREEN", blue="BLUE" }
@jsonObject
class Thing {
@jsonMember({ constructor: String })
public color?: Color
}
That said though, this is not going to be value-checked at runtime. To do that, you can instead use jsonMember
with an accessor, e.g.:
enum Color { red="RED", green="GREEN", blue="BLUE" }
@jsonObject
class Thing {
private _color?: Color;
@jsonMember({ constructor: String })
public get color(): Color ...
public set color(value: Color) ...
}
If you properly value-check and throw in your setter, the TypedJSON.parse
call should throw on invalid values (assuming your error handler does as well, which is the default behavior). This can be simplified by using a custom decorator that value-checks assignments, as jsonMember
can be used with other decorators.
I want to deserialize an enum property without ReflectDecorators:
How do I populate those
@jsonMember
options to achieve this? Enum types don't have a constructor.The only solution I've found is:
but that's pretty verbose, and requires a separate deserializer function for each enum type (or at least I haven't found a workable way to make it generic).