1

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???

derChris
  • 726
  • 8
  • 19

0 Answers0