1
1
package com .fasterxml .jackson .dataformat .cbor .mapper ;
2
2
3
+ import java .io .ByteArrayOutputStream ;
4
+ import java .math .BigDecimal ;
5
+ import java .math .BigInteger ;
6
+
7
+ import com .fasterxml .jackson .annotation .JsonUnwrapped ;
8
+
3
9
import com .fasterxml .jackson .databind .ObjectMapper ;
10
+
11
+ import com .fasterxml .jackson .dataformat .cbor .CBORGenerator ;
4
12
import com .fasterxml .jackson .dataformat .cbor .CBORTestBase ;
5
13
6
14
public class NumberBeanTest extends CBORTestBase
@@ -19,6 +27,23 @@ static class LongBean {
19
27
protected LongBean () { }
20
28
}
21
29
30
+ static class NumberWrapper {
31
+ public Number nr ;
32
+ }
33
+
34
+ // // Copied form "JDKNumberDeserTest", related to BigDecimal precision
35
+ // // retaining through buffering
36
+
37
+ // [databind#2784]
38
+ static class BigDecimalHolder2784 {
39
+ public BigDecimal value ;
40
+ }
41
+
42
+ static class NestedBigDecimalHolder2784 {
43
+ @ JsonUnwrapped
44
+ public BigDecimalHolder2784 holder ;
45
+ }
46
+
22
47
/*
23
48
/**********************************************************
24
49
/* Test methods
@@ -46,15 +71,28 @@ public void testLongRoundTrip() throws Exception
46
71
100L , -200L ,
47
72
5000L , -3600L ,
48
73
Integer .MIN_VALUE , Integer .MAX_VALUE ,
49
- 1L + Integer .MAX_VALUE , -1L + Integer .MIN_VALUE ,
50
- 2330462449L , // from [dataformats-binary#30]
51
- Long .MIN_VALUE , Long .MAX_VALUE
52
- }) {
53
- LongBean input = new LongBean (v );
54
- byte [] b = MAPPER .writeValueAsBytes (input );
55
- LongBean result = MAPPER .readValue (b , LongBean .class );
56
- assertEquals (input .value , result .value );
74
+ 1L + Integer .MAX_VALUE , -1L + Integer .MIN_VALUE
75
+ }) {
76
+ _testLongRoundTrip (v );
57
77
}
78
+
79
+ _testLongRoundTrip (2330462449L ); // from [dataformats-binary#30]
80
+ _testLongRoundTrip (0xFFFFFFFFL ); // max positive uint32
81
+ _testLongRoundTrip (-0xFFFFFFFFL );
82
+ _testLongRoundTrip (0x100000000L );
83
+ _testLongRoundTrip (-0x100000000L );
84
+ _testLongRoundTrip (0x100000001L );
85
+ _testLongRoundTrip (-0x100000001L );
86
+ _testLongRoundTrip (Long .MIN_VALUE );
87
+ _testLongRoundTrip (Long .MAX_VALUE );
88
+ }
89
+
90
+ private void _testLongRoundTrip (long v ) throws Exception
91
+ {
92
+ LongBean input = new LongBean (v );
93
+ byte [] b = MAPPER .writeValueAsBytes (input );
94
+ LongBean result = MAPPER .readValue (b , LongBean .class );
95
+ assertEquals (input .value , result .value );
58
96
}
59
97
60
98
// for [dataformats-binary#32] coercion of Float into Double
@@ -67,4 +105,87 @@ public void testUntypedWithFloat() throws Exception
67
105
assertEquals (Float .class , result [0 ].getClass ());
68
106
assertEquals (input [0 ], result [0 ]);
69
107
}
108
+
109
+ public void testNumberTypeRetainingInt () throws Exception
110
+ {
111
+ NumberWrapper result ;
112
+ ByteArrayOutputStream bytes ;
113
+
114
+ bytes = new ByteArrayOutputStream ();
115
+ try (CBORGenerator g = cborGenerator (bytes )) {
116
+ g .writeStartObject ();
117
+ g .writeNumberField ("nr" , 123 );
118
+ g .writeEndObject ();
119
+ }
120
+ result = MAPPER .readValue (bytes .toByteArray (), NumberWrapper .class );
121
+ assertEquals (Integer .valueOf (123 ), result .nr );
122
+
123
+ bytes = new ByteArrayOutputStream ();
124
+ try (CBORGenerator g = cborGenerator (bytes )) {
125
+ g .writeStartObject ();
126
+ g .writeNumberField ("nr" , Long .MAX_VALUE );
127
+ g .writeEndObject ();
128
+ }
129
+ result = MAPPER .readValue (bytes .toByteArray (), NumberWrapper .class );
130
+ assertEquals (Long .valueOf (Long .MAX_VALUE ), result .nr );
131
+
132
+ bytes = new ByteArrayOutputStream ();
133
+ try (CBORGenerator g = cborGenerator (bytes )) {
134
+ g .writeStartObject ();
135
+ g .writeNumberField ("nr" , BigInteger .valueOf (-42L ));
136
+ g .writeEndObject ();
137
+ }
138
+ result = MAPPER .readValue (bytes .toByteArray (), NumberWrapper .class );
139
+ assertEquals (BigInteger .valueOf (-42L ), result .nr );
140
+ }
141
+
142
+ public void testNumberTypeRetainingFP () throws Exception
143
+ {
144
+ NumberWrapper result ;
145
+ ByteArrayOutputStream bytes ;
146
+
147
+ bytes = new ByteArrayOutputStream ();
148
+ try (CBORGenerator g = cborGenerator (bytes )) {
149
+ g .writeStartObject ();
150
+ g .writeNumberField ("nr" , 0.25f );
151
+ g .writeEndObject ();
152
+ }
153
+ result = MAPPER .readValue (bytes .toByteArray (), NumberWrapper .class );
154
+ assertEquals (Float .valueOf (0.25f ), result .nr );
155
+
156
+ bytes = new ByteArrayOutputStream ();
157
+ try (CBORGenerator g = cborGenerator (bytes )) {
158
+ g .writeStartObject ();
159
+ g .writeNumberField ("nr" , 0.5 );
160
+ g .writeEndObject ();
161
+ }
162
+ result = MAPPER .readValue (bytes .toByteArray (), NumberWrapper .class );
163
+ assertEquals (Double .valueOf (0.5 ), result .nr );
164
+
165
+ bytes = new ByteArrayOutputStream ();
166
+ try (CBORGenerator g = cborGenerator (bytes )) {
167
+ g .writeStartObject ();
168
+ g .writeNumberField ("nr" , new BigDecimal ("0.100" ));
169
+ g .writeEndObject ();
170
+ }
171
+ result = MAPPER .readValue (bytes .toByteArray (), NumberWrapper .class );
172
+ assertEquals (new BigDecimal ("0.100" ), result .nr );
173
+ }
174
+
175
+ // [databind#2784]
176
+ public void testBigDecimalWithBuffering () throws Exception
177
+ {
178
+ final BigDecimal VALUE = new BigDecimal ("5.00" );
179
+ // Need to generate by hand since JSON would not indicate desire for BigDecimal
180
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
181
+ try (CBORGenerator g = cborGenerator (bytes )) {
182
+ g .writeStartObject ();
183
+ g .writeNumberField ("value" , VALUE );
184
+ g .writeEndObject ();
185
+ }
186
+
187
+ NestedBigDecimalHolder2784 result = MAPPER .readValue (bytes .toByteArray (),
188
+ NestedBigDecimalHolder2784 .class );
189
+ assertEquals (VALUE , result .holder .value );
190
+ }
70
191
}
0 commit comments