Skip to content

Commit fad91d9

Browse files
authored
Feature: add support for JsonSerializable.dateTimeUtc (#1557)
Fixes #1371
1 parent 3d4dc60 commit fad91d9

19 files changed

Lines changed: 80 additions & 22 deletions

json_annotation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.11.0-wip
2+
3+
- Add `JsonSerializable.dateTimeUtc` configuration option.
4+
([#1371](https://github.com/google/json_serializable.dart/issues/1371))
5+
16
## 4.10.0
27

38
- Support `JsonKey` annotation on constructor parameters.

json_annotation/lib/src/json_serializable.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ class JsonSerializable {
129129
/// ```
130130
final bool? createToJson;
131131

132+
/// Whether the generator should use UTC time for [DateTime] fields.
133+
///
134+
/// If `true`, all [DateTime] fields are written to JSON in UTC using
135+
/// `DateTime.toUtc().toIso8601String()`.
136+
/// If `false` (the default), `DateTime.toIso8601String()` is used.
137+
final bool? dateTimeUtc;
138+
132139
/// If `false` (the default), then the generated `FromJson` function will
133140
/// ignore unrecognized keys in the provided JSON [Map].
134141
///
@@ -283,6 +290,7 @@ class JsonSerializable {
283290
this.converters,
284291
this.genericArgumentFactories,
285292
this.createPerFieldToJson,
293+
this.dateTimeUtc,
286294
});
287295

288296
factory JsonSerializable.fromJson(Map<String, dynamic> json) =>

json_annotation/lib/src/json_serializable.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_annotation/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_annotation
2-
version: 4.10.0
2+
version: 4.11.0-wip
33
description: >-
44
Classes and helper functions that support JSON code generation via the
55
`json_serializable` package.

json_serializable/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## 6.12.1-wip
1+
## 6.13.0-wip
22

3+
- Support `JsonSerializable.dateTimeUtc` configuration option.
4+
([#1371](https://github.com/google/json_serializable.dart/issues/1371))
35
- Remove `json_schema` from `dependencies` (it is only used in tests).
46

57
## 6.12.0

json_serializable/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ targets:
319319
create_json_schema: false
320320
create_per_field_to_json: false
321321
create_to_json: true
322+
date_time_utc: false
322323
disallow_unrecognized_keys: false
323324
explicit_to_json: false
324325
field_rename: none
@@ -350,15 +351,15 @@ targets:
350351
[`Enum`]: https://api.dart.dev/dart-core/Enum-class.html
351352
[`int`]: https://api.dart.dev/dart-core/int-class.html
352353
[`Iterable`]: https://api.dart.dev/dart-core/Iterable-class.html
353-
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonConverter-class.html
354-
[`JsonEnum.valueField`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonEnum/valueField.html
355-
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonEnum-class.html
356-
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonKey/fromJson.html
357-
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonKey/toJson.html
358-
[`JsonKey`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonKey-class.html
359-
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonLiteral-class.html
360-
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonSerializable-class.html
361-
[`JsonValue`]: https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonValue-class.html
354+
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
355+
[`JsonEnum.valueField`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum/valueField.html
356+
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum-class.html
357+
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
358+
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
359+
[`JsonKey`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey-class.html
360+
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonLiteral-class.html
361+
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable-class.html
362+
[`JsonValue`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonValue-class.html
362363
[`List`]: https://api.dart.dev/dart-core/List-class.html
363364
[`Map`]: https://api.dart.dev/dart-core/Map-class.html
364365
[`num`]: https://api.dart.dev/dart-core/num-class.html

json_serializable/lib/src/check_dependencies.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const _annotationPkgName = 'json_annotation';
1515
final _supportLanguageRange = VersionConstraint.parse(
1616
supportedLanguageConstraint,
1717
);
18-
final requiredJsonAnnotationMinVersion = Version.parse('4.10.0');
18+
final requiredJsonAnnotationMinVersion = Version.parse('4.11.0-0');
1919

2020
Future<void> pubspecHasRightVersion(BuildStep buildStep) async {
2121
final segments = buildStep.inputId.pathSegments;

json_serializable/lib/src/settings.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Settings {
5252
/// If [typeHelpers] is not provided, the built-in helpers are used:
5353
/// [BigIntHelper], [DateTimeHelper], [DurationHelper], [JsonHelper], and
5454
/// [UriHelper].
55-
Settings({JsonSerializable? config, List<TypeHelper>? typeHelpers})
55+
Settings({required JsonSerializable? config, List<TypeHelper>? typeHelpers})
5656
: config = config != null
5757
? ClassConfig.fromJsonSerializable(config)
5858
: ClassConfig.defaults,

json_serializable/lib/src/type_helpers/config_types.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ClassConfig {
6161
final bool createJsonKeys;
6262
final bool createPerFieldToJson;
6363
final bool createJsonSchema;
64+
final bool dateTimeUtc;
6465
final bool disallowUnrecognizedKeys;
6566
final bool explicitToJson;
6667
final FieldRename fieldRename;
@@ -80,6 +81,7 @@ class ClassConfig {
8081
required this.createJsonKeys,
8182
required this.createPerFieldToJson,
8283
required this.createJsonSchema,
84+
required this.dateTimeUtc,
8385
required this.disallowUnrecognizedKeys,
8486
required this.explicitToJson,
8587
required this.fieldRename,
@@ -108,6 +110,7 @@ class ClassConfig {
108110
createFactory:
109111
config.createFactory ?? ClassConfig.defaults.createFactory,
110112
createToJson: config.createToJson ?? ClassConfig.defaults.createToJson,
113+
dateTimeUtc: config.dateTimeUtc ?? ClassConfig.defaults.dateTimeUtc,
111114
ignoreUnannotated:
112115
config.ignoreUnannotated ?? ClassConfig.defaults.ignoreUnannotated,
113116
explicitToJson:
@@ -121,7 +124,6 @@ class ClassConfig {
121124
disallowUnrecognizedKeys:
122125
config.disallowUnrecognizedKeys ??
123126
ClassConfig.defaults.disallowUnrecognizedKeys,
124-
// TODO typeConverters = []
125127
);
126128

127129
/// An instance of [JsonSerializable] with all fields set to their default
@@ -136,6 +138,7 @@ class ClassConfig {
136138
createJsonKeys: false,
137139
createPerFieldToJson: false,
138140
createJsonSchema: false,
141+
dateTimeUtc: false,
139142
disallowUnrecognizedKeys: false,
140143
explicitToJson: false,
141144
fieldRename: FieldRename.none,
@@ -160,6 +163,7 @@ class ClassConfig {
160163
genericArgumentFactories: genericArgumentFactories,
161164
fieldRename: fieldRename,
162165
disallowUnrecognizedKeys: disallowUnrecognizedKeys,
166+
dateTimeUtc: dateTimeUtc,
163167
// TODO typeConverters = []
164168
);
165169
}

json_serializable/lib/src/type_helpers/date_time_helper.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import '../default_container.dart';
99
import '../type_helper.dart';
1010
import 'to_from_string.dart';
1111

12-
class DateTimeHelper extends TypeHelper {
12+
class DateTimeHelper extends TypeHelper<TypeHelperContextWithConfig> {
1313
const DateTimeHelper();
1414

1515
@override
1616
String? serialize(
1717
DartType targetType,
1818
String expression,
19-
TypeHelperContext context,
20-
) => dateTimeString.serialize(
19+
TypeHelperContextWithConfig context,
20+
) => context._dateTimeHelper.serialize(
2121
targetType,
2222
expression,
2323
targetType.isNullableType,
@@ -36,3 +36,8 @@ class DateTimeHelper extends TypeHelper {
3636
false,
3737
);
3838
}
39+
40+
extension on TypeHelperContextWithConfig {
41+
ToFromStringHelper get _dateTimeHelper =>
42+
config.dateTimeUtc ? dateTimeUtcString : dateTimeString;
43+
}

0 commit comments

Comments
 (0)