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
// 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: annotate_overrides, hash_and_equals
import 'package:json_annotation/json_annotation.dart';
 
import 'json_converters.dart';
import 'kitchen_sink_interface.dart' as k;
import 'simple_object.dart';
import 'strict_keys_object.dart';
 
part 'kitchen_sink.g.dart';
 
// NOTE: these methods are replaced in the `non_nullable` cases to return
// non-null values.
List<T> _defaultList<T>() => null;
Set<T> _defaultSet<T>() => null;
Map _defaultMap() => null;
SimpleObject _defaultSimpleObject() => null;
StrictKeysObject _defaultStrictKeysObject() => null;
 
k.KitchenSink testFactory(
        {int ctorValidatedNo42,
        Iterable iterable,
        Iterable<dynamic> dynamicIterable,
        Iterable<Object> objectIterable,
        Iterable<int> intIterable,
        Iterable<DateTime> dateTimeIterable}) =>
    KitchenSink(
        ctorValidatedNo42: ctorValidatedNo42,
        iterable: iterable,
        dynamicIterable: dynamicIterable,
        objectIterable: objectIterable,
        intIterable: intIterable,
        dateTimeIterable: dateTimeIterable);
 
k.KitchenSink testFromJson(Map json) => KitchenSink.fromJson(json);
 
@JsonSerializable(anyMap: true, generateToJsonFunction: false)
class KitchenSink extends Object
    with _$KitchenSinkSerializerMixin
    implements k.KitchenSink {
  // To ensure static members are not considered for serialization.
  static const answer = 42;
  static final reason = 42;
  static int get understand => 42;
 
  // NOTE: exposing these as Iterable, but storing the values as List
  // to make the equality test work trivially.
  final Iterable _iterable;
  final Iterable<dynamic> _dynamicIterable;
  final Iterable<Object> _objectIterable;
  final Iterable<int> _intIterable;
  final Iterable<DateTime> _dateTimeIterable;
 
  @JsonKey(name: 'no-42')
  final int ctorValidatedNo42;
 
  KitchenSink(
      {this.ctorValidatedNo42,
      Iterable iterable,
      Iterable<dynamic> dynamicIterable,
      Iterable<Object> objectIterable,
      Iterable<int> intIterable,
      Iterable<DateTime> dateTimeIterable})
      : _iterable = iterable?.toList() ?? _defaultList(),
        _dynamicIterable = dynamicIterable?.toList() ?? _defaultList(),
        _objectIterable = objectIterable?.toList() ?? _defaultList(),
        _intIterable = intIterable?.toList() ?? _defaultList(),
        _dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList() {
    if (ctorValidatedNo42 == 42) {
      throw ArgumentError.value(
          42, 'ctorValidatedNo42', 'The value `42` is not allowed.');
    }
  }
 
  factory KitchenSink.fromJson(Map json) => _$KitchenSinkFromJson(json);
 
  @JsonKey(includeIfNull: false)
  DateTime dateTime;
 
  @JsonKey(includeIfNull: false)
  Iterable get iterable => _iterable;
  Iterable<dynamic> get dynamicIterable => _dynamicIterable;
  Iterable<Object> get objectIterable => _objectIterable;
  Iterable<int> get intIterable => _intIterable;
 
  Set set = _defaultSet();
  Set<dynamic> dynamicSet = _defaultSet();
  Set<Object> objectSet = _defaultSet();
  Set<int> intSet = _defaultSet();
  Set<DateTime> dateTimeSet = _defaultSet();
 
  // Added a one-off annotation on a property (not a field)
  @JsonKey(name: 'datetime-iterable')
  Iterable<DateTime> get dateTimeIterable => _dateTimeIterable;
 
  List list = _defaultList();
  List<dynamic> dynamicList = _defaultList();
  List<Object> objectList = _defaultList();
  List<int> intList = _defaultList();
  @JsonKey(includeIfNull: false)
  List<DateTime> dateTimeList = _defaultList();
 
  Map map = _defaultMap();
  Map<String, String> stringStringMap = _defaultMap();
  Map<dynamic, int> dynamicIntMap = _defaultMap();
  Map<Object, DateTime> objectDateTimeMap = _defaultMap();
 
  @JsonKey(includeIfNull: false)
  List<Map<String, Map<String, List<List<DateTime>>>>> crazyComplex =
      _defaultList();
 
  // Handle fields with names that collide with helper names
  @JsonKey(includeIfNull: false)
  Map<String, bool> val = _defaultMap();
  bool writeNotNull;
  @JsonKey(name: r'$string')
  String string;
 
  SimpleObject simpleObject = _defaultSimpleObject();
 
  StrictKeysObject strictKeysObject = _defaultStrictKeysObject();
 
  int _validatedPropertyNo42;
  int get validatedPropertyNo42 => _validatedPropertyNo42;
 
  set validatedPropertyNo42(int value) {
    if (value == 42) {
      throw StateError('Cannot be 42!');
    }
    _validatedPropertyNo42 = value;
  }
 
  bool operator ==(Object other) => k.sinkEquals(this, other);
}
 
@JsonSerializable(anyMap: true, generateToJsonFunction: false)
// referencing a top-level field should work
@durationConverter
// referencing via a const constructor should work
@BigIntStringConverter()
@TrivialNumberConverter.instance
@EpochDateTimeConverter()
class JsonConverterTestClass extends Object
    with _$JsonConverterTestClassSerializerMixin {
  JsonConverterTestClass();
 
  factory JsonConverterTestClass.fromJson(Map<String, dynamic> json) =>
      _$JsonConverterTestClassFromJson(json);
 
  Duration duration;
  List<Duration> durationList;
 
  BigInt bigInt;
  Map<String, BigInt> bigIntMap;
 
  TrivialNumber numberSilly;
  Set<TrivialNumber> numberSillySet;
 
  DateTime dateTime;
}
 
@JsonSerializable(anyMap: true, generateToJsonFunction: false)
@GenericConverter()
class JsonConverterGeneric<S, T, U> extends Object
    with _$JsonConverterGenericSerializerMixin<S, T, U> {
  S item;
  List<T> itemList;
  Map<String, U> itemMap;
 
  JsonConverterGeneric();
 
  factory JsonConverterGeneric.fromJson(Map<String, dynamic> json) =>
      _$JsonConverterGenericFromJson(json);
}