@@ -25,12 +25,29 @@ static class Data {
25
25
public long key ;
26
26
}
27
27
28
+ // Basic `ObjectWrapper` from base uses delegating ctor, won't work well; should
29
+ // figure out why, but until then we'll use separate impl
30
+ protected static class ObjectWrapperForPoly {
31
+ Object object ;
32
+
33
+ protected ObjectWrapperForPoly () { }
34
+ public ObjectWrapperForPoly (final Object o ) {
35
+ object = o ;
36
+ }
37
+ public Object getObject () { return object ; }
38
+ }
39
+
28
40
/*
29
41
/**********************************************************
30
42
/* Test methods
31
43
/**********************************************************
32
44
*/
33
-
45
+
46
+ private final ObjectMapper DEFAULT_TYPING_MAPPER = newObjectMapper ();
47
+ {
48
+ DEFAULT_TYPING_MAPPER .enableDefaultTyping ();
49
+ }
50
+
34
51
/**
35
52
* Unit test to verify that limited number of core types do NOT include
36
53
* type information, even if declared as Object. This is only done for types
@@ -39,45 +56,36 @@ static class Data {
39
56
*/
40
57
public void testNumericScalars () throws Exception
41
58
{
42
- ObjectMapper m = new ObjectMapper ();
43
- m .enableDefaultTyping ();
44
-
45
59
// no typing for Integer, Double, yes for others
46
- assertEquals ("[123]" , m .writeValueAsString (new Object [] { Integer .valueOf (123 ) }));
47
- assertEquals ("[[\" java.lang.Long\" ,37]]" , m .writeValueAsString (new Object [] { Long .valueOf (37 ) }));
48
- assertEquals ("[0.25]" , m .writeValueAsString (new Object [] { Double .valueOf (0.25 ) }));
49
- assertEquals ("[[\" java.lang.Float\" ,0.5]]" , m .writeValueAsString (new Object [] { Float .valueOf (0.5f ) }));
60
+ assertEquals ("[123]" , DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { Integer .valueOf (123 ) }));
61
+ assertEquals ("[[\" java.lang.Long\" ,37]]" , DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { Long .valueOf (37 ) }));
62
+ assertEquals ("[0.25]" , DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { Double .valueOf (0.25 ) }));
63
+ assertEquals ("[[\" java.lang.Float\" ,0.5]]" , DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { Float .valueOf (0.5f ) }));
50
64
}
51
65
52
66
public void testDateScalars () throws Exception
53
67
{
54
- ObjectMapper m = new ObjectMapper ();
55
- m .enableDefaultTyping ();
56
-
57
68
long ts = 12345678L ;
58
69
assertEquals ("[[\" java.util.Date\" ," +ts +"]]" ,
59
- m .writeValueAsString (new Object [] { new Date (ts ) }));
70
+ DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { new Date (ts ) }));
60
71
61
72
// Calendar is trickier... hmmh. Need to ensure round-tripping
62
73
Calendar c = Calendar .getInstance ();
63
74
c .setTimeInMillis (ts );
64
- String json = m .writeValueAsString (new Object [] { c });
75
+ String json = DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { c });
65
76
assertEquals ("[[\" " +c .getClass ().getName ()+"\" ," +ts +"]]" , json );
66
77
// and let's make sure it also comes back same way:
67
- Object [] result = m .readValue (json , Object [].class );
78
+ Object [] result = DEFAULT_TYPING_MAPPER .readValue (json , Object [].class );
68
79
assertEquals (1 , result .length );
69
80
assertTrue (result [0 ] instanceof Calendar );
70
81
assertEquals (ts , ((Calendar ) result [0 ]).getTimeInMillis ());
71
82
}
72
83
73
84
public void testMiscScalars () throws Exception
74
85
{
75
- ObjectMapper m = new ObjectMapper ();
76
- m .enableDefaultTyping ();
77
-
78
86
// no typing for Strings, booleans
79
- assertEquals ("[\" abc\" ]" , m .writeValueAsString (new Object [] { "abc" }));
80
- assertEquals ("[true,null,false]" , m .writeValueAsString (new Boolean [] { true , null , false }));
87
+ assertEquals ("[\" abc\" ]" , DEFAULT_TYPING_MAPPER .writeValueAsString (new Object [] { "abc" }));
88
+ assertEquals ("[true,null,false]" , DEFAULT_TYPING_MAPPER .writeValueAsString (new Boolean [] { true , null , false }));
81
89
}
82
90
83
91
/**
@@ -101,11 +109,9 @@ public void testScalarArrays() throws Exception
101
109
102
110
public void test417 () throws Exception
103
111
{
104
- ObjectMapper m = new ObjectMapper ();
105
- m .enableDefaultTyping ();
106
112
Jackson417Bean input = new Jackson417Bean ();
107
- String json = m .writeValueAsString (input );
108
- Jackson417Bean result = m .readValue (json , Jackson417Bean .class );
113
+ String json = DEFAULT_TYPING_MAPPER .writeValueAsString (input );
114
+ Jackson417Bean result = DEFAULT_TYPING_MAPPER .readValue (json , Jackson417Bean .class );
109
115
assertEquals (input .foo , result .foo );
110
116
assertEquals (input .bar , result .bar );
111
117
}
@@ -136,4 +142,15 @@ public void testDefaultTypingWithLong() throws Exception
136
142
assertNotNull (result );
137
143
assertEquals (2 , result .size ());
138
144
}
145
+
146
+ // [databind#2236]: do need type info for NaN
147
+ public void testDefaultTypingWithNaN () throws Exception
148
+ {
149
+ final ObjectWrapperForPoly INPUT = new ObjectWrapperForPoly (Double .POSITIVE_INFINITY );
150
+ final String json = DEFAULT_TYPING_MAPPER .writeValueAsString (INPUT );
151
+ final ObjectWrapperForPoly result = DEFAULT_TYPING_MAPPER .readValue (json , ObjectWrapperForPoly .class );
152
+ assertEquals (Double .class , result .getObject ().getClass ());
153
+ assertEquals (INPUT .getObject ().toString (), result .getObject ().toString ());
154
+ assertTrue (((Double ) result .getObject ()).isInfinite ());
155
+ }
139
156
}
0 commit comments