Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question about generated .g.dart code #522

Closed
programmerAbc opened this issue Aug 6, 2019 · 1 comment
Closed

question about generated .g.dart code #522

programmerAbc opened this issue Aug 6, 2019 · 1 comment

Comments

@programmerAbc
Copy link

I have following complex model

part 'A.g.dart';

@JsonSerializable()
class A{
  B b;
  C c;
  A();
}

@JsonSerializable()
class B{
  int v;
  B();
}

@JsonSerializable()
class C{
  int v;
  C();
}

When I use json_serializable to generate A.g.dart ,I got

part of 'A.dart';

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

A _$AFromJson(Map<String, dynamic> json) {
  return A()
    ..b =
        json['b'] == null ? null : B.fromJson(json['b'] as Map<String, dynamic>)
    ..c = json['c'] == null
        ? null
        : C.fromJson(json['c'] as Map<String, dynamic>);
}

Map<String, dynamic> _$AToJson(A instance) => <String, dynamic>{
      'b': instance.b,
      'c': instance.c,
    };

B _$BFromJson(Map<String, dynamic> json) {
  return B()..v = json['v'] as int;
}

Map<String, dynamic> _$BToJson(B instance) => <String, dynamic>{
      'v': instance.v,
    };

C _$CFromJson(Map<String, dynamic> json) {
  return C()..v = json['v'] as int;
}

Map<String, dynamic> _$CToJson(C instance) => <String, dynamic>{
      'v': instance.v,
    };

everything is ok,but only one problem(maybe it is a problem just for me) is function _$AFromJson

A _$AFromJson(Map<String, dynamic> json) {
  return A()
    ..b =
        json['b'] == null ? null : B.fromJson(json['b'] as Map<String, dynamic>)
    ..c = json['c'] == null
        ? null
        : C.fromJson(json['c'] as Map<String, dynamic>);
}

it use B.fromJson and C.fromJson to decode json and create instance for member B and C in class A which makes me must implement fromJson method in both B and C, why not use _$BFromJson and _$CFromJson to decode json and create instrance for both of them? just like following code

A _$AFromJson(Map<String, dynamic> json) {
  return A()
    ..b =
        json['b'] == null ? null : _$BFromJson(json['b'] as Map<String, dynamic>)
    ..c = json['c'] == null
        ? null
        : _$CFromJson(json['c'] as Map<String, dynamic>);
}

and these implemention only need me implement fromJson method in class A , and I do not need to modify class B and class C any more which make my life better.

@kevmoo
Copy link
Collaborator

kevmoo commented Aug 24, 2020

This package tries not to reason about how serialization is done with fields. It just looks for toJson and fromJson.

This eliminates a lot of special handling in the implementation.

@kevmoo kevmoo closed this as completed Aug 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants