-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathJsonIncludeProperties.java
More file actions
209 lines (185 loc) · 6.82 KB
/
JsonIncludeProperties.java
File metadata and controls
209 lines (185 loc) · 6.82 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.fasterxml.jackson.annotation;
import java.lang.annotation.*;
import java.util.*;
/**
* Annotation that can be used to either only include serialization of
* properties (during serialization), or only include processing of
* JSON properties read (during deserialization).
* <p>
* Example:
* <pre>
* // to only include specified fields from being serialized or deserialized
* // (i.e. only include in JSON output; or being set even if they were included)
* @JsonIncludeProperties({ "internalId", "secretKey" })
* </pre>
* <p>
* Annotation can be applied both to classes and
* to properties. If used for both, actual set will be union of all
* includes: that is, you can only add properties to include, not remove
* or override. So you can not remove properties to include using
* per-property annotation.
*
* @since 2.12
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE,
ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonIncludeProperties
{
/**
* Names of properties to include.
*/
public String[] value() default {};
/**
* Property that can be enabled to indicate that the order of properties
* in {@link #value()} is the order in which properties should be serialized;
* that is, {@link #value()} should be used as if it was
* {@link JsonPropertyOrder#value()} (unless that annotation already
* exists. This is useful in reducing amount of annotation duplication.
*<p>
* Property defaults to {@link OptBoolean#DEFAULT}, meaning "undefined"
* (which effectively translates into {@code OptBoolean.FALSE}).
*
* @since 2.22
*/
public OptBoolean order() default OptBoolean.DEFAULT;
/*
/**********************************************************
/* Value class used to enclose information, allow for
/* merging of layered configuration settings.
/**********************************************************
*/
/**
* Helper class used to contain information from a single {@link JsonIncludeProperties}
* annotation, as well as to provide possible overrides from non-annotation sources.
*
* @since 2.12
*/
public static class Value implements JacksonAnnotationValue<JsonIncludeProperties>,
java.io.Serializable
{
private static final long serialVersionUID = 1L;
/**
* Default instance has no explicitly included fields
*/
protected final static JsonIncludeProperties.Value ALL = new JsonIncludeProperties.Value(null, null);
/**
* Name of the properties to include.
* Null means that all properties are included, empty means none.
*/
protected final Set<String> _included;
/**
* Whether the order of properties in {@link #_included} defines
* the serialization order. {@code null} indicates "not defined"
* (and generally is used as {@code Boolean.FALSE}).
*
* @since 2.22
*/
protected final Boolean _ordered;
/**
* @deprecated Since 2.22, use {@link #Value(Set, Boolean)} instead.
*/
@Deprecated
protected Value(Set<String> included)
{
this(included, null);
}
/**
* @since 2.22
*/
protected Value(Set<String> included, Boolean ordered)
{
_included = included;
_ordered = ordered;
}
public static JsonIncludeProperties.Value from(JsonIncludeProperties src)
{
if (src == null) {
return ALL;
}
return new Value(_asSet(src.value()), src.order().asBoolean());
}
public static JsonIncludeProperties.Value all()
{
return ALL;
}
@Override
public Class<JsonIncludeProperties> valueFor()
{
return JsonIncludeProperties.class;
}
/**
* @return Names included, if any, possibly empty; {@code null} for "not defined"
*/
public Set<String> getIncluded()
{
return _included;
}
/**
* @return Whether the order of properties in {@link #getIncluded()} defines
* the serialization order; {@code null} if not set.
*
* @since 2.22
*/
public Boolean getOrdered()
{
return _ordered;
}
/**
* Mutant factory method to override the current value with an another,
* merging the included fields so that only entries that exist in both original
* and override set are included, taking into account that "undefined" {@link Value}s
* do not count ("undefined" meaning that {@code getIncluded()} returns {@code null}).
* So: overriding with "undefined" returns original {@code Value} as-is; overriding an
* "undefined" {@code Value} returns override {@code Value} as-is.
*/
public JsonIncludeProperties.Value withOverrides(JsonIncludeProperties.Value overrides)
{
final Set<String> otherIncluded;
if (overrides == null || (otherIncluded = overrides.getIncluded()) == null) {
return this;
}
if (_included == null) {
return overrides;
}
HashSet<String> toInclude = new HashSet<String>();
for (String incl : otherIncluded) {
if (_included.contains(incl)) {
toInclude.add(incl);
}
}
Boolean ordered = (overrides._ordered != null) ? overrides._ordered : _ordered;
return new JsonIncludeProperties.Value(toInclude, ordered);
}
@Override
public String toString() {
return String.format("JsonIncludeProperties.Value(included=%s,ordered=%s)",
_included, _ordered);
}
@Override
public int hashCode() {
return ((_included == null) ? 0 : _included.hashCode())
+ (Boolean.TRUE.equals(_ordered) ? 1 : 0);
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o == null) return false;
return (o.getClass() == getClass())
&& Objects.equals(_ordered, ((Value) o)._ordered)
&& Objects.equals(_included, ((Value) o)._included);
}
private static Set<String> _asSet(String[] v)
{
if (v == null || v.length == 0) {
return Collections.emptySet();
}
Set<String> s = new HashSet<String>(v.length);
for (String str : v) {
s.add(str);
}
return s;
}
}
}