trphoenix
2018-11-12 29fbfc5dd1d55d189f23eb6d32f000252f92985f
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
// 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.
 
part of '_json_serializable_test_input.dart';
 
int _toInt(bool input) => 42;
int _twoArgFunction(int a, int b) => 42;
 
dynamic _toDynamic(dynamic input) => null;
Object _toObject(Object input) => null;
 
@ShouldThrow(
    'Error with `@JsonKey` on `field`. The `fromJson` function `_toInt` '
    'return type `int` is not compatible with field type `String`.')
@JsonSerializable()
class BadFromFuncReturnType {
  @JsonKey(fromJson: _toInt)
  String field;
}
 
@ShouldThrow('Error with `@JsonKey` on `field`. The `fromJson` function '
    '`_twoArgFunction` must have one positional paramater.')
@JsonSerializable()
class InvalidFromFunc2Args {
  @JsonKey(fromJson: _twoArgFunction)
  String field;
}
 
@ShouldGenerate(r'''
ValidToFromFuncClassStatic _$ValidToFromFuncClassStaticFromJson(
    Map<String, dynamic> json) {
  return ValidToFromFuncClassStatic()
    ..field = json['field'] == null
        ? null
        : ValidToFromFuncClassStatic._staticFunc(json['field'] as String);
}
 
Map<String, dynamic> _$ValidToFromFuncClassStaticToJson(
        ValidToFromFuncClassStatic instance) =>
    <String, dynamic>{
      'field': instance.field == null
          ? null
          : ValidToFromFuncClassStatic._staticFunc(instance.field)
    };
''')
@JsonSerializable()
class ValidToFromFuncClassStatic {
  static String _staticFunc(String param) => null;
 
  @JsonKey(fromJson: _staticFunc, toJson: _staticFunc)
  String field;
}
 
@ShouldThrow('Error with `@JsonKey` on `field`. The `toJson` function `_toInt` '
    'argument type `bool` is not compatible with field type `String`.')
@JsonSerializable()
class BadToFuncReturnType {
  @JsonKey(toJson: _toInt)
  String field;
}
 
@ShouldThrow('Error with `@JsonKey` on `field`. The `toJson` function '
    '`_twoArgFunction` must have one positional paramater.')
@JsonSerializable()
class InvalidToFunc2Args {
  @JsonKey(toJson: _twoArgFunction)
  String field;
}
 
@ShouldGenerate("_toObject(json['field'])", contains: true)
@JsonSerializable()
class ObjectConvertMethods {
  @JsonKey(fromJson: _toObject, toJson: _toObject)
  String field;
}
 
@ShouldGenerate("_toDynamic(json['field'])", contains: true)
@JsonSerializable()
class DynamicConvertMethods {
  @JsonKey(fromJson: _toDynamic, toJson: _toDynamic)
  String field;
}
 
String _toString(String input) => null;
 
@ShouldGenerate("_toString(json['field'] as String)", contains: true)
@JsonSerializable()
class TypedConvertMethods {
  @JsonKey(fromJson: _toString, toJson: _toString)
  String field;
}
 
String _fromDynamicMap(Map input) => null;
String _fromDynamicList(List input) => null;
String _fromDynamicIterable(Iterable input) => null;
 
@ShouldGenerate(r'''
FromDynamicCollection _$FromDynamicCollectionFromJson(
    Map<String, dynamic> json) {
  return FromDynamicCollection()
    ..mapField = json['mapField'] == null
        ? null
        : _fromDynamicMap(json['mapField'] as Map)
    ..listField = json['listField'] == null
        ? null
        : _fromDynamicList(json['listField'] as List)
    ..iterableField = json['iterableField'] == null
        ? null
        : _fromDynamicIterable(json['iterableField'] as List);
}
''')
@JsonSerializable(createToJson: false)
class FromDynamicCollection {
  @JsonKey(fromJson: _fromDynamicMap)
  String mapField;
  @JsonKey(fromJson: _fromDynamicList)
  String listField;
  @JsonKey(fromJson: _fromDynamicIterable)
  String iterableField;
}
 
String _noArgs() => null;
 
@ShouldThrow('Error with `@JsonKey` on `field`. The `fromJson` function '
    '`_noArgs` must have one positional paramater.')
@JsonSerializable(createToJson: false)
class BadNoArgs {
  @JsonKey(fromJson: _noArgs)
  String field;
}
 
String _twoArgs(a, b) => null;
 
@ShouldThrow('Error with `@JsonKey` on `field`. The `fromJson` function '
    '`_twoArgs` must have one positional paramater.')
@JsonSerializable(createToJson: false)
class BadTwoRequiredPositional {
  @JsonKey(fromJson: _twoArgs)
  String field;
}
 
String _oneNamed({a}) => null;
 
@ShouldThrow('Error with `@JsonKey` on `field`. The `fromJson` function '
    '`_oneNamed` must have one positional paramater.')
@JsonSerializable(createToJson: false)
class BadOneNamed {
  @JsonKey(fromJson: _oneNamed)
  String field;
}
 
String _oneNormalOnePositional(a, [b]) => null;
 
@ShouldGenerate("_oneNormalOnePositional(json['field'])", contains: true)
@JsonSerializable(createToJson: false)
class OkayOneNormalOptionalPositional {
  @JsonKey(fromJson: _oneNormalOnePositional)
  String field;
}
 
String _oneNormalOptionalNamed(a, {b}) => null;
 
@ShouldGenerate("_oneNormalOptionalNamed(json['field'])", contains: true)
@JsonSerializable(createToJson: false)
class OkayOneNormalOptionalNamed {
  @JsonKey(fromJson: _oneNormalOptionalNamed)
  String field;
}
 
String _onlyOptionalPositional([a, b]) => null;
 
@ShouldGenerate("_onlyOptionalPositional(json['field'])", contains: true)
@JsonSerializable(createToJson: false)
class OkayOnlyOptionalPositional {
  @JsonKey(fromJson: _onlyOptionalPositional)
  String field;
}