1
1
package com .fasterxml .jackson .databind .struct ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .Collections ;
3
5
import java .util .EnumSet ;
4
6
import java .util .List ;
5
7
8
+ import com .fasterxml .jackson .annotation .JsonCreator ;
6
9
import com .fasterxml .jackson .annotation .JsonFormat ;
10
+ import com .fasterxml .jackson .annotation .JsonProperty ;
7
11
import com .fasterxml .jackson .databind .BaseMapTest ;
8
12
import com .fasterxml .jackson .databind .ObjectMapper ;
13
+ import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
9
14
10
15
public class FormatFeatureAcceptSingleTest extends BaseMapTest
11
16
{
@@ -51,6 +56,33 @@ static class StringListWrapper {
51
56
public List <String > values ;
52
57
}
53
58
59
+ @ JsonDeserialize (builder = StringListWrapperWithBuilder .Builder .class )
60
+ static class StringListWrapperWithBuilder {
61
+ public final List <String > values ;
62
+
63
+ private StringListWrapperWithBuilder (List <String > values ) {
64
+ this .values = values ;
65
+ }
66
+
67
+ static class Builder {
68
+ private List <String > values = Collections .emptyList ();
69
+
70
+ @ JsonProperty
71
+ @ JsonFormat (with = JsonFormat .Feature .ACCEPT_SINGLE_VALUE_AS_ARRAY )
72
+ public Builder values (Iterable <? extends String > elements ) {
73
+ values = new ArrayList <>();
74
+ for (String value : elements ) {
75
+ values .add (value );
76
+ }
77
+ return this ;
78
+ }
79
+
80
+ public StringListWrapperWithBuilder build () {
81
+ return new StringListWrapperWithBuilder (values );
82
+ }
83
+ }
84
+ }
85
+
54
86
static class EnumSetWrapper {
55
87
@ JsonFormat (with = JsonFormat .Feature .ACCEPT_SINGLE_VALUE_AS_ARRAY )
56
88
public EnumSet <ABC > values ;
@@ -66,11 +98,60 @@ static class RolesInList {
66
98
public List <Role > roles ;
67
99
}
68
100
101
+ @ JsonDeserialize (builder = RolesInListWithBuilder .Builder .class )
102
+ static class RolesInListWithBuilder {
103
+ public final List <Role > roles ;
104
+
105
+ private RolesInListWithBuilder (List <Role > roles ) {
106
+ this .roles = roles ;
107
+ }
108
+
109
+ static class Builder {
110
+ private List <Role > values = Collections .emptyList ();
111
+
112
+ @ JsonProperty
113
+ @ JsonFormat (with = JsonFormat .Feature .ACCEPT_SINGLE_VALUE_AS_ARRAY )
114
+ public Builder roles (Iterable <? extends Role > elements ) {
115
+ values = new ArrayList <>();
116
+ for (Role value : elements ) {
117
+ values .add (value );
118
+ }
119
+ return this ;
120
+ }
121
+
122
+ public RolesInListWithBuilder build () {
123
+ return new RolesInListWithBuilder (values );
124
+ }
125
+ }
126
+ }
127
+
128
+ static class WrapperWithStringFactoryInList {
129
+ @ JsonFormat (with = JsonFormat .Feature .ACCEPT_SINGLE_VALUE_AS_ARRAY )
130
+ public List <WrapperWithStringFactory > values ;
131
+ }
132
+
69
133
static class Role {
70
134
public String ID ;
71
135
public String Name ;
72
136
}
73
137
138
+ @ JsonDeserialize
139
+ static class WrapperWithStringFactory {
140
+ private final Role role ;
141
+
142
+ private WrapperWithStringFactory (Role role ) {
143
+ this .role = role ;
144
+ }
145
+
146
+ @ JsonCreator
147
+ static WrapperWithStringFactory from (String value ) {
148
+ Role role = new Role ();
149
+ role .ID = "1" ;
150
+ role .Name = value ;
151
+ return new WrapperWithStringFactory (role );
152
+ }
153
+ }
154
+
74
155
private final ObjectMapper MAPPER = new ObjectMapper ();
75
156
76
157
/*
@@ -150,7 +231,7 @@ public void testSingleElementArrayRead() throws Exception {
150
231
assertEquals (1 , response .roles .length );
151
232
assertEquals ("333" , response .roles [0 ].ID );
152
233
}
153
-
234
+
154
235
public void testSingleStringListRead () throws Exception {
155
236
String json = aposToQuotes (
156
237
"{ 'values': 'first' }" );
@@ -160,6 +241,16 @@ public void testSingleStringListRead() throws Exception {
160
241
assertEquals ("first" , result .values .get (0 ));
161
242
}
162
243
244
+ public void testSingleStringListReadWithBuilder () throws Exception {
245
+ String json = aposToQuotes (
246
+ "{ 'values': 'first' }" );
247
+ StringListWrapperWithBuilder result =
248
+ MAPPER .readValue (json , StringListWrapperWithBuilder .class );
249
+ assertNotNull (result .values );
250
+ assertEquals (1 , result .values .size ());
251
+ assertEquals ("first" , result .values .get (0 ));
252
+ }
253
+
163
254
public void testSingleElementListRead () throws Exception {
164
255
String json = aposToQuotes (
165
256
"{ 'roles': { 'Name': 'User', 'ID': '333' } }" );
@@ -169,6 +260,24 @@ public void testSingleElementListRead() throws Exception {
169
260
assertEquals ("333" , response .roles .get (0 ).ID );
170
261
}
171
262
263
+ public void testSingleElementListReadWithBuilder () throws Exception {
264
+ String json = aposToQuotes (
265
+ "{ 'roles': { 'Name': 'User', 'ID': '333' } }" );
266
+ RolesInListWithBuilder response = MAPPER .readValue (json , RolesInListWithBuilder .class );
267
+ assertNotNull (response .roles );
268
+ assertEquals (1 , response .roles .size ());
269
+ assertEquals ("333" , response .roles .get (0 ).ID );
270
+ }
271
+
272
+ public void testSingleElementWithStringFactoryRead () throws Exception {
273
+ String json = aposToQuotes (
274
+ "{ 'values': '333' }" );
275
+ WrapperWithStringFactoryInList response = MAPPER .readValue (json , WrapperWithStringFactoryInList .class );
276
+ assertNotNull (response .values );
277
+ assertEquals (1 , response .values .size ());
278
+ assertEquals ("333" , response .values .get (0 ).role .Name );
279
+ }
280
+
172
281
public void testSingleEnumSetRead () throws Exception {
173
282
EnumSetWrapper result = MAPPER .readValue (aposToQuotes ("{ 'values': 'B' }" ),
174
283
EnumSetWrapper .class );
0 commit comments