k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
399 stars 33 forks source link

"It seems your class 'Uint8List' has not been annotated with @jsonSerializable" error when using `Uint8ListConverter` #184

Closed DetachHead closed 2 years ago

DetachHead commented 2 years ago
import 'dart:typed_data';

import 'package:dart_json_mapper/dart_json_mapper.dart';

import 'main.mapper.g.dart';

void main() {
  initializeJsonMapper(
    adapters: [
      const JsonMapperAdapter(
        converters: {
          Uint8List: uint8ListConverter,
        },
      ),
    ],
  );
  final foo = JsonMapper.serialize(Uint8List(100));
}
E/flutter (10138): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: It seems your class 'Uint8List' has not been annotated with @jsonSerializable
E/flutter (10138): #0      JsonMapper._serializeObject (package:dart_json_mapper/src/mapper.dart:797:9)
E/flutter (10138): #1      JsonMapper.serialize (package:dart_json_mapper/src/mapper.dart:27:27)
E/flutter (10138): #2      main (package:project/main.dart:17:26)
E/flutter (10138): #3      main (file:///C:/Users/amogus/IdeaProjects/project/.dart_tool/flutter_build/generated_main.dart:190:42)
E/flutter (10138): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:128:38)
E/flutter (10138): #5      _rootRun (dart:async/zone.dart:1426:13)
E/flutter (10138): #6      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter (10138): #7      _runZoned (dart:async/zone.dart:1861:10)
E/flutter (10138): #8      runZonedGuarded (dart:async/zone.dart:1849:12)
E/flutter (10138): #9      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:126:5)
E/flutter (10138): #10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter (10138): #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
E/flutter (10138): 
k-paxian commented 2 years ago

Hm, there is a green unit test on that converter here

And it should be working out of the box, w/o adding any adapters.

Can you elaborate more on your use case?

DetachHead commented 2 years ago

hmm, it seems to work in that case, but not when attempting to serialize a List<Uint8List> or an instance of the Uint8List class itself (ie. not wrapped by anything):

void main() {
  initializeJsonMapper(
    adapters: [
      JsonMapperAdapter(
        valueDecorators: {
          typeOf<List<Uint8List>>(): (final value) =>
              (value as List).cast<Uint8List>(),
        },
      ),
    ],
  );
  final foo = JsonMapper.serialize(Foo(Uint8List(100))); //works (same scenario as that test)
  final bar = JsonMapper.serialize(Bar([Uint8List(100)])); //fails
  final baz = JsonMapper.serialize(Uint8List(100)); //fails
}

Can you elaborate more on your use case?

i'm trying to store a list of images. maybe this isn't the best way to do that but it's what i've settled on for now

k-paxian commented 2 years ago

Well, you will be the first real user of that converter, that's good, since we could polish cases around that. Thanks for reaching out 😃

k-paxian commented 2 years ago

should be ok now both use cases you've had. With the version 2.2.3