Skip to content

Commit a79c6b7

Browse files
authored
Fix BigInt, DateTime, Uri JsonKey.defaultValue w/ a function (#1220)
Fixes #1219 Prepare to release v6.5.1
1 parent 0e89d96 commit a79c6b7

18 files changed

+123
-15
lines changed

json_serializable/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.5.1
2+
3+
- Fixed `BigInt`, `DateTime`, and `Uri` support for `JsonKey.defaultValue` with
4+
a function value.
5+
16
## 6.5.0
27

38
- Allow constructors to be passed to `JsonKey` parameters that support

json_serializable/lib/src/type_helpers/big_int_helper.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/element/type.dart';
66
import 'package:source_helper/source_helper.dart';
77

8+
import '../default_container.dart';
89
import '../type_helper.dart';
910
import 'to_from_string.dart';
1011

@@ -24,7 +25,7 @@ class BigIntHelper extends TypeHelper {
2425
);
2526

2627
@override
27-
String? deserialize(
28+
DefaultContainer? deserialize(
2829
DartType targetType,
2930
String expression,
3031
TypeHelperContext context,

json_serializable/lib/src/type_helpers/date_time_helper.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/element/type.dart';
66
import 'package:source_helper/source_helper.dart';
77

8+
import '../default_container.dart';
89
import '../type_helper.dart';
910
import 'to_from_string.dart';
1011

@@ -24,7 +25,7 @@ class DateTimeHelper extends TypeHelper {
2425
);
2526

2627
@override
27-
String? deserialize(
28+
DefaultContainer? deserialize(
2829
DartType targetType,
2930
String expression,
3031
TypeHelperContext context,

json_serializable/lib/src/type_helpers/map_helper.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class MapHelper extends TypeHelper<TypeHelperContextWithConfig> {
126126

127127
final toFromString = _forType(keyArg);
128128
if (toFromString != null) {
129-
keyUsage = toFromString.deserialize(keyArg, keyUsage, false, true)!;
129+
keyUsage =
130+
toFromString.deserialize(keyArg, keyUsage, false, true).toString();
130131
}
131132

132133
return '($expression $mapCast)$optionalQuestion.map( '

json_serializable/lib/src/type_helpers/to_from_string.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analyzer/dart/element/type.dart';
66
import 'package:source_gen/source_gen.dart';
77

8-
import '../utils.dart';
8+
import '../default_container.dart';
99

1010
final bigIntString = ToFromStringHelper(
1111
'BigInt.parse',
@@ -64,7 +64,7 @@ class ToFromStringHelper {
6464
return '$expression.$_toString';
6565
}
6666

67-
String? deserialize(
67+
DefaultContainer? deserialize(
6868
DartType type,
6969
String expression,
7070
bool nullable,
@@ -78,6 +78,9 @@ class ToFromStringHelper {
7878

7979
final output = '$_parse($parseParam)';
8080

81-
return nullable ? ifNullOrElse(expression, 'null', output) : output;
81+
return DefaultContainer(
82+
expression,
83+
output,
84+
);
8285
}
8386
}

json_serializable/lib/src/type_helpers/uri_helper.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/element/type.dart';
66
import 'package:source_helper/source_helper.dart';
77

8+
import '../default_container.dart';
89
import '../type_helper.dart';
910
import 'to_from_string.dart';
1011

@@ -24,7 +25,7 @@ class UriHelper extends TypeHelper {
2425
);
2526

2627
@override
27-
String? deserialize(
28+
DefaultContainer? deserialize(
2829
DartType targetType,
2930
String expression,
3031
TypeHelperContext context,

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.5.0
2+
version: 6.5.1
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

json_serializable/test/supported_types/input.type_bigint.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ part 'input.type_bigint.g.dart';
1010
class SimpleClass {
1111
final BigInt value;
1212

13+
@JsonKey(defaultValue: _defaultValueFunc)
14+
BigInt withDefault;
15+
1316
SimpleClass(
1417
this.value,
18+
this.withDefault,
1519
);
1620

1721
factory SimpleClass.fromJson(Map<String, Object?> json) =>
@@ -24,12 +28,18 @@ class SimpleClass {
2428
class SimpleClassNullable {
2529
final BigInt? value;
2630

31+
@JsonKey(defaultValue: _defaultValueFunc)
32+
BigInt? withDefault;
33+
2734
SimpleClassNullable(
2835
this.value,
36+
this.withDefault,
2937
);
3038

3139
factory SimpleClassNullable.fromJson(Map<String, Object?> json) =>
3240
_$SimpleClassNullableFromJson(json);
3341

3442
Map<String, Object?> toJson() => _$SimpleClassNullableToJson(this);
3543
}
44+
45+
BigInt _defaultValueFunc() => BigInt.parse('12345');

json_serializable/test/supported_types/input.type_bigint.g.dart

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

json_serializable/test/supported_types/input.type_datetime.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ part 'input.type_datetime.g.dart';
1010
class SimpleClass {
1111
final DateTime value;
1212

13+
@JsonKey(defaultValue: _defaultValueFunc)
14+
DateTime withDefault;
15+
1316
SimpleClass(
1417
this.value,
18+
this.withDefault,
1519
);
1620

1721
factory SimpleClass.fromJson(Map<String, Object?> json) =>
@@ -24,12 +28,18 @@ class SimpleClass {
2428
class SimpleClassNullable {
2529
final DateTime? value;
2630

31+
@JsonKey(defaultValue: _defaultValueFunc)
32+
DateTime? withDefault;
33+
2734
SimpleClassNullable(
2835
this.value,
36+
this.withDefault,
2937
);
3038

3139
factory SimpleClassNullable.fromJson(Map<String, Object?> json) =>
3240
_$SimpleClassNullableFromJson(json);
3341

3442
Map<String, Object?> toJson() => _$SimpleClassNullableToJson(this);
3543
}
44+
45+
DateTime _defaultValueFunc() => DateTime.parse('2020-01-01T00:00:00.000');

0 commit comments

Comments
 (0)