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

[json_codable] the object passed to fromJson is not necessarily a Map #59580

Open
cedvdb opened this issue Nov 21, 2024 · 3 comments · May be fixed by #59581
Open

[json_codable] the object passed to fromJson is not necessarily a Map #59580

cedvdb opened this issue Nov 21, 2024 · 3 comments · May be fixed by #59581
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. pkg-json The json macro package type-enhancement A request for a change that isn't a bug

Comments

@cedvdb
Copy link
Contributor

cedvdb commented Nov 21, 2024

Currently json_codable assumes that a Map is passed to the fromJson constructor when that constructor is defined by the user.

This is not supported:

@JsonCodable()
class A {
  final CustomStringSerialization customStringSerializationField;
}

class CustomStringSerialization {
  final String a;

  CustomStringSerialization(this.a);

  String toJson() => a;

  factory CustomStringSerialization.fromJson(String a) =>
      CustomStringSerialization(a);
}

Use cases:

  • deserializing values to object, for example a 'EUR' value could be deserialized to a currency object with its format method
  • it will be necessary for enum support
@dart-github-bot
Copy link
Collaborator

Summary: json_codable wrongly assumes fromJson receives a Map. The example shows a fromJson accepting a String, causing a failure.

@dart-github-bot dart-github-bot added area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 21, 2024
@lrhn lrhn added type-enhancement A request for a change that isn't a bug pkg-json The json macro package and removed type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Nov 21, 2024
@cedvdb cedvdb linked a pull request Nov 21, 2024 that will close this issue
@jakemac53
Copy link
Contributor

cc @dart-lang/core-package-admins curious for your thoughts on the design here. See the corresponding PR as well.

I am currently leaning in the favorable direction, I don't see any real downside to the approach.

@natebosch
Copy link
Member

The design looks sensible to me. I do think that serializing to objects other than Maps is a useful feature, and it doesn't look like it makes usage any more complex.

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. pkg-json The json macro package type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants