2
2
3
3
import java .util .*;
4
4
5
-
5
+ import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
6
+ import com .fasterxml .jackson .core .type .TypeReference ;
6
7
import com .fasterxml .jackson .databind .BaseMapTest ;
7
8
import com .fasterxml .jackson .databind .ObjectMapper ;
8
9
9
10
public class TestGenericTypes extends BaseMapTest
10
11
{
11
- /*
12
- /**********************************************************
13
- /* Helper types
14
- /**********************************************************
15
- */
16
-
17
12
static class Account {
18
13
private Long id ;
19
14
private String name ;
@@ -90,25 +85,41 @@ class Element {
90
85
public Element (T v ) { value = v ; }
91
86
}
92
87
}
88
+
89
+ // For [databind#728]
90
+ static class Base727 {
91
+ public int a ;
92
+ }
93
93
94
+ @ JsonPropertyOrder (alphabetic =true )
95
+ static class Impl727 extends Base727 {
96
+ public int b ;
97
+
98
+ public Impl727 (int a , int b ) {
99
+ this .a = a ;
100
+ this .b = b ;
101
+ }
102
+ }
94
103
/*
95
104
/**********************************************************
96
105
/* Unit tests
97
106
/**********************************************************
98
107
*/
99
108
109
+ // final ObjectMapper MAPPER = new ObjectMapper();
110
+ final ObjectMapper MAPPER = objectMapper ();
111
+
100
112
@ SuppressWarnings ("unchecked" )
101
113
public void testIssue468a () throws Exception
102
114
{
103
115
Person1 p1 = new Person1 ("John" );
104
116
p1 .setAccount (new Key <Account >(new Account ("something" , 42L )));
105
117
106
118
// First: ensure we can serialize (pre 1.7 this failed)
107
- ObjectMapper mapper = new ObjectMapper ();
108
- String json = mapper .writeValueAsString (p1 );
119
+ String json = MAPPER .writeValueAsString (p1 );
109
120
110
121
// and then verify that results make sense
111
- Map <String ,Object > map = mapper .readValue (json , Map .class );
122
+ Map <String ,Object > map = MAPPER .readValue (json , Map .class );
112
123
assertEquals ("John" , map .get ("name" ));
113
124
Object ob = map .get ("account" );
114
125
assertNotNull (ob );
@@ -131,11 +142,10 @@ public void testIssue468b() throws Exception
131
142
p2 .setAccounts (accounts );
132
143
133
144
// serialize without error:
134
- ObjectMapper mapper = new ObjectMapper ();
135
- String json = mapper .writeValueAsString (p2 );
145
+ String json = MAPPER .writeValueAsString (p2 );
136
146
137
147
// then verify output
138
- Map <String ,Object > map = mapper .readValue (json , Map .class );
148
+ Map <String ,Object > map = MAPPER .readValue (json , Map .class );
139
149
assertEquals ("John" , map .get ("name" ));
140
150
Object ob = map .get ("accounts" );
141
151
assertNotNull (ob );
@@ -145,14 +155,28 @@ public void testIssue468b() throws Exception
145
155
}
146
156
147
157
/**
148
- * Issue [JACKSON-572] is about unbound type variables, usually resulting
158
+ * Test related to unbound type variables, usually resulting
149
159
* from inner classes of generic classes (like Sets).
150
160
*/
151
- public void testUnboundIssue572 () throws Exception
161
+ public void testUnboundTypes () throws Exception
152
162
{
153
163
GenericBogusWrapper <Integer > list = new GenericBogusWrapper <Integer >(Integer .valueOf (7 ));
154
- String json = new ObjectMapper () .writeValueAsString (list );
164
+ String json = MAPPER .writeValueAsString (list );
155
165
assertEquals ("{\" wrapped\" :{\" value\" :7}}" , json );
156
166
}
157
- }
158
167
168
+ public void testRootTypeForCollections727 () throws Exception
169
+ {
170
+ List <Base727 > input = new ArrayList <Base727 >();
171
+ input .add (new Impl727 (1 , 2 ));
172
+
173
+ final String EXP = aposToQuotes ("[{'a':1,'b':2}]" );
174
+ // Without type enforcement, produces expected output:
175
+ assertEquals (EXP , MAPPER .writeValueAsString (input ));
176
+ assertEquals (EXP , MAPPER .writer ().writeValueAsString (input ));
177
+
178
+ // but enforcing type will hinder:
179
+ TypeReference <?> typeRef = new TypeReference <List <Base727 >>() { };
180
+ assertEquals (EXP , MAPPER .writer ().forType (typeRef ).writeValueAsString (input ));
181
+ }
182
+ }
0 commit comments