google / google/json_serializable.dart
Skip unparsable Collection values
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
Given a `JSONArray`, where certain items can't be parsed, what's the best way to skip/filter only those elements, instead of failing to parse the whole `JSONArray`.
Example:
```
@JsonSerializable
class MyWrapperResponse(List object);
```
where parsing certain `SomeType`s will fail, but the app would still like to show the other items.
Thinking of a custom converter:
```
class ListSkipConverter implements JsonConverter, Map> {
const ListSkipConverter({required this.key});
final String key;
@override
List fromJson(Map json) => (json[key] as List)
.map((e) {
try {
return e.fromJson(e as Map) as T;
} catch (e) {
return null;
}
})
.whereType()
.toList();
@override
Map toJson(List object) => {key: object.map((e) => e.toJson()).toList()};
}
```
which unfortunately doesn't work as these converters are only post processors 🤔
Contributor guide
Assessment
This issue has not been assessed yet.