google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 395 forks source link

@JsonKey on derived attributes #1437

Open Salvatore95 opened 1 month ago

Salvatore95 commented 1 month ago

I am trying to implement Clean Architecture by keeping Entities separate from Models but I cannot assign JsonKeys to the attributes derived from parent, below is an example of what I would like to achieve:

class Car {
    final String attributeOne;
    final String attributeTwo;
    Car({required this.attributeOne, required this.attributeTwo})
}

@JsonSerializable()
class CarModel extends Car {
    CarModel({
    @JsonKey(name: 'attr_one') required String attrOne, 
    @JsonKey(name: 'attr_two') required String attrTwo,
    }): super(attributeOne: attrOne, attributeTwo: attrTwo),
}

This approach does not work, is there any alternative way?

kevmoo commented 1 month ago

At the moment, no. This is a feature request.