-
Notifications
You must be signed in to change notification settings - Fork 387
/
converter_examples.dart
97 lines (71 loc) · 2.41 KB
/
converter_examples.dart
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
import 'dart:convert';
import 'package:json_annotation/json_annotation.dart';
part 'converter_examples.g.dart';
@JsonEnum(valueField: 'value')
enum Issue1202RegressionEnum {
normalValue(42),
nullValue(null);
const Issue1202RegressionEnum(this.value);
final int? value;
}
@JsonSerializable(includeIfNull: false)
class Issue1202RegressionClass {
@JsonKey(fromJson: _fromJson, toJson: _toJson)
final int valueWithFunctions;
@_Issue1202RegressionNotNullConverter()
final int notNullableValueWithConverter;
final Issue1202RegressionEnum value;
final int? normalNullableValue;
@_Issue1202RegressionConverter()
final int notNullableValueWithNullableConverter;
@JsonKey(fromJson: _fromJsonNullable, toJson: _toJsonNullable)
final int valueWithNullableFunctions;
Issue1202RegressionClass({
required this.value,
required this.normalNullableValue,
required this.notNullableValueWithNullableConverter,
required this.notNullableValueWithConverter,
required this.valueWithFunctions,
required this.valueWithNullableFunctions,
});
factory Issue1202RegressionClass.fromJson(Map<String, dynamic> json) =>
_$Issue1202RegressionClassFromJson(json);
Map<String, dynamic> toJson() => _$Issue1202RegressionClassToJson(this);
@override
bool operator ==(Object other) =>
other is Issue1202RegressionClass &&
jsonEncode(other) == jsonEncode(this);
@override
int get hashCode => jsonEncode(this).hashCode;
static int _fromJsonNullable(String? json) {
if (json == null) return _default;
return int.parse(json);
}
static String? _toJsonNullable(int object) {
if (object == _default) return null;
return object.toString();
}
static int _fromJson(String json) => int.parse(json);
static String _toJson(int object) => object.toString();
}
const _default = 42;
class _Issue1202RegressionConverter extends JsonConverter<int, String?> {
const _Issue1202RegressionConverter();
@override
int fromJson(String? json) {
if (json == null) return _default;
return int.parse(json);
}
@override
String? toJson(int object) {
if (object == _default) return null;
return object.toString();
}
}
class _Issue1202RegressionNotNullConverter extends JsonConverter<int, String> {
const _Issue1202RegressionNotNullConverter();
@override
int fromJson(String json) => int.parse(json);
@override
String toJson(int object) => object.toString();
}