-
Notifications
You must be signed in to change notification settings - Fork 184
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
In StandardJsonPlugin, deserialization fails when the value in a Map is NULL #653
Comments
I am now working around the issue using the following plugin: class RemoveNullInMapConvertedListPlugin implements SerializerPlugin {
Object beforeSerialize(Object object, FullType specifiedType) => object;
Object afterSerialize(Object object, FullType specifiedType) => object;
Object beforeDeserialize(Object object, FullType specifiedType) {
if (specifiedType.root == BuiltMap && object is List) {
return object.where((v) => v != null).toList();
}
return object;
}
Object afterDeserialize(Object object, FullType specifiedType) => object;
} But I'd prefer those keys with NULL values are preserved in the final BuiltMap, and still don't understand why it was decided that |
Thanks for the detailed report! That code was written with objects in mind; when a field is null,
|
Thank you @tamcy for the workaround... @davidmorgan do you have plan to solve this issue anytime soon? In the meanwhile, @tamcy workaround should be used? Thank you @davidmorgan |
No ETA I'm afraid; I'll try to include it with the next batch of fixes/improvements. Yes, the workaround given above is the right way to go for now. |
Consider the following model class:
Here
remarks
is aMap
that stores localized values. When deserializing withStandardJsonPlugin
, the following data works fine:But this example will fail:
And the error is as below:
Looks like this is caused by the function
StandardJsonPlugin
's_toListUsingDiscriminator
, which converts theMap
to aList
:built_value.dart/built_value/lib/standard_json_plugin.dart
Lines 159 to 202 in 2480cb4
In this function, keys with
NULL
value are simply skipped (line 195). I don't know the rationale behind this, but since the length of the list is fixed to the sum of keys and values of the Map (line 187), this causes two NULL values at the end of the list whenever a NULL value in the Map is encountered and skipped. Which is why the second JSON sample data results in a List with four NULLs, and the code fails when attempting to use the NULL as the key in_toMap()
:built_value.dart/built_value/lib/standard_json_plugin.dart
Lines 80 to 88 in 2480cb4
Seems the "lightest" fix would be to break the loop in
_toMap()
whenkey
isNULL
(this may affect other plugins though). What do you think?The text was updated successfully, but these errors were encountered: