Skip to content

Commit

Permalink
Json codable no type assumption in custom fromJson
Browse files Browse the repository at this point in the history
Closes #59581

GitOrigin-RevId: 8137077
Change-Id: I263a189a6ec181fa8a6e28d77efe20d0cec0629b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396629
Reviewed-by: Jake Macdonald <jakemac@google.com>
Commit-Queue: Jake Macdonald <jakemac@google.com>
  • Loading branch information
cedvdb authored and Commit Queue committed Dec 2, 2024
1 parent e013a4c commit 56b6e5b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/json/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.20.4

- Allow custom `fromJson` to have an arbitrary parameter type.

# 0.20.3

- Add support for DateTime, serializing it to an ISO-8601 String.
Expand Down
8 changes: 4 additions & 4 deletions pkg/json/lib/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,16 @@ mixin _FromJson on _Shared {
// Otherwise, check if `classDecl` has a `fromJson` constructor.
final constructors = await builder.constructorsOf(classDecl);
final fromJson = constructors
.firstWhereOrNull((c) => c.identifier.name == 'fromJson')
?.identifier;
.firstWhereOrNull((c) => c.identifier.name == 'fromJson');

if (fromJson != null) {
return RawCode.fromParts([
if (nullCheck != null) nullCheck,
fromJson,
fromJson.identifier,
'(',
jsonReference,
' as ',
introspectionData.jsonMapCode,
fromJson.positionalParameters.first.type.code,
')',
]);
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/json/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: >
`toJson` encoding method.
repository: https://github.com/dart-lang/sdk/tree/main/pkg/json
version: 0.20.3
version: 0.20.4
environment:
sdk: ^3.6.0-edge
dependencies:
Expand Down
138 changes: 138 additions & 0 deletions pkg/json/test/json_codable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ void main() {
'mapOfSerializableField': {
'c': {'x': 3}
},
'customStringSerializationField': 'hi',
'customNullableStringSerialization': null,
'customMapSerializationField': {
'z': 'zzz',
}
};

var a = A.fromJson(json);
Expand All @@ -38,6 +43,11 @@ void main() {
expect(a.listOfSerializableField.single.x, 1);
expect(a.setOfSerializableField.single.x, 2);
expect(a.mapOfSerializableField['c']!.x, 3);
expect(a.customStringSerializationField, CustomStringSerialization('hi'));
expect(a.customNullableStringSerialization,
equals(CustomNullableStringSerialization(null)));
expect(
a.customMapSerializationField, CustomMapSerialization({'z': 'zzz'}));

expect(a.toJson(), equals(json));
});
Expand All @@ -59,6 +69,8 @@ void main() {
'nullableMapOfSerializableField': {
'd': {'x': 3},
},
'nullableCustomStringSerializationField': 'hi',
'nullableCustomMapSerializationField': {'z': 'zzz'},
};

var b = B.fromJson(json);
Expand All @@ -72,6 +84,10 @@ void main() {
expect(b.nullableListOfSerializableField!.single.x, 1);
expect(b.nullableSetOfSerializableField!.single.x, 2);
expect(b.nullableMapOfSerializableField!['d']!.x, 3);
expect(b.nullableCustomStringSerializationField,
CustomStringSerialization('hi'));
expect(b.nullableCustomMapSerializationField,
CustomMapSerialization({'z': 'zzz'}));

expect(b.toJson(), equals(json));
});
Expand All @@ -87,6 +103,8 @@ void main() {
'nullableListOfSerializableField': null,
'nullableSetOfSerializableField': null,
'nullableMapOfSerializableField': null,
'nullableCustomStringSerializationField': null,
'nullableCustomMapSerializationField': null,
});
expect(b.nullableBoolField, null);
expect(b.nullableStringField, null);
Expand All @@ -97,6 +115,8 @@ void main() {
expect(b.nullableListOfSerializableField, null);
expect(b.nullableMapOfSerializableField, null);
expect(b.nullableSetOfSerializableField, null);
expect(b.nullableCustomStringSerializationField, null);
expect(b.nullableCustomMapSerializationField, null);

expect(b.toJson(), isEmpty);
});
Expand All @@ -112,6 +132,8 @@ void main() {
expect(b.nullableListOfSerializableField, null);
expect(b.nullableMapOfSerializableField, null);
expect(b.nullableSetOfSerializableField, null);
expect(b.nullableCustomStringSerializationField, null);
expect(b.nullableCustomMapSerializationField, null);

expect(b.toJson(), isEmpty);
});
Expand Down Expand Up @@ -140,6 +162,11 @@ void main() {
null,
{'a': 1, 'b': null},
],
'listOfCustomStringSerializables': [null, 'hi'],
'listOfCustomMapSerializables': [
null,
{'z': 'zzz'},
],
'setOfNullableDates': [
null,
'2024-11-12T03:42:29.108308',
Expand All @@ -159,6 +186,11 @@ void main() {
'b': null,
},
],
'setOfCustomStringSerializables': [null, 'hi'],
'setOfCustomMapSerializables': [
null,
{'z': 'zzz'}
],
'mapOfNullableDates': {
'a': '2024-11-13T03:42:29.108308',
'b': null,
Expand All @@ -175,6 +207,14 @@ void main() {
'a': [null, 3],
'b': null,
},
'mapOfCustomStringSerializables': {
'a': null,
'b': 'hi',
},
'mapOfCustomMapSerializables': {
'a': null,
'b': {'z': 'zzz'},
},
};

var e = E.fromJson(json);
Expand All @@ -189,6 +229,13 @@ void main() {
null,
{'a': 1, 'b': null},
]));
expect(e.listOfCustomStringSerializables.first, null);
expect(e.listOfCustomStringSerializables[1],
CustomStringSerialization('hi'));
expect(e.listOfCustomMapSerializables.first, null);
expect(e.listOfCustomMapSerializables[1],
CustomMapSerialization({'z': 'zzz'}));

expect(e.setOfNullableDates,
equals([null, DateTime.parse('2024-11-12T03:42:29.108308')]));
expect(e.setOfNullableInts, equals({null, 2}));
Expand All @@ -203,6 +250,13 @@ void main() {
'b': null,
},
}));
expect(e.setOfCustomStringSerializables.first, null);
expect(e.setOfCustomStringSerializables.toList()[1],
CustomStringSerialization('hi'));
expect(e.setOfCustomMapSerializables.first, null);
expect(e.setOfCustomMapSerializables.toList()[1],
CustomMapSerialization({'z': 'zzz'}));

expect(
e.mapOfNullableDates,
equals({
Expand All @@ -222,6 +276,12 @@ void main() {
'a': {null, 3},
'b': null,
});
expect(e.mapOfCustomStringSerializables['a'], null);
expect(e.mapOfCustomStringSerializables['b'],
CustomStringSerialization('hi'));
expect(e.mapOfCustomMapSerializables['a'], null);
expect(e.mapOfCustomMapSerializables['b'],
CustomMapSerialization({'z': 'zzz'}));

expect(e.toJson(), equals(json));
});
Expand Down Expand Up @@ -257,6 +317,12 @@ class A {
final Set<C> setOfSerializableField;

final Map<String, C> mapOfSerializableField;

final CustomStringSerialization customStringSerializationField;

final CustomMapSerialization customMapSerializationField;

final CustomNullableStringSerialization customNullableStringSerialization;
}

@JsonCodable()
Expand All @@ -278,6 +344,10 @@ class B {
final Set<C>? nullableSetOfSerializableField;

final Map<String, C>? nullableMapOfSerializableField;

final CustomStringSerialization? nullableCustomStringSerializationField;

final CustomMapSerialization? nullableCustomMapSerializationField;
}

@JsonCodable()
Expand All @@ -300,6 +370,10 @@ class E {

final List<Map<String, int?>?> listOfNullableMapsOfNullableInts;

final List<CustomStringSerialization?> listOfCustomStringSerializables;

final List<CustomMapSerialization?> listOfCustomMapSerializables;

final Set<DateTime?> setOfNullableDates;

final Set<int?> setOfNullableInts;
Expand All @@ -308,16 +382,80 @@ class E {

final Set<Map<String, int?>?> setOfNullableMapsOfNullableInts;

final Set<CustomStringSerialization?> setOfCustomStringSerializables;

final Set<CustomMapSerialization?> setOfCustomMapSerializables;

final Map<String, DateTime?> mapOfNullableDates;

final Map<String, int?> mapOfNullableInts;

final Map<String, C?> mapOfNullableSerializables;

final Map<String, Set<int?>?> mapOfNullableSetsOfNullableInts;

final Map<String, CustomStringSerialization?> mapOfCustomStringSerializables;

final Map<String, CustomMapSerialization?> mapOfCustomMapSerializables;
}

@JsonCodable()
class F {
final int fieldWithDollarSign$;
}

class CustomStringSerialization {
final String a;

CustomStringSerialization(this.a);

String toJson() => a;

factory CustomStringSerialization.fromJson(String a) =>
CustomStringSerialization(a);

@override
bool operator ==(Object other) =>
other is CustomStringSerialization && a == other.a;

@override
int get hashCode => a.hashCode;
}

class CustomNullableStringSerialization {
final String? a;

CustomNullableStringSerialization(this.a);

String? toJson() => a;

factory CustomNullableStringSerialization.fromJson(String? a) =>
CustomNullableStringSerialization(a);

@override
bool operator ==(Object other) =>
other is CustomNullableStringSerialization && a == other.a;

@override
int get hashCode => a.hashCode;
}

class CustomMapSerialization {
final Map<String, dynamic> a;

CustomMapSerialization(this.a);

Map<String, dynamic> toJson() => a;

factory CustomMapSerialization.fromJson(Map<String, dynamic> a) =>
CustomMapSerialization(a);

@override
bool operator ==(Object other) {
return other is CustomMapSerialization &&
a.toString() == other.a.toString();
}

@override
int get hashCode => a.hashCode;
}

0 comments on commit 56b6e5b

Please sign in to comment.