k-paxian / dart-json-mapper

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

Issues with deserialising Color #215

Closed ma-pe closed 1 year ago

ma-pe commented 1 year ago

Hi :) thank you for working on this library!

I was using dart-json-mapper and dart_json_mapper_flutter successfully to serialize and deserialize Colors. Something changed (and I can't pinpoint it) and now Models containing Colors are not successfully deserialized anymore. This may or may not have to do with version mismatches (new flutter version, etc.).

  dart_json_mapper: ^2.2.9
  dart_json_mapper_flutter: 2.0.1

And: I am using flutter 3.7.5.

I'll even changed my main.dart to the following sample from the readme (and adapted it to also deserialize):

import 'dart:ui' show Color;
import 'package:dart_json_mapper/dart_json_mapper.dart' show JsonMapper, jsonSerializable;
import 'package:dart_json_mapper_flutter/dart_json_mapper_flutter.dart' show flutterAdapter;

import 'main.mapper.g.dart' show initializeJsonMapper;

@jsonSerializable
class ColorfulItem {
  String name;
  Color color;

  ColorfulItem(this.name, this.color);
}

void main() {
  initializeJsonMapper(adapters: [flutterAdapter]);

  final serializedItem = JsonMapper.serialize(ColorfulItem('Item 1', Color(0x003f4f5f)));
  print(serializedItem);
  print(JsonMapper.deserialize<ColorfulItem>(serializedItem));
  // => throws `Unhandled Exception: Unable to instantiate class 'ColorfulItem'`
}

While the serialization works like a charm, the deserialization throws. This is the stacktrace:

Unhandled Exception: Unable to instantiate class 'ColorfulItem'
#0      JsonMapper._deserializeObject (mapper.dart:988)
#1      JsonMapper.deserialize (mapper.dart:53)
#2      main (main.dart:18)
#3      _runMain.<anonymous closure> (dart:ui/hooks.dart:131:23)
#4      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#5      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

The one thing I found out so far is:

Do you have any idea what I am missing?

k-paxian commented 1 year ago

Thanks for reporting this thingy. I'll take a look definitely. Not promising quick resolution, since I'm extremely busy these days.

ma-pe commented 1 year ago

Oh, it is introduced with dart_json_mapper 2.2.9. And now I understand why it suddenly broke. I used ^2.2.7 in my pubspec which allowed 2.2.9 to be installed.

So if anyone else is running into this: Pin the dependency to 2.2.7.

k-paxian commented 1 year ago

I've added your exact code as a unit test here and it appears to be working fine with the flutter 3.10.6