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

Feature request: FromJson format to local time

Open trtiger4520 opened this issue 2 months ago • 2 comments

Due to the need to display local time in the application

Want to add an option in the configuration file to allow users to choose whether to use local time

Achievable:

  • Avoid manually changing to local time every time you use it
  • Avoid modifying automatically generated files

Thanks


config:

targets:
  $default:
    builders:
      json_serializable:
        options:
          parse_local_time: true // default: false

from:

part of 'example.dart';

Person _$PersonFromJson(Map<String, dynamic> json) => Person(
      firstName: json['firstName'] as String,
      lastName: json['lastName'] as String,
      dateOfBirth: json['dateOfBirth'] == null
          ? null
          : DateTime.parse(json['dateOfBirth'] as String),
    );

to:

part of 'example.dart';

Person _$PersonFromJson(Map<String, dynamic> json) => Person(
      firstName: json['firstName'] as String,
      lastName: json['lastName'] as String,
      dateOfBirth: json['dateOfBirth'] == null
          ? null
          : DateTime.parse(json['dateOfBirth'] as String).toLocal(),
    );

trtiger4520 avatar Apr 25 '24 02:04 trtiger4520