Skip to content

Commit b56700a

Browse files
committed
rebase upstream
1 parent 8bab841 commit b56700a

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

json_serializable/README.md

+44-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Given a library `example.dart` with an `Person` class annotated with
2626
[`JsonSerializable`]:
2727

2828
```dart
29+
import 'dart:convert';
30+
2931
import 'package:json_annotation/json_annotation.dart';
3032
3133
part 'example.g.dart';
@@ -39,7 +41,15 @@ class Person {
3941
/// exist or is empty.
4042
final DateTime? dateOfBirth;
4143
42-
Person({required this.firstName, required this.lastName, this.dateOfBirth});
44+
/// An example of a custom map like type with non-string key
45+
final Person2<int, String> person;
46+
47+
Person({
48+
required this.firstName,
49+
required this.lastName,
50+
this.dateOfBirth,
51+
required this.person,
52+
});
4353
4454
/// Connect the generated [_$PersonFromJson] function to the `fromJson`
4555
/// factory.
@@ -48,6 +58,23 @@ class Person {
4858
/// Connect the generated [_$PersonToJson] function to the `toJson` method.
4959
Map<String, dynamic> toJson() => _$PersonToJson(this);
5060
}
61+
62+
const jsonKeyType = 'jsonKeyType';
63+
64+
class Person2<@jsonKeyType K, V> {
65+
Person2({required this.name, required this.id});
66+
final V name;
67+
final K id;
68+
69+
factory Person2.fromJson(Map<String, dynamic> json,
70+
K Function(Object?) fromJsonK, V Function(Object?) fromJsonV) =>
71+
Person2(id: fromJsonK(json), name: fromJsonV(json));
72+
Map<String, dynamic> toJson(
73+
String Function(K) toJsonK, Object? Function(V) toJsonV) =>
74+
{
75+
toJsonK(id): toJsonV(name),
76+
};
77+
}
5178
```
5279

5380
Building creates the corresponding part `example.g.dart`:
@@ -61,12 +88,20 @@ Person _$PersonFromJson(Map<String, dynamic> json) => Person(
6188
dateOfBirth: json['dateOfBirth'] == null
6289
? null
6390
: DateTime.parse(json['dateOfBirth'] as String),
91+
person: Person2<int, String>.fromJson(
92+
json['person'] as Map<String, dynamic>,
93+
(value) => int.parse(value as String),
94+
(value) => value as String),
6495
);
6596
6697
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
6798
'firstName': instance.firstName,
6899
'lastName': instance.lastName,
69100
'dateOfBirth': instance.dateOfBirth?.toIso8601String(),
101+
'person': instance.person.toJson(
102+
(value) => value.toString(),
103+
(value) => value,
104+
),
70105
};
71106
```
72107

@@ -199,14 +234,14 @@ targets:
199234
[`Enum`]: https://api.dart.dev/stable/dart-core/Enum-class.html
200235
[`int`]: https://api.dart.dev/stable/dart-core/int-class.html
201236
[`Iterable`]: https://api.dart.dev/stable/dart-core/Iterable-class.html
202-
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonConverter-class.html
203-
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonEnum-class.html
204-
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonKey/fromJson.html
205-
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonKey/toJson.html
206-
[`JsonKey`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonKey-class.html
207-
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonLiteral-class.html
208-
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonSerializable-class.html
209-
[`JsonValue`]: https://pub.dev/documentation/json_annotation/4.3.0/json_annotation/JsonValue-class.html
237+
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
238+
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum-class.html
239+
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
240+
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
241+
[`JsonKey`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey-class.html
242+
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonLiteral-class.html
243+
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable-class.html
244+
[`JsonValue`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonValue-class.html
210245
[`List`]: https://api.dart.dev/stable/dart-core/List-class.html
211246
[`Map`]: https://api.dart.dev/stable/dart-core/Map-class.html
212247
[`num`]: https://api.dart.dev/stable/dart-core/num-class.html

json_serializable/example/example.g.dart

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)