-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathjson_key.dart
189 lines (171 loc) · 7.03 KB
/
json_key.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
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
179
180
181
182
183
184
185
186
187
188
189
// 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:meta/meta_meta.dart';
import 'allowed_keys_helpers.dart';
import 'json_serializable.dart';
/// An annotation used to specify how a field is serialized.
@Target({TargetKind.field, TargetKind.getter})
class JsonKey {
/// The value to use if the source JSON does not contain this key or if the
/// value is `null`.
///
/// Also supported: a top-level or static [Function] or a constructor with no
/// required parameters and a return type compatible with the field being
/// assigned.
final Object? defaultValue;
/// If `true`, generated code will throw a [DisallowedNullValueException] if
/// the corresponding key exists, but the value is `null`.
///
/// Note: this value does not affect the behavior of a JSON map *without* the
/// associated key.
///
/// If [disallowNullValue] is `true`, [includeIfNull] will be treated as
/// `false` to ensure compatibility between `toJson` and `fromJson`.
///
/// If both [includeIfNull] and [disallowNullValue] are set to `true` on the
/// same field, an exception will be thrown during code generation.
final bool? disallowNullValue;
/// A [Function] to use when decoding the associated JSON value to the
/// annotated field.
///
/// Must be a top-level or static [Function] or a constructor that accepts one
/// positional argument mapping a JSON literal to a value compatible with the
/// type of the annotated field.
///
/// When creating a class that supports both `toJson` and `fromJson`
/// (the default), you should also set [toJson] if you set [fromJson].
/// Values returned by [toJson] should "round-trip" through [fromJson].
final Function? fromJson;
/// `true` if the generator should ignore this field completely.
///
/// If `null` (the default) or `false`, the field will be considered for
/// serialization.
///
/// This field is DEPRECATED use [includeFromJson] and [includeToJson]
/// instead.
@Deprecated(
'Use `includeFromJson` and `includeToJson` with a value of `false` '
'instead.',
)
final bool? ignore;
/// Used to force a field to be included (or excluded) when decoding a object
/// from JSON.
///
/// `null` (the default) means the field will be handled with the default
/// semantics that take into account if it's private or if it can be cleanly
/// round-tripped to-from JSON.
///
/// `true` means the field should always be decoded, even if it's private.
///
/// `false` means the field should never be decoded.
final bool? includeFromJson;
/// Whether the generator should include fields with `null` values in the
/// serialized output.
///
/// If `true`, the generator should include the field in the serialized
/// output, even if the value is `null`.
///
/// The default value, `null`, indicates that the behavior should be
/// acquired from the [JsonSerializable.includeIfNull] annotation on the
/// enclosing class.
///
/// If [disallowNullValue] is `true`, this value is treated as `false` to
/// ensure compatibility between `toJson` and `fromJson`.
///
/// If both [includeIfNull] and [disallowNullValue] are set to `true` on the
/// same field, an exception will be thrown during code generation.
final bool? includeIfNull;
/// Used to force a field to be included (or excluded) when encoding a object
/// to JSON.
///
/// `null` (the default) means the field will be handled with the default
/// semantics that take into account if it's private or if it can be cleanly
/// round-tripped to-from JSON.
///
/// `true` means the field should always be encoded, even if it's private.
///
/// `false` means the field should never be encoded.
final bool? includeToJson;
/// The key in a JSON map to use when reading and writing values corresponding
/// to the annotated fields.
///
/// If `null`, the field name is used.
final String? name;
/// Specialize how a value is read from the source JSON map.
///
/// Typically, the value corresponding to a given key is read directly from
/// the JSON map using `map[key]`. At times it's convenient to customize this
/// behavior to support alternative names or to support logic that requires
/// accessing multiple values at once.
///
/// The provided, the [Function] must be a top-level or static within the
/// using class.
///
/// Note: using this feature does not change any of the subsequent decoding
/// logic for the field. For instance, if the field is of type [DateTime] we
/// expect the function provided here to return a [String].
final Object? Function(Map, String)? readValue;
/// When `true`, generated code for `fromJson` will verify that the source
/// JSON map contains the associated key.
///
/// If the key does not exist, a [MissingRequiredKeysException] exception is
/// thrown.
///
/// Note: only the existence of the key is checked. A key with a `null` value
/// is considered valid.
final bool? required;
/// A [Function] to use when encoding the annotated field to JSON.
///
/// Must be a top-level or static [Function] or a constructor that accepts one
/// positional argument compatible with the field being serialized that
/// returns a JSON-compatible value.
///
/// When creating a class that supports both `toJson` and `fromJson`
/// (the default), you should also set [fromJson] if you set [toJson].
/// Values returned by [toJson] should "round-trip" through [fromJson].
final Function? toJson;
/// The value to use for an enum field when the value provided is not in the
/// source enum.
///
/// Valid only on enum fields with a compatible enum value.
///
/// If you want to use the value `null` when encountering an unknown value,
/// use the value of [JsonKey.nullForUndefinedEnumValue] instead. This is only
/// valid on a nullable enum field.
final Enum? unknownEnumValue;
/// If true, enum will be parsed with case-insensitive.
/// Specifically, both values will be lower-cased and compared.
///
/// Valid only on enum fields with a compatible enum value.
final bool caseInsensitive;
/// Creates a new [JsonKey] instance.
///
/// Only required when the default behavior is not desired.
const JsonKey({
@Deprecated('Has no effect')
bool? nullable,
this.defaultValue,
this.disallowNullValue,
this.fromJson,
@Deprecated(
'Use `includeFromJson` and `includeToJson` with a value of `false` '
'instead.',
)
this.ignore,
this.includeFromJson,
this.includeIfNull,
this.includeToJson,
this.name,
this.readValue,
this.required,
this.toJson,
this.unknownEnumValue,
this.caseInsensitive = false,
});
/// Sentinel value for use with [unknownEnumValue].
///
/// Read the documentation on [unknownEnumValue] for more details.
static const Enum nullForUndefinedEnumValue = _NullAsDefault.value;
}
enum _NullAsDefault { value }