-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enum as Map keys causing key type is int #1362
Comments
I think I just have a bug in the |
The generated enum map will use their values name as keys const _$SlotEnumMap = {
Slot.slot1: 'slot1',
Slot.slot2: 'slot2',
Slot.slot3: 'slot3',
}; |
@kevmoo yes, it works fine in other cases. It only happens when enums are used as json keys and is annotated with int values. |
I think the code is correct, honestly! The input value can be either a String |
It converts to a int, shouldn't json's keys always be strings? |
It causes wierd errors like this. final object = MyClass(inv: {Slot.slot1: "ww", Slot.slot3: "ww"});
final json = object.toJson();
final deserialized = MyClass.fromJson(json); throws Even the jsonEncode in standard lib doesn't work. final object = MyClass(inv: {Slot.slot1: "ww", Slot.slot3: "ww"});
final json = object.toJson();
final deserialized = jsonEncode(json); throws |
Hrm...let me look again... |
Yeah. This is crazy complex. Effectively, (now) you CANNOT use an enum as a Map key AND set a We'd need to create some funky custom version of We CAN'T just to |
We could/should throw an error here and say "hey, you can't use an enum here" to be more clear this is not supported. |
I read the doc and swa that only int and String are the supported enum values. This should make it less complex? |
Not really. I could just throw in a |
json_serializable.dart/json_serializable/README.md Lines 112 to 113 in e2c8bad
The doc referenced above I could open a pr for throwing the error, when should we throw it? |
@SpeedReach not outdated, but it's not specific enough to handle the case where You could try for a PR, but it's CRAZY complex to do it cleanly. I wrote most of this package. To do the plumbing correctly will be quite tough. I appreciate the enthusiasm, but it'd be a LOT of work. |
Problem
generated enum map
Generated toJson:
Since enums can be Map keys, and enums can be tranfered to int with JsonValue.
This produces a out come that the json key is a int.
Generated fromJson:
I don't think this should happen, since json's key should always be a String.
A solution would be:
Generated toJson:
Generated fromJson:
enumDecodeJsonKey would be a helper specific for enum map keys, would have something like.
If this looks nice , I'd be happy to work on this and submit a pr!
The text was updated successfully, but these errors were encountered: