4
4
5
5
import static org .junit .Assert .*;
6
6
7
+ import com .fasterxml .jackson .annotation .JsonTypeInfo ;
7
8
import com .fasterxml .jackson .databind .*;
9
+ import com .fasterxml .jackson .databind .jsontype .impl .StdTypeResolverBuilder ;
8
10
9
11
/**
10
12
* Unit tests to verify that Java/JSON scalar values (non-structured values)
@@ -18,9 +20,14 @@ static class Jackson417Bean {
18
20
public java .io .Serializable bar = new Integer (13 );
19
21
}
20
22
23
+ // [databind#1395]: prevent attempts at including type info for primitives
24
+ static class Data {
25
+ public long key ;
26
+ }
27
+
21
28
/*
22
29
/**********************************************************
23
- /* Unit tests
30
+ /* Test methods
24
31
/**********************************************************
25
32
*/
26
33
@@ -92,9 +99,6 @@ public void testScalarArrays() throws Exception
92
99
assertArrayEquals (input , output );
93
100
}
94
101
95
- /**
96
- * Loosely scalar; for [JACKSON-417]
97
- */
98
102
public void test417 () throws Exception
99
103
{
100
104
ObjectMapper m = new ObjectMapper ();
@@ -105,4 +109,31 @@ public void test417() throws Exception
105
109
assertEquals (input .foo , result .foo );
106
110
assertEquals (input .bar , result .bar );
107
111
}
112
+
113
+ // [databind#1395]: prevent attempts at including type info for primitives
114
+ public void testDefaultTypingWithLong () throws Exception
115
+ {
116
+ Data data = new Data ();
117
+ data .key = 1L ;
118
+ Map <String , Object > mapData = new HashMap <String , Object >();
119
+ mapData .put ("longInMap" , 2L );
120
+ mapData .put ("longAsField" , data );
121
+
122
+ // Configure Jackson to preserve types
123
+ ObjectMapper mapper = new ObjectMapper ();
124
+ StdTypeResolverBuilder resolver = new StdTypeResolverBuilder ();
125
+ resolver .init (JsonTypeInfo .Id .CLASS , null );
126
+ resolver .inclusion (JsonTypeInfo .As .PROPERTY );
127
+ resolver .typeProperty ("__t" );
128
+ mapper .setDefaultTyping (resolver );
129
+ mapper .enable (SerializationFeature .INDENT_OUTPUT );
130
+
131
+ // Serialize
132
+ String json = mapper .writeValueAsString (mapData );
133
+
134
+ // Deserialize
135
+ Map <?,?> result = mapper .readValue (json , Map .class );
136
+ assertNotNull (result );
137
+ assertEquals (2 , result .size ());
138
+ }
108
139
}
0 commit comments