@@ -26,6 +26,8 @@ Given a library `example.dart` with an `Person` class annotated with
26
26
[ ` JsonSerializable ` ] :
27
27
28
28
``` dart
29
+ import 'dart:convert';
30
+
29
31
import 'package:json_annotation/json_annotation.dart';
30
32
31
33
part 'example.g.dart';
@@ -39,7 +41,15 @@ class Person {
39
41
/// exist or is empty.
40
42
final DateTime? dateOfBirth;
41
43
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
+ });
43
53
44
54
/// Connect the generated [_$PersonFromJson] function to the `fromJson`
45
55
/// factory.
@@ -48,6 +58,23 @@ class Person {
48
58
/// Connect the generated [_$PersonToJson] function to the `toJson` method.
49
59
Map<String, dynamic> toJson() => _$PersonToJson(this);
50
60
}
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
+ }
51
78
```
52
79
53
80
Building creates the corresponding part ` example.g.dart ` :
@@ -61,12 +88,20 @@ Person _$PersonFromJson(Map<String, dynamic> json) => Person(
61
88
dateOfBirth: json['dateOfBirth'] == null
62
89
? null
63
90
: 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),
64
95
);
65
96
66
97
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
67
98
'firstName': instance.firstName,
68
99
'lastName': instance.lastName,
69
100
'dateOfBirth': instance.dateOfBirth?.toIso8601String(),
101
+ 'person': instance.person.toJson(
102
+ (value) => value.toString(),
103
+ (value) => value,
104
+ ),
70
105
};
71
106
```
72
107
@@ -199,14 +234,14 @@ targets:
199
234
[`Enum`] : https://api.dart.dev/stable/dart-core/Enum-class.html
200
235
[`int`] : https://api.dart.dev/stable/dart-core/int-class.html
201
236
[`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
210
245
[`List`] : https://api.dart.dev/stable/dart-core/List-class.html
211
246
[`Map`] : https://api.dart.dev/stable/dart-core/Map-class.html
212
247
[`num`] : https://api.dart.dev/stable/dart-core/num-class.html
0 commit comments