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 non-String map keys, if there exists a compatible converter #396

Open
kevmoo opened this issue Feb 26, 2019 · 6 comments · May be fixed by #1221
Open

Allow non-String map keys, if there exists a compatible converter #396

kevmoo opened this issue Feb 26, 2019 · 6 comments · May be fixed by #1221

Comments

@kevmoo
Copy link
Collaborator

kevmoo commented Feb 26, 2019

Map<Version, int> should be doable...

@kevmoo
Copy link
Collaborator Author

kevmoo commented Apr 2, 2019

Or Map<int, int> for that matter.

@bgetsug
Copy link

bgetsug commented Jul 8, 2019

I'd really like to be able to use value objects in place of Strings to make my Maps a bit more clear. When this feature is complete will I be able to do something like the following?

@JsonSerializable()
class ContainerThing {
  Map<ThingId, int> stuff;
}

class ThingId extends StringValue {
  ThingId(String value) : super(value);
}

class StringValue extends Value<String> {
  StringValue(String value) : super(value);
}

class StringValueConverter implements JsonConverter<StringValue, String> {
  @override
  StringValue fromJson(String json) => StringValue(json);

  @override
  String toJson(StringValue object) => object.value;
}

@immutable
abstract class Value<T> {
  final T value;

  Value(this.value);

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Value &&
          runtimeType == other.runtimeType &&
          value == other.value;

  @override
  int get hashCode => value.hashCode;
}

@kevmoo
Copy link
Collaborator Author

kevmoo commented Jul 8, 2019

@bgetsug – right now, no. I've wired through support for known types.

Getting this generic for an arbitrary key type will be...interesting. 😄 I think I can pull it off, though.

@sterlingwellscaffeine
Copy link

Any update on this feature?

@kevmoo
Copy link
Collaborator Author

kevmoo commented Sep 8, 2021

Nope. Prs welcome!

@TimWhiting
Copy link

The basics seems to work for regular maps (Map<int, int>), but not for maps that implement their own toJsonK / toJsonV / fromJsonK / fromJsonV converters such as IMap<int, int> from the fast_immutable_collections package. For that sort of use case, a @jsonKeyType annotation on the toJsonK and fromJsonK could be a good API, or even directly on the K type in the generic class. I tried looking into implementing this but got stuck.

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