Open trtiger4520 opened 2 months ago
Avoid manually changing to local time every time you use it Avoid modifying automatically generated files
This is easily solvable by creating your own JsonConverter
as shown:
import 'package:json_annotation/json_annotation.dart';
part 'date_converter.g.dart';
class LocalDateConverter implements JsonConverter<DateTime, String> {
const LocalDateConverter();
@override
DateTime fromJson(String value) {
return DateTime.parse(value).toLocal(); // implement your custom `fromJson` logic here
}
@override
String toJson(DateTime value) {
return value.toUtc().toIso8601String(); // implement your custom `toJson` logic here
}
}
@JsonSerializable()
class Person {
final String firstName;
final String lastName;
@LocalDateConverter() // annotate the field requiring custom serialization logic
final DateTime dateOfBirth;
const Person({
required this.firstName,
required this.lastName,
required this.dateOfBirth,
});
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}
Does that solve your problem?
@pattobrien Thanks Reply
I understand that using JsonConverter
can solve the conversion problem
However, my situation here is: the current project uses swagger_dart_code_generator
, which is also part of the automatic generation. At that time, I looked at the contents of the files on both sides and found no better solution, so I came up with this idea.
I'll look for other solutions
in addition
I forked this repository and tried to adjust it, but found that ToFromStringHelper
is placed in the final variable of the file. It seems that bringing config into the affected content will require adjusting a lot of structural issues.
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:
Thanks
config:
from:
to: