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

JsonCodable macro field names #55811

Open
hanskokx opened this issue May 22, 2024 · 6 comments
Open

JsonCodable macro field names #55811

hanskokx opened this issue May 22, 2024 · 6 comments
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-json The json macro package

Comments

@hanskokx
Copy link

The JsonCodable macro is a colossal step in the right direction, however it lacks an important feature which should be considered. Often, when working with serialized data (in particular, data returned from an API), the field names may differ from the appropriate Dart names. For instance, I've worked with APIs (particularly with Python backends) that would return JSON that looks like this:

{
  "my_value": "some data"
}

When using Freezed or JsonSerializable, I could simply annotate the Dart object as thus:

@freezed
class AccessToken with _$AccessToken {
  const factory AccessToken({
    @JsonKey(name: 'access_token') required String accessToken,
    @JsonKey(name: 'refresh_token') required String refreshToken,
  }) = _AccessToken;

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

This allows me to map the snake case JSON value to my camel case Dart objects.

@jakemac53
Copy link
Contributor

Yes, it lacks any configurability at all right now. This is because the metadata introspection APIs for macros are not yet implemented (and we are hashing out the details still).

Once it is well supported, we will certainly add this feature (as well as some other configuration options).

@jakemac53 jakemac53 transferred this issue from dart-lang/language May 22, 2024
@jakemac53 jakemac53 added pkg-json The json macro package area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature labels May 22, 2024
@FarisArmoush
Copy link

There is a solution for this when all your data model's keys follow the same naming convention, which is creating a build.yaml folder in the root of your project.

targets:
  $default:
    builders:
      json_serializable:
        options:
          field_rename: snake

It also would be nice if we could configure each model's keys naming convention separately.

@jakemac53
Copy link
Contributor

@FarisArmoush this issue is about the JsonCodable macro, which is different from the JsonSerializable builder (which is a code generator based on build_runner, not a macro).

@GeceGibi
Copy link

any updates about this feature ? Dart 3.5.1 is live and i want to use macros intead of any build_runner libraries.

@mraleph
Copy link
Member

mraleph commented Aug 22, 2024

any updates about this feature ? Dart 3.5.1 is live and i want to use macros intead of any build_runner libraries.

Macros is an experimental unreleased feature provided as an early preview to solicit feedback. You should not rely on it (or any other package which depends on it) in production. It can change, and break and even be entirely removed without any notice. There is no timeline for it.

@GeceGibi
Copy link

Sadge. I know that, but i want to use it anyway. Thanks for info btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-json The json macro package
Projects
Development

No branches or pull requests

5 participants