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
// 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';
 
@ShouldGenerate(r'''
JsonConverterNamedCtor<E> _$JsonConverterNamedCtorFromJson<E>(
    Map<String, dynamic> json) {
  return JsonConverterNamedCtor<E>()
    ..value = json['value'] == null
        ? null
        : const _DurationMillisecondConverter.named()
            .fromJson(json['value'] as int)
    ..genericValue = json['genericValue'] == null
        ? null
        : _GenericConverter<E>.named().fromJson(json['genericValue'] as int)
    ..keyAnnotationFirst = json['keyAnnotationFirst'] == null
        ? null
        : JsonConverterNamedCtor._fromJson(json['keyAnnotationFirst'] as int);
}
 
Map<String, dynamic> _$JsonConverterNamedCtorToJson<E>(
        JsonConverterNamedCtor<E> instance) =>
    <String, dynamic>{
      'value': instance.value == null
          ? null
          : const _DurationMillisecondConverter.named().toJson(instance.value),
      'genericValue': instance.genericValue == null
          ? null
          : _GenericConverter<E>.named().toJson(instance.genericValue),
      'keyAnnotationFirst': instance.keyAnnotationFirst == null
          ? null
          : JsonConverterNamedCtor._toJson(instance.keyAnnotationFirst)
    };
''')
@JsonSerializable()
@_DurationMillisecondConverter.named()
@_GenericConverter.named()
class JsonConverterNamedCtor<E> {
  Duration value;
  E genericValue;
 
  // Field annotations have precedence over class annotations
  @JsonKey(fromJson: _fromJson, toJson: _toJson)
  Duration keyAnnotationFirst;
 
  static Duration _fromJson(int value) => null;
  static int _toJson(Duration object) => 42;
}
 
@ShouldGenerate(r'''
JsonConvertOnField<E> _$JsonConvertOnFieldFromJson<E>(
    Map<String, dynamic> json) {
  return JsonConvertOnField<E>()
    ..annotatedField = json['annotatedField'] == null
        ? null
        : const _DurationMillisecondConverter()
            .fromJson(json['annotatedField'] as int)
    ..annotatedWithNamedCtor = json['annotatedWithNamedCtor'] == null
        ? null
        : const _DurationMillisecondConverter.named()
            .fromJson(json['annotatedWithNamedCtor'] as int)
    ..classAnnotatedWithField = json['classAnnotatedWithField'] == null
        ? null
        : _durationConverter.fromJson(json['classAnnotatedWithField'] as int)
    ..genericValue = json['genericValue'] == null
        ? null
        : _GenericConverter<E>().fromJson(json['genericValue'] as int);
}
 
Map<String, dynamic> _$JsonConvertOnFieldToJson<E>(
        JsonConvertOnField<E> instance) =>
    <String, dynamic>{
      'annotatedField': instance.annotatedField == null
          ? null
          : const _DurationMillisecondConverter()
              .toJson(instance.annotatedField),
      'annotatedWithNamedCtor': instance.annotatedWithNamedCtor == null
          ? null
          : const _DurationMillisecondConverter.named()
              .toJson(instance.annotatedWithNamedCtor),
      'classAnnotatedWithField': instance.classAnnotatedWithField == null
          ? null
          : _durationConverter.toJson(instance.classAnnotatedWithField),
      'genericValue': instance.genericValue == null
          ? null
          : _GenericConverter<E>().toJson(instance.genericValue)
    };
''')
@JsonSerializable()
@_durationConverter
class JsonConvertOnField<E> {
  @_DurationMillisecondConverter()
  Duration annotatedField;
 
  @_DurationMillisecondConverter.named()
  Duration annotatedWithNamedCtor;
 
  Duration classAnnotatedWithField;
 
  @_GenericConverter()
  E genericValue;
}
 
class _GenericConverter<T> implements JsonConverter<T, int> {
  const _GenericConverter();
  const _GenericConverter.named();
 
  @override
  T fromJson(int json) => null;
 
  @override
  int toJson(T object) => 0;
}
 
@ShouldThrow('`JsonConverter` implementations can have no more than one type '
    'argument. `_BadConverter` has 2.')
@JsonSerializable()
@_BadConverter()
class JsonConverterWithBadTypeArg<T> {
  T value;
}
 
class _BadConverter<T, S> implements JsonConverter<S, int> {
  const _BadConverter();
 
  @override
  S fromJson(int json) => null;
 
  @override
  int toJson(S object) => 0;
}
 
@ShouldThrow('Found more than one matching converter for `Duration`.')
@JsonSerializable()
@_durationConverter
@_DurationMillisecondConverter()
class JsonConverterDuplicateAnnotations {
  Duration value;
}
 
const _durationConverter = _DurationMillisecondConverter();
 
class _DurationMillisecondConverter implements JsonConverter<Duration, int> {
  const _DurationMillisecondConverter();
 
  const _DurationMillisecondConverter.named();
 
  @override
  Duration fromJson(int json) =>
      json == null ? null : Duration(milliseconds: json);
 
  @override
  int toJson(Duration object) => object?.inMilliseconds;
}
 
@ShouldThrow('Generators with constructor arguments are not supported.')
@JsonSerializable()
@_ConverterWithCtorParams(42)
class JsonConverterCtorParams {
  Duration value;
}
 
class _ConverterWithCtorParams implements JsonConverter<Duration, int> {
  final int param;
  const _ConverterWithCtorParams(this.param);
 
  @override
  Duration fromJson(int json) => null;
 
  @override
  int toJson(Duration object) => 0;
}