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 extension type as Map keys if it extends one of primitive json types or has toJson() method #1406

Open
AlexeyKatsuro opened this issue Mar 10, 2024 · 0 comments

Comments

@AlexeyKatsuro
Copy link

AlexeyKatsuro commented Mar 10, 2024

Codegenerator logs error:

Could not generate fromJson code for map because of type ItemId.
Map keys must be one of: Object, dynamic, enum, String, BigInt, DateTime, int, Uri.

@JsonSerializable(explicitToJson: true)
class Item {
  Item({required this.id, required this.map});

  final ItemId id;
  
  // Problem here
  final Map<ItemId, ItemId> map; 

  factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);

  Map<String, dynamic> toJson() => _$ItemToJson(this);
}

extension type const ItemId(String id) {
  factory ItemId.fromJson(String id) {
    // Here could be some logic to parse the id
    return ItemId(id);
  }

  String toJson() {
    // Here could be some logic to convert the id to a string
    return id;
  }
}

Expected generated code

Item _$ItemFromJson(Map<String, dynamic> json) => Item(
      id: ItemId.fromJson(json['id'] as String),
      map: (json['map'] as Map<String, dynamic>).map(
        (k, e) => MapEntry(ItemId.fromJson(k as String), ItemId.fromJson(e as String)),
      ),
    );

Map<String, dynamic> _$ItemToJson(Item instance) => <String, dynamic>{
      'id': instance.id.toJson(),
      'map': instance.map.map((k, e) => MapEntry(k.toJson(), e.toJson())),
    };

Also, the case with explicitToJson: false could be tricky because in the ItemId class, toJson is just an extension method, and jsonEncode will simply unbox the String id value. This behavior is unexpected if the extension type has a toJson method.

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

No branches or pull requests

1 participant