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

Allow extra non-serializable fields to be passed into fromJson and ignored in toJson #780

Open
hacker1024 opened this issue Dec 31, 2020 · 0 comments · May be fixed by #783
Open

Allow extra non-serializable fields to be passed into fromJson and ignored in toJson #780

hacker1024 opened this issue Dec 31, 2020 · 0 comments · May be fixed by #783

Comments

@hacker1024
Copy link

I have a User object, which is deserialized from JSON in an API response:

@JsonSerializable()
class User {
  final String authToken;
  final String username;
  final String profilePictureUrl;

  const User({
    required this.authToken,
    required this.username,
    required this.profilePictureUrl,
  });

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  
  static Future<User> login(String email, String password) async {
    final responseJson = await api.login(email, password);
    return User.fromJson(responseJson);
  }
}

Occasionally, the authToken used in API calls may be invalidated. If this happens, I need to reauthenticate the user.
To do this, I need the user's credentials. Ideally, I'd like to store these credentials in the User object when it's created so I can use them later. As the credentials are not included in the API response, I'd love to do something like this:

@JsonSerializable()
class User {
  @JsonKey(extra: true)
  final String email;
  @JsonKey(extra: true)
  final String password;

  final String authToken;
  final String username;
  final String profilePictureUrl;

  const User({
    required this.email,
    required this.password,
    required this.authToken,
    required this.username,
    required this.profilePictureUrl,
  });

  factory User.fromJson(
    Map<String, dynamic> json, {
    required String email,
    required String password,
  }) =>
      _$UserFromJson(
        json,
        email: email,
        password: password,
      );

  static Future<User> login(String email, String password) async {
    final responseJson = await api.login(email, password);
    return User.fromJson(
      responseJson,
      email: email,
      password: password,
    );
  }
}

As you can see, the use of a JsonKey argument like 'extra' would allow the additional values to be passed to the generated deserialisation function, which in turn would pass them through to the constructor unmodified.

Upon serialisation, the extra fields can be ignored, as they're application-specific and not part of the API spec.

My SDK version, as requested
Flutter 1.26.0-1.0.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 63062a6443 (3 weeks ago) • 2020-12-13 23:19:13 +0800
Engine • revision 4797b06652
Tools • Dart 2.12.0 (build 2.12.0-141.0.dev)
hacker1024 added a commit to hacker1024/json_serializable.dart that referenced this issue Feb 6, 2021
hacker1024 added a commit to hacker1024/json_serializable.dart that referenced this issue Feb 17, 2021
hacker1024 added a commit to hacker1024/json_serializable.dart that referenced this issue Mar 8, 2021
hacker1024 added a commit to hacker1024/json_serializable.dart that referenced this issue Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant