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

Use @JsonConverter for map keys. #1393

Open
bramp opened this issue Jan 24, 2024 · 3 comments
Open

Use @JsonConverter for map keys. #1393

bramp opened this issue Jan 24, 2024 · 3 comments

Comments

@bramp
Copy link

bramp commented Jan 24, 2024

I have a Map<Id, int> whose key is a custom class. That class is is annotated with @JsonConverter for converting it to a String. When I run json_serializable over my code, I get the error

[project]: [SEVERE] json_serializable on lib/src/cart.dart:
[project]:
[project]: Could not generate `toJson` code for `items` because of type `Id`.
[project]: Map keys must be one of: Object, dynamic, enum, String, BigInt, DateTime, int, Uri.

Now Id is not one of those types, but by default it will serialise to a String. So should this use can be supported? More info/example below:

❯ dart --version
Dart SDK version: 3.2.5 (stable) (Tue Jan 16 15:02:13 2024 +0000) on "macos_arm64"
@JsonSerializable()
final class Cart {
  final Map<Id, int> items;
}

@JsonSerializable()
@IdJsonConverter()
final class Id implements Comparable<Id> {
...
}

final class IdJsonConverter extends JsonConverter<Id, String> {
  const IdJsonConverter();

  @override
  Id fromJson(String json) {
    return Id.fromString(json);
  }

  @override
  String toJson(Id object) {
    return object.toString();
  }
}

Map keys must be one of:

@pattobrien
Copy link

I would go even further to say that if a particular type has a toJson / fromJson that returns/takes a String type, then the conversion should be implicit (or perhaps put this behind a configuration parameter?).

@BuyMyBeard
Copy link

BuyMyBeard commented Feb 6, 2025

I second this. Even trying to make a JsonConverter<Map<Id, T> Map<String, T>> won't work. The only way around this is making a specific map converter for your type. For example, I had to program this:

class QuestionIdAnswerMapJsonConverter
    implements
        JsonConverter<Map<QuestionId, Answer>,
            Map<String, Map<String, dynamic>>> {
  const QuestionIdAnswerMapJsonConverter();

  @override
  Map<QuestionId, Answer> fromJson(Map<String, Map<String, dynamic>> json) =>
      json.map((key, value) => MapEntry(
          const QuestionIdJsonConverter().fromJson(key),
          Answer.fromJson(value)));

  @override
  Map<String, Map<String, dynamic>> toJson(Map<QuestionId, Answer> object) =>
      object.map((key, value) => MapEntry(
          const QuestionIdJsonConverter().toJson(key), value.toJson()));
}

I'd like if there was feedback or talk about implementing this.

@kevmoo
Copy link
Collaborator

kevmoo commented Feb 10, 2025

I see the desire here. It's complicated to implement, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants