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 name is ignored #1300

Closed lucasdidur closed 1 year ago

lucasdidur commented 1 year ago

Hello,

I'm using the last version of json_serialzable, and create a class to map json keys with variables, but the json key ignores the value to generate the code

Class

@JsonSerializable()
class UsuarioModel extends Usuario {
  UsuarioModel({
    @JsonKey(name: 'name') required String nome,
    @JsonKey(name: 'rf') required String rf,
  }) : super(
          nome: nome,
          rf: rf,
        );

  factory UsuarioModel.fromJson(Map<String, dynamic> json) => _$UsuarioModelFromJson(json);
  Map<String, dynamic> toJson() => _$UsuarioModelToJson(this);
}

Aways generates the variable name to parse inside of .g.dart file

// GENERATED CODE - DO NOT MODIFY BY HAND

// ignore_for_file: implicit_dynamic_parameter

part of 'usuario.model.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

UsuarioModel _$UsuarioModelFromJson(Map<String, dynamic> json) => UsuarioModel(
      nome: json['nome'] as String,
      rf: json['rf'] as String,
    );

Map<String, dynamic> _$UsuarioModelToJson(UsuarioModel instance) =>
    <String, dynamic>{
      'nome': instance.nome,
      'rf': instance.rf,
    };

build_runner: ^2.3.3 json_annotation: ^4.8.0 json_serializable: ^6.6.1