dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

[json_codable] the object passed to fromJson is not necessarily a Map #59580

Open cedvdb opened 1 day ago

cedvdb commented 1 day ago

Currently json_codable assumes that a Map is passed to the fromJson constructor when that constructor is defined by the user.

This is not supported:


@JsonCodable()
class A {
  final CustomStringSerialization customStringSerializationField;
}

class CustomStringSerialization {
  final String a;

  CustomStringSerialization(this.a);

  String toJson() => a;

  factory CustomStringSerialization.fromJson(String a) =>
      CustomStringSerialization(a);
}
dart-github-bot commented 1 day ago

Summary: json_codable wrongly assumes fromJson receives a Map. The example shows a fromJson accepting a String, causing a failure.