1
+ package com .fasterxml .jackson .failing ;
2
+
3
+ import com .fasterxml .jackson .annotation .JsonCreator ;
4
+ import com .fasterxml .jackson .databind .BaseMapTest ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
7
+
8
+ import java .util .List ;
9
+
10
+ public class BuilderDeserializationTest2486
11
+ extends BaseMapTest
12
+ {
13
+ @ JsonDeserialize (builder = MyPOJOWithArrayCreator .Builder .class )
14
+ public static class MyPOJOWithArrayCreator {
15
+ private final int index ;
16
+
17
+ private MyPOJOWithArrayCreator (int i ) {
18
+ index = i ;
19
+ }
20
+
21
+ public int getIndex () {
22
+ return index ;
23
+ }
24
+
25
+ public static class Builder {
26
+ private int index ;
27
+
28
+ public Builder () {
29
+ // Default constructor
30
+ }
31
+
32
+ @ JsonCreator (mode = JsonCreator .Mode .DELEGATING )
33
+ public Builder (final List <Object > jsonArray ) {
34
+ withIndex ((int ) jsonArray .get (0 ));
35
+ }
36
+
37
+ public Builder withIndex (int i ) {
38
+ index = i ;
39
+ return this ;
40
+ }
41
+
42
+ public MyPOJOWithArrayCreator build () {
43
+ return new MyPOJOWithArrayCreator (index );
44
+ }
45
+ }
46
+ }
47
+
48
+ @ JsonDeserialize (builder = MyPOJOWithPrimitiveCreator .Builder .class )
49
+ public static class MyPOJOWithPrimitiveCreator {
50
+ private final int index ;
51
+
52
+ private MyPOJOWithPrimitiveCreator (int i ) {
53
+ index = i ;
54
+ }
55
+
56
+ public int getIndex () {
57
+ return index ;
58
+ }
59
+
60
+ public static class Builder {
61
+ private int index ;
62
+
63
+ public Builder () {
64
+ // Default constructor
65
+ }
66
+
67
+ @ JsonCreator (mode = JsonCreator .Mode .DELEGATING )
68
+ public Builder (final int i ) {
69
+ withIndex (i );
70
+ }
71
+
72
+ public Builder withIndex (int i ) {
73
+ index = i ;
74
+ return this ;
75
+ }
76
+
77
+ public MyPOJOWithPrimitiveCreator build () {
78
+ return new MyPOJOWithPrimitiveCreator (index );
79
+ }
80
+ }
81
+ }
82
+
83
+ private final ObjectMapper MAPPER = newJsonMapper ();
84
+
85
+ // This test passes when the array based @JsonCreator is removed from the
86
+ // MyPOJOWithArrayCreator.Builder implementation. The presence of the creator
87
+ // in the case of arrays breaks deserialize from an object.
88
+ //
89
+ // Compare that to the analogous tests for MyPOJOWithPrimitiveCreator which
90
+ // pass in both cases.
91
+ //
92
+ // I left some notes in BeanDeserializerBase as to behavior.
93
+ public void testPOJOWithArrayCreatorFromObjectRepresentation () throws Exception {
94
+ final String json = aposToQuotes ("{ 'index': 123 }" );
95
+ final MyPOJOWithArrayCreator deserialized = MAPPER .readValue (json , MyPOJOWithArrayCreator .class );
96
+ assertEquals (123 , deserialized .getIndex ());
97
+ }
98
+
99
+ public void testPOJOWithArrayCreatorFromArrayRepresentation () throws Exception {
100
+ final String json = "[123]" ;
101
+ final MyPOJOWithArrayCreator deserialized = MAPPER .readValue (json , MyPOJOWithArrayCreator .class );
102
+ assertEquals (123 , deserialized .getIndex ());
103
+ }
104
+
105
+ public void testPOJOWithPrimitiveCreatorFromObjectRepresentation () throws Exception {
106
+ final String json = aposToQuotes ("{ 'index': 123 }" );
107
+ final MyPOJOWithPrimitiveCreator deserialized = MAPPER .readValue (json , MyPOJOWithPrimitiveCreator .class );
108
+ assertEquals (123 , deserialized .getIndex ());
109
+ }
110
+
111
+ public void testPOJOWithPrimitiveCreatorFromPrimitiveRepresentation () throws Exception {
112
+ final String json ="123" ;
113
+ final MyPOJOWithPrimitiveCreator deserialized = MAPPER .readValue (json , MyPOJOWithPrimitiveCreator .class );
114
+ assertEquals (123 , deserialized .getIndex ());
115
+ }
116
+ }
0 commit comments