As I got no answer on that on stackoverflow I hope for an answer here.
When extending a class and serializing a complexe object I get some problems. Ist seems the problem only existing when extending and having the complexe object in the constructor:
import 'package:json_annotation/json_annotation.dart';
part 'app_user.g.dart';
@JsonSerializable(explicitToJson: true)
class AppUser extends BaseUser{
String name;
AppUser(this.name, avatar) : super(avatar);
factory AppUser.fromJson(Map<String, dynamic> json) => _$AppUserFromJson(json);
Map<String, dynamic> toJson() => _$AppUserToJson(this);
}
class BaseUser{
AvatarConfiguration? avatar;
BaseUser(AvatarConfiguration avatar);
}
@JsonSerializable()
class AvatarConfiguration{
@JsonKey(name: "he")
int head;
@JsonKey(name: "ha")
int hair;
@JsonKey(name: "no")
int nose;
@JsonKey(name: "mo")
int mouth;
@JsonKey(name: "ey")
int eye;
AvatarConfiguration(this.head, this.hair, this.nose, this.mouth, this.eye);
factory AvatarConfiguration.fromJson(Map<String, dynamic> json) => _$AvatarConfigurationFromJson(json);
Map<String, dynamic> toJson() => _$AvatarConfigurationToJson(this);
}
I get that generated:
part of 'app_user.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
AppUser _$AppUserFromJson(Map<String, dynamic> json) => AppUser(
json['name'] as String,
json['avatar'],
);
Map<String, dynamic> _$AppUserToJson(AppUser instance) => <String, dynamic>{
'avatar': instance.avatar?.toJson(),
'name': instance.name,
};
AvatarConfiguration _$AvatarConfigurationFromJson(Map<String, dynamic> json) =>
AvatarConfiguration(
json['he'] as int,
json['ha'] as int,
json['no'] as int,
json['mo'] as int,
json['ey'] as int,
);
Map<String, dynamic> _$AvatarConfigurationToJson(
AvatarConfiguration instance) =>
<String, dynamic>{
'he': instance.head,
'ha': instance.hair,
'no': instance.nose,
'mo': instance.mouth,
'ey': instance.eye,
};
and
json['avatar'],
is not what I expect.
When not extending the class
@JsonSerializable(explicitToJson: true)
class AppUser{
String name;
AvatarConfiguration? avatar;
AppUser(this.name, this.avatar);
factory AppUser.fromJson(Map<String, dynamic> json) => _$AppUserFromJson(json);
Map<String, dynamic> toJson() => _$AppUserToJson(this);
}
I get what I expect:
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
AppUser _$AppUserFromJson(Map<String, dynamic> json) => AppUser(
json['name'] as String,
json['avatar'] == null
? null
: AvatarConfiguration.fromJson(
json['avatar'] as Map<String, dynamic>),
);
Map<String, dynamic> _$AppUserToJson(AppUser instance) => <String, dynamic>{
'name': instance.name,
'avatar': instance.avatar?.toJson(),
};
...
Can anyone tell me if that is a bug or does it make sense how json_serializable is working???
Version is 6.5.3
[√] Flutter (Channel stable, 3.3.5, on Microsoft Windows [Version 10.0.19044.2130], locale de-DE)
• Flutter version 3.3.5 on channel stable at C:\SDKs\flutter_latest\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision d9111f6402 (8 days ago), 2022-10-19 12:27:13 -0700
• Engine revision 3ad69d7be3
• Dart version 2.18.2
• DevTools version 2.15.0
...