dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Support fast_immutable_collections

Open point-source opened this issue 1 year ago • 2 comments

First off, thanks for making the best serialization library available. It's been a painful road getting here. I'm currently migrating my code over from freezed + json_serializable to dart_mappable + fast_immutable_collections.

I noticed that dart_mappable does not appear to natively support FIC. Obviously I could create a custom mapper for this but I just wanted to open a ticket since FIC already supports serialization as documented here.

I'm hoping it's rather trivial to enable dart_mappable to detect and use these methods.

Edit: For reference, here is the custom mapper I am currently using:

/// Enables dart_mappable library to support IMap from fast_immutable_collections
class IMapMapper extends BaseMapper<IMap> {
  @override
  Function decoder = <Key, Val>(value) {
    final r = IMap<Key, Val>(Mapper.fromValue(value));

    return r;
  };

  @override
  Function encoder = (IMap self) {
    final r = Mapper.toValue(self.unlock);

    return r;
  };

  @override
  Function get typeFactory => <Key, Val>(f) => f<IMap<Key, Val>>();
}

point-source avatar Sep 16 '22 15:09 point-source

Thanks 🙏

So what we could do is let the builder automatically detect classes that have both fromJson and toJson methods and then generate a mapper for those. That would probably work but isn't quite trivial. I have to see when got the time, also because it works fine right now with custom mappers so it's not urgent.

For reference, this would be the related code from json_serializable that does this: https://github.com/google/json_serializable.dart/blob/master/json_serializable/lib/src/type_helpers/json_helper.dart

schultek avatar Sep 18 '22 12:09 schultek

Detecting serialization methods would be a much more dynamic solution and would possibly solve support for much more than FIC. That said, it's certainly not trivial to make something so generic as that. Especially once you consider that people do not consistently use fromJson and fromMap to always mean String vs Map, etc. I see them confused often so it would need to check/detect that as well.

I'll take a look at that link you've provided and see if I can get some insight. Thanks!

point-source avatar Sep 19 '22 18:09 point-source

I finally published this as part of prerelease 2.0.0-dev.8

schultek avatar Jan 02 '23 20:01 schultek