json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

[Suggestion] Add a project wide option and a @JsonKey option to customize how far to parse DateTime

Open noriHanda opened this issue 1 year ago • 9 comments

Why

Sometimes one wants to parse dart's DateTime object only to Date instead of microseconds, but json_serializable does not have an option to specify this. This applies on both toJson and fromJson.

At the same time, how far one wants to parse DateTime may vary on every usecase.

Suggestion

  • Add a project wide option to specify how far to be parsed such as below in build.yaml or pubspec.yaml:
targets:
  $default:
    builders:
      json_serializable:
        options:
          date_time_parse: "toSeconds" # or "toDays" or "toHours" etc.
  • Add an option to set on each field member as such:
@JsonSerializable()
class User {
  User(this.name, this.email);

  String name;
  String email;
  @JsonKey(dateTimeToParse: "toSeconds") DateTime date;

  Map<String, dynamic> toJson() => _$UserToJson(this);
}

noriHanda avatar May 12 '23 01:05 noriHanda

Sounds like something that can be handled with a custom converter that will just drop the precision as you wish.

Can be done like so:

const myCustomAnnotation = JsonSerializable(
  converters: [SecondsDateTimeConverter()],
);

then use @myCustomAnnotation.

To me a setting such as dateTimeToParse is way too specific. I doubt it's json_serializable's goal to provide customization to supported serialized types. If you want different serialization it sounds like a converter usecase.

shilangyu avatar May 16 '23 18:05 shilangyu

To me this setting is similar to options such as field_rename which can be customized with both build.yaml and @JsonKey. What do you think?

noriHanda avatar May 17 '23 03:05 noriHanda

@noriHanda – the difference is that you'd need create some model for specifying and configuring arbitrary code that json_serializable doesn't already understand. That gets REALLY tricky!

kevmoo avatar May 17 '23 04:05 kevmoo

@kevmoo Oh so it's more of a challenge on implementation rather than the idea. Good to know. Thx for the comment :)

noriHanda avatar May 17 '23 04:05 noriHanda

It's a GREAT idea. If it was easy, I would have done it already. 😄

kevmoo avatar May 17 '23 04:05 kevmoo

I wish I had enough knowledge and skills...

noriHanda avatar May 17 '23 04:05 noriHanda

@noriHanda – you'll get there. But jumping in on this package would be CRAZY. I wrote most of it and it's still hard for me to tweak.

kevmoo avatar May 17 '23 04:05 kevmoo

I see. That does sound super hard indeed.

noriHanda avatar May 17 '23 04:05 noriHanda

@noriHanda First of all, if this feature will be implemented it's better to pass @JsonKey a DateFormat.

But I think its unneeded complexity because you can implement it yourself: https://pub.dev/packages/json_serializable#custom-types-and-custom-encoding

Just write your date special parser.

moshe5745 avatar May 21 '23 07:05 moshe5745