trphoenix
2018-11-14 86910df0578149bf09eb93cf00cc6e7ac83fa3b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
 
//ignore_for_file: avoid_unused_constructor_parameters, prefer_initializing_formals
import 'package:json_annotation/json_annotation.dart';
 
import 'annotation.dart';
 
part 'checked_test_input.dart';
part 'configuration_input.dart';
part 'default_value_input.dart';
part 'field_namer_input.dart';
part 'generic_test_input.dart';
part 'inheritance_test_input.dart';
part 'json_converter_test_input.dart';
part 'setter_test_input.dart';
part 'to_from_json_test_input.dart';
 
@ShouldThrow('Generator cannot target `theAnswer`.',
    'Remove the JsonSerializable annotation from `theAnswer`.')
@JsonSerializable()
const theAnswer = 42;
 
@ShouldThrow('Generator cannot target `annotatedMethod`.',
    'Remove the JsonSerializable annotation from `annotatedMethod`.')
@JsonSerializable()
void annotatedMethod() => null;
 
@ShouldGenerate(r'''
Person _$PersonFromJson(Map<String, dynamic> json) {
  return Person()
    ..firstName = json['firstName'] as String
    ..lastName = json['lastName'] as String
    ..height = json['h'] as int
    ..dateOfBirth = json['dateOfBirth'] == null
        ? null
        : DateTime.parse(json['dateOfBirth'] as String)
    ..dynamicType = json['dynamicType']
    ..varType = json['varType']
    ..listOfInts = (json['listOfInts'] as List)?.map((e) => e as int)?.toList();
}
 
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
      'firstName': instance.firstName,
      'lastName': instance.lastName,
      'h': instance.height,
      'dateOfBirth': instance.dateOfBirth?.toIso8601String(),
      'dynamicType': instance.dynamicType,
      'varType': instance.varType,
      'listOfInts': instance.listOfInts
    };
''')
@JsonSerializable()
class Person {
  String firstName, lastName;
  @JsonKey(name: 'h')
  int height;
  DateTime dateOfBirth;
  dynamic dynamicType;
  //ignore: prefer_typing_uninitialized_variables
  var varType;
  List<int> listOfInts;
}
 
@ShouldGenerate(r'''
Order _$OrderFromJson(Map<String, dynamic> json) {
  return Order(json['height'] as int, json['firstName'] as String,
      json['lastName'] as String)
    ..dateOfBirth = json['dateOfBirth'] == null
        ? null
        : DateTime.parse(json['dateOfBirth'] as String);
}
 
Map<String, dynamic> _$OrderToJson(Order instance) => <String, dynamic>{
      'firstName': instance.firstName,
      'lastName': instance.lastName,
      'height': instance.height,
      'dateOfBirth': instance.dateOfBirth?.toIso8601String()
    };
''')
@JsonSerializable()
class Order {
  final String firstName, lastName;
  int height;
  DateTime dateOfBirth;
 
  Order(this.height, String firstName, [this.lastName]) : firstName = firstName;
}
 
@ShouldGenerate(r'''
FinalFields _$FinalFieldsFromJson(Map<String, dynamic> json) {
  return FinalFields(json['a'] as int);
}
 
Map<String, dynamic> _$FinalFieldsToJson(FinalFields instance) =>
    <String, dynamic>{'a': instance.a};
''')
@JsonSerializable()
class FinalFields {
  final int a;
  int get b => 4;
 
  FinalFields(this.a);
}
 
@ShouldGenerate(r'''
FinalFieldsNotSetInCtor _$FinalFieldsNotSetInCtorFromJson(
    Map<String, dynamic> json) {
  return FinalFieldsNotSetInCtor();
}
 
Map<String, dynamic> _$FinalFieldsNotSetInCtorToJson(
        FinalFieldsNotSetInCtor instance) =>
    <String, dynamic>{};
''')
@JsonSerializable()
class FinalFieldsNotSetInCtor {
  final int a = 1;
 
  FinalFieldsNotSetInCtor();
}
 
@ShouldGenerate(r'''
SetSupport _$SetSupportFromJson(Map<String, dynamic> json) {
  return SetSupport((json['values'] as List)?.map((e) => e as int)?.toSet());
}
 
Map<String, dynamic> _$SetSupportToJson(SetSupport instance) =>
    <String, dynamic>{'values': instance.values?.toList()};
''')
@JsonSerializable()
class SetSupport {
  final Set<int> values;
 
  SetSupport(this.values);
}
 
@JsonSerializable(createToJson: false)
class FromJsonOptionalParameters {
  final ChildWithFromJson child;
 
  FromJsonOptionalParameters(this.child);
}
 
class ChildWithFromJson {
  ChildWithFromJson.fromJson(json, {initValue = false});
}
 
@JsonSerializable()
class ParentObject {
  int number;
  String str;
  ChildObject child;
}
 
@JsonSerializable()
class ChildObject {
  int number;
  String str;
}
 
@JsonSerializable()
class ParentObjectWithChildren {
  int number;
  String str;
  List<ChildObject> children;
}
 
@JsonSerializable()
class ParentObjectWithDynamicChildren {
  int number;
  String str;
  List<dynamic> children;
}
 
@JsonSerializable()
class UnknownCtorParamType {
  int number;
 
  // ignore: undefined_class, field_initializer_not_assignable
  UnknownCtorParamType(Bob number) : number = number;
}
 
@JsonSerializable()
class UnknownFieldType {
  // ignore: undefined_class
  Bob number;
}
 
@JsonSerializable(createFactory: false)
class UnknownFieldTypeToJsonOnly {
  // ignore: undefined_class
  Bob number;
}
 
@JsonSerializable()
class UnknownFieldTypeWithConvert {
  @JsonKey(fromJson: _everythingIs42, toJson: _everythingIs42)
  // ignore: undefined_class
  Bob number;
}
 
dynamic _everythingIs42(Object input) => 42;
 
@JsonSerializable(createFactory: false)
class NoSerializeFieldType {
  Stopwatch watch;
}
 
@JsonSerializable(createToJson: false)
class NoDeserializeFieldType {
  Stopwatch watch;
}
 
@JsonSerializable(createFactory: false)
class NoSerializeBadKey {
  Map<int, DateTime> intDateTimeMap;
}
 
@JsonSerializable(createToJson: false)
class NoDeserializeBadKey {
  Map<int, DateTime> intDateTimeMap;
}
 
@JsonSerializable(createFactory: false)
class IncludeIfNullAll {
  @JsonKey(includeIfNull: true)
  int number;
  String str;
}
 
@ShouldGenerate(r'''
Map<String, dynamic> _$IncludeIfNullOverrideToJson(
    IncludeIfNullOverride instance) {
  final val = <String, dynamic>{
    'number': instance.number,
  };
 
  void writeNotNull(String key, dynamic value) {
    if (value != null) {
      val[key] = value;
    }
  }
 
  writeNotNull('str', instance.str);
  return val;
}
''')
@JsonSerializable(createFactory: false, includeIfNull: false)
class IncludeIfNullOverride {
  @JsonKey(includeIfNull: true)
  int number;
  String str;
}
 
// https://github.com/dart-lang/json_serializable/issues/7 regression
@JsonSerializable()
class NoCtorClass {
  final int member;
 
  factory NoCtorClass.fromJson(Map<String, dynamic> json) => null;
}
 
@ShouldThrow('More than one field has the JSON key `str`.',
    'Check the `JsonKey` annotations on fields.')
@JsonSerializable(createFactory: false)
class KeyDupesField {
  @JsonKey(name: 'str')
  int number;
 
  String str;
}
 
@ShouldThrow('More than one field has the JSON key `a`.',
    'Check the `JsonKey` annotations on fields.')
@JsonSerializable(createFactory: false)
class DupeKeys {
  @JsonKey(name: 'a')
  int number;
 
  @JsonKey(name: 'a')
  String str;
}
 
@ShouldGenerate(r'''
Map<String, dynamic> _$IgnoredFieldClassToJson(IgnoredFieldClass instance) =>
    <String, dynamic>{
      'ignoredFalseField': instance.ignoredFalseField,
      'ignoredNullField': instance.ignoredNullField
    };
''')
@JsonSerializable(createFactory: false)
class IgnoredFieldClass {
  @JsonKey(ignore: true)
  int ignoredTrueField;
 
  @JsonKey(ignore: false)
  int ignoredFalseField;
 
  int ignoredNullField;
}
 
@ShouldThrow('Cannot populate the required constructor argument: '
    'ignoredTrueField. It is assigned to an ignored field.')
@JsonSerializable()
class IgnoredFieldCtorClass {
  @JsonKey(ignore: true)
  int ignoredTrueField;
  IgnoredFieldCtorClass(this.ignoredTrueField);
}
 
@ShouldThrow('Cannot populate the required constructor argument: '
    '_privateField. It is assigned to a private field.')
@JsonSerializable()
class PrivateFieldCtorClass {
  // ignore: unused_field
  int _privateField;
  PrivateFieldCtorClass(this._privateField);
}
 
@ShouldThrow('Error with `@JsonKey` on `field`. '
    'Cannot set both `disallowNullvalue` and `includeIfNull` to `true`. '
    'This leads to incompatible `toJson` and `fromJson` behavior.')
@JsonSerializable()
class IncludeIfNullDisallowNullClass {
  @JsonKey(includeIfNull: true, disallowNullValue: true)
  int field;
}
 
@JsonSerializable(createFactory: false)
class TrivialNestedNullable {
  TrivialNestedNullable child;
  int otherField;
}
 
@JsonSerializable(createFactory: false, nullable: false)
class TrivialNestedNonNullable {
  TrivialNestedNonNullable child;
  int otherField;
}
 
@ShouldThrow(
    'The `JsonValue` annotation on `BadEnum.value` does not have a value '
    'of type String, int, or null.')
@JsonSerializable()
class JsonValueWithBool {
  BadEnum field;
}
 
enum BadEnum {
  @JsonValue(true)
  value
}
 
@ShouldGenerate(r'''const _$GoodEnumEnumMap = <GoodEnum, dynamic>{
  GoodEnum.noAnnotation: 'noAnnotation',
  GoodEnum.stringAnnotation: 'string annotation',
  GoodEnum.stringAnnotationWeird: r"string annotation with $ funky 'values'",
  GoodEnum.intValue: 42,
  GoodEnum.nullValue: null
};
''', contains: true)
@JsonSerializable()
class JsonValueValid {
  GoodEnum field;
}
 
enum GoodEnum {
  noAnnotation,
  @JsonValue('string annotation')
  stringAnnotation,
  @JsonValue("string annotation with \$ funky 'values'")
  stringAnnotationWeird,
  @JsonValue(42)
  intValue,
  @JsonValue(null)
  nullValue
}