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.07k stars 1.56k forks source link

pkg:json - Support DateTime attributes #56175

Open azeldaniel opened 2 months ago

azeldaniel commented 2 months ago

Attempting to use a DateTime attribute within any @JsonCodable() annotated class throws an unsupported error. DateTime is a commonly used type in dart and other languages to represent timestamps for creation data points etc.

Consider the code:

@JsonCodable()
class Person {
  final DateTime birthday;
}

It throws the following errors:

Unable to deserialize type, it must be a native JSON type or a type with a `fromJson(Map<String, Object?> json)` constructor
Unable to serialize type, it must be a native JSON type or a type with a `Map<String, Object?> toJson()` method.

If supporting DateTime is not within the scope of this package, can you suggest a possible hack or workaround?

dart-github-bot commented 2 months ago

Summary: The pkg:json package currently doesn't support serializing and deserializing DateTime objects, resulting in errors when used within @JsonCodable() classes. The user requests support for DateTime or a workaround to handle timestamps.