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.22k stars 1.57k forks source link

JsonCodable should use type in Xxx.fromJson/toJson as representation in json #56346

Open a14n opened 3 months ago

a14n commented 3 months ago

On decimal package someone reported an issue with JsonCodable. He tried the following code:

@JsonCodable()
class User {
  final Decimal? test;
  User(this.test);
}

As Decimal.fromJson accepts String as parameter he get an error because the generated code use a Map<String, Object?> as input.

It would be great if JsonCodable could read the type present in Xxx.fromJson and use it as serialization format.

dart-github-bot commented 3 months ago

Summary: The JsonCodable annotation currently uses Map<String, Object?> for serialization, causing issues when custom types like Decimal require specific input formats. The user suggests using the type specified in Xxx.fromJson to determine the serialization format.