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

IMap.fromJson does not support custom types for keys #82

Open
urusai88 opened this issue Sep 15, 2024 · 1 comment
Open

IMap.fromJson does not support custom types for keys #82

urusai88 opened this issue Sep 15, 2024 · 1 comment
Labels
feature request New feature or request good first issue Good for newcomers

Comments

@urusai88
Copy link

map_point.dart
import 'dart:math';

typedef MapPoint = Point<int>;

class MapPointJsonConverter extends JsonConverter<MapPoint, String> {
  const MapPointJsonConverter();

  @override
  MapPoint fromJson(String json) => parseMapPoint(json);

  @override
  String toJson(MapPoint object) => '${object.x}x${object.y}';
}
entity.dart
import 'package:json_serializable/json_serializable.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';

part 'entity.g.dart';

@JsonSerializable(converters: [MapPointJsonConverter()])
class Entity {
  const Entity({required this.map});

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

  final IMap<MapPoint, int> map;
}
entity.g.dart
Entity _$EntityFromJson(Map<String, dynamic> json) =>
    Entity(
      map: json['map'] == null
          ? const IMap.empty()
          : IMap<Point<int>, int>.fromJson(
              json['map'] as Map<String, dynamic>,
              (value) =>
                  const MapPointJsonConverter().fromJson(value as String),
              (value) => (value as num).toInt(),
    );
error
Unhandled Exception: Unsupported operation: 
JSON deserialization of IMap keys of type Point<int> are not supported at the moment.
stack trace
#0 _safeKeyFromJson (package:fast_immutable_collections/src/imap/imap.dart:1558:5)
#1 new IMap.fromJson.<anonymous closure> (package:fast_immutable_collections/src/imap/imap.dart:498:50)
#2 MapBase.map (dart:collection/maps.dart:82:28)
#3 new IMap.fromJson (package:fast_immutable_collections/src/imap/imap.dart:497:12)
@marcglasberg
Copy link
Owner

Related to:

#39
#58

@marcglasberg marcglasberg added feature request New feature or request good first issue Good for newcomers labels Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants