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
// 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.
 
import 'package:json_annotation/json_annotation.dart';
 
part 'generic_class.g.dart';
 
@JsonSerializable()
class GenericClass<T extends num, S> {
  @JsonKey(fromJson: _dataFromJson, toJson: _dataToJson)
  Object fieldObject;
 
  @JsonKey(fromJson: _dataFromJson, toJson: _dataToJson)
  dynamic fieldDynamic;
 
  @JsonKey(fromJson: _dataFromJson, toJson: _dataToJson)
  int fieldInt;
 
  @JsonKey(fromJson: _dataFromJson, toJson: _dataToJson)
  T fieldT;
 
  @JsonKey(fromJson: _dataFromJson, toJson: _dataToJson)
  S fieldS;
 
  GenericClass();
 
  factory GenericClass.fromJson(Map<String, dynamic> json) =>
      _$GenericClassFromJson<T, S>(json);
 
  Map<String, dynamic> toJson() => _$GenericClassToJson(this);
 
  static T _dataFromJson<T, S, U>(Map<String, dynamic> input,
          [S other1, U other2]) =>
      input['value'] as T;
 
  static Map<String, dynamic> _dataToJson<T, S, U>(T input,
          [S other1, U other2]) =>
      {'value': input};
}
 
@JsonSerializable()
@_DurationMillisecondConverter()
@_DurationListMillisecondConverter()
class GenericClassWithConverter<T extends num, S> {
  @_SimpleConverter()
  Object fieldObject;
 
  @_SimpleConverter()
  dynamic fieldDynamic;
 
  @_SimpleConverter()
  int fieldInt;
 
  @_SimpleConverter()
  T fieldT;
 
  @_SimpleConverter()
  S fieldS;
 
  Duration duration;
 
  List<Duration> listDuration;
 
  GenericClassWithConverter();
 
  factory GenericClassWithConverter.fromJson(Map<String, dynamic> json) =>
      _$GenericClassWithConverterFromJson<T, S>(json);
 
  Map<String, dynamic> toJson() => _$GenericClassWithConverterToJson(this);
}
 
class _SimpleConverter<T> implements JsonConverter<T, Map<String, dynamic>> {
  const _SimpleConverter();
 
  @override
  T fromJson(Map<String, dynamic> json) => json['value'] as T;
 
  @override
  Map<String, dynamic> toJson(T object) => {'value': object};
}
 
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;
}
 
class _DurationListMillisecondConverter
    implements JsonConverter<List<Duration>, int> {
  const _DurationListMillisecondConverter();
 
  @override
  List<Duration> fromJson(int json) => [Duration(milliseconds: json)];
 
  @override
  int toJson(List<Duration> object) => object?.fold<int>(0, (sum, obj) {
        return sum + obj.inMilliseconds;
      });
}