google / google/json_serializable.dart
serialize parent attribute in child class
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
Hi,
I have this parent class:
```
@JsonSerializable()
class Parent extends Grandpa {
final Item item;
const Parent({
required id,
}) : super(
id: id,
);
factory Parent.fromJson(Map json) =>
_$ParentFromJson(json);
@override
Map toJson() => _$ParentToJson(this);
}
```
And this child class:
```
@JsonSerializable()
class Child extends Parent {
final List quantities;
const Child({
required id,
required item,
}) : super(
id: id,
item: item,
);
factory Child.fromJson(Map json) =>
_$ChildFromJson(json);
@override
Map toJson() => _$ChildToJson(this);
}
```
The script generates this code:
```
// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Child _$ChildFromJson(Map json) =>
Child(
id: json['id'],
item: json['item'], // <---- HERE I GET THE ERROR
);
Map _$ChildToJson(Child instance) =>
{
'id': instance.id,
'item': instance.item,
};
```
But I have an error cause `json['item']` is not a String, is a `Map` that should be converted with `Item.fromJson(json['item'] as Map)`
Contributor guide
Assessment
This issue has not been assessed yet.