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

Why not integrate a annotation to rename JSON fields (like in JsonSerializable)? #1077

Closed
iqthuc opened this issue Apr 25, 2024 · 3 comments
Closed
Assignees
Labels
enhancement New feature or request needs triage

Comments

@iqthuc
Copy link

iqthuc commented Apr 25, 2024

@freezed
class Player with _$Player {
  @JsonSerializable(fieldRename: FieldRename.snake)
  const factory Player({
    final int? id,
    final String? firstName,
  }) = _Player;

  factory Player.fromJson(Map<String, dynamic> json) => _$PlayerFromJson(json);
}

I thought there would be something similar to that @ Freezed(jsonFieldRename: JsonFieldRename.snake)

@rrousselGit
Copy link
Owner

You mean rename the generated field?

That'd be different from how most people use fieldRename

@iqthuc iqthuc changed the title Why not integrate the FieldRename functionality from JsonSerializable into Freezed? Why not integrate a annotatio to rename JSON fields (like in JsonSerializable)? Apr 25, 2024
@iqthuc
Copy link
Author

iqthuc commented Apr 25, 2024

You mean rename the generated field?

That'd be different from how most people use fieldRename

sorry about that, I've rewritten the description

@iqthuc iqthuc changed the title Why not integrate a annotatio to rename JSON fields (like in JsonSerializable)? Why not integrate a annotation to rename JSON fields (like in JsonSerializable)? Apr 25, 2024
@rrousselGit
Copy link
Owner

Freezed supports writing what you did in your code-snippet.

@freezed
class Player with _$Player {
  @JsonSerializable(fieldRename: FieldRename.snake)
  const factory Player({
    final int? id,
    final String? firstName,
  }) = _Player;

  factory Player.fromJson(Map<String, dynamic> json) => _$PlayerFromJson(json);
}

Cf https://github.com/rrousselGit/freezed/?tab=readme-ov-file#deserializing-generic-classes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

2 participants