Open PurushottamHari opened 3 years ago
I have found that the jsonSerializable is not considering the attributes of the super class......I am having to specify it manually in the generated code
Map<String, dynamic> _$ShouldersToJson(Shoulders instance) => <String, dynamic>{ 'parts': instance.parts, };
Is there a way to make the JsonSerializer consider the super class attributes as well?
This library https://github.com/k-paxian/dart-json-mapper does what you need.
The issue I had was that I forgot to include the inherited attributes in the default constructor of the child class. Because the attribute is marked as final in the parent class, json_serializable
has no way to initialize the inherited attributes.
In this specific example, you can try to convert the Shoulders({bool front=false,bool rear=false})
constructor to a named factory constructor and make a new default constructor that does use Map<String,bool> parts
directly. That solved the issue for me.
I hope that helps.
///////////////////////////////////////////////////////////////////////////// "This is the implementation of my classes"
@JsonSerializable() class BodyGroup{
final Map<String,bool> parts;
BodyGroup(this.parts){ debugPrint("Constructor called and initialized!:"+parts.toString()); }
//Abstract function Map<String,double> tabMap(){ return {}; }
void incrementTabMap(Map<String,double> tabMap){ tabMap.forEach((String part, double hits) { debugPrint("TabMap:"+tabMap.toString()); debugPrint("Part:"+part); debugPrint("Parts:"+parts.toString()); if(parts[part]) tabMap[part] = tabMap[part] + 1; }); }
factory BodyGroup.fromJson(Map<String,dynamic> data) => _$BodyGroupFromJson(data);
Map<String,dynamic> toJson() => _$BodyGroupToJson(this); }
@JsonSerializable() class Shoulders extends BodyGroup{
Shoulders({bool front=false,bool rear=false}) : super({"front":front,"rear":rear});
factory Shoulders.fromJson(Map<String,dynamic> data) => _$ShouldersFromJson(data);
Map<String,dynamic> toJson() => _$ShouldersToJson(this);
@override Map<String,double> tabMap() { return { "front":0, "rear":0 }; }
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Now when I make an object of Shoulder class and serialize it......it doesn't include the "parts" map which is initialized always in the BodyGroup Parent class....
Can you please help me out here