Skip to content

Commit 8d514ba

Browse files
authored
Add MessagePackMapper#handleBigIntegerAndBigDecimalAsString (#768)
* Add MessagePackMapper#handleBigIntegerAndBigDecimalAsString * Rename unit test method names
1 parent ce4410d commit 8d514ba

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

msgpack-jackson/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ Or more easily:
6464
ObjectMapper objectMapper = new MessagePackMapper();
6565
```
6666

67-
We strongly recommend to call `MessagePackMapper#handleBigDecimalAsString()` if you serialize and/or deserialize BigDecimal values. See [Serialize and deserialize BigDecimal as str type internally in MessagePack format](#serialize-and-deserialize-bigdecimal-as-str-type-internally-in-messagepack-format) for details.
67+
We strongly recommend to call `MessagePackMapper#handleBigIntegerAndBigDecimalAsString()` if you serialize and/or deserialize BigInteger/BigDecimal values. See [Serialize and deserialize BigDecimal as str type internally in MessagePack format](#serialize-and-deserialize-bigdecimal-as-str-type-internally-in-messagepack-format) for details.
6868

6969
```java
70-
ObjectMapper objectMapper = new MessagePackMapper().handleBigDecimalAsString();
70+
ObjectMapper objectMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();
7171
```
7272

7373
### Serialization/Deserialization of List
@@ -232,10 +232,10 @@ When you want to use non-String value as a key of Map, use `MessagePackKeySerial
232232

233233
### Serialize and deserialize BigDecimal as str type internally in MessagePack format
234234

235-
`jackson-dataformat-msgpack` represents BigDecimal values as float type in MessagePack format by default for backward compatibility. But the default behavior could fail when handling too large value for `double` type. So we strongly recommend to call `MessagePackMapper#handleBigDecimalAsString()` to internally handle BigDecimal values as String.
235+
`jackson-dataformat-msgpack` represents BigDecimal values as float type in MessagePack format by default for backward compatibility. But the default behavior could fail when handling too large value for `double` type. So we strongly recommend to call `MessagePackMapper#handleBigIntegerAndBigDecimalAsString()` to internally handle BigDecimal values as String.
236236

237237
```java
238-
ObjectMapper objectMapper = new MessagePackMapper().handleBigDecimalAsString();
238+
ObjectMapper objectMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();
239239

240240
Pojo obj = new Pojo();
241241
// This value is too large to be serialized as double
@@ -245,10 +245,11 @@ When you want to use non-String value as a key of Map, use `MessagePackKeySerial
245245

246246
System.out.println(objectMapper.readValue(converted, Pojo.class)); // => Pojo{value=1234567890.98765432100}
247247
```
248-
`MessagePackMapper#handleBigDecimalAsString()` is equivalent to the following configuration.
248+
`MessagePackMapper#handleBigIntegerAndDecimalAsString()` is equivalent to the following configuration.
249249

250250
```java
251251
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
252+
objectMapper.configOverride(BigInteger.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
252253
objectMapper.configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
253254
```
254255

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackMapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
2121

2222
import java.math.BigDecimal;
23+
import java.math.BigInteger;
2324

2425
public class MessagePackMapper extends ObjectMapper
2526
{
@@ -43,12 +44,23 @@ public MessagePackMapper(MessagePackFactory f)
4344
super(f);
4445
}
4546

47+
public MessagePackMapper handleBigIntegerAsString()
48+
{
49+
configOverride(BigInteger.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
50+
return this;
51+
}
52+
4653
public MessagePackMapper handleBigDecimalAsString()
4754
{
4855
configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
4956
return this;
5057
}
5158

59+
public MessagePackMapper handleBigIntegerAndBigDecimalAsString()
60+
{
61+
return handleBigIntegerAsString().handleBigDecimalAsString();
62+
}
63+
5264
public static Builder builder()
5365
{
5466
return new Builder(new MessagePackMapper());

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackMapperTest.java

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,97 @@
1515
//
1616
package org.msgpack.jackson.dataformat;
1717

18+
import com.fasterxml.jackson.core.JsonProcessingException;
1819
import org.junit.Test;
1920

2021
import java.io.IOException;
2122
import java.math.BigDecimal;
23+
import java.math.BigInteger;
2224

2325
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.fail;
2427

2528
public class MessagePackMapperTest
2629
{
27-
static class Pojo
30+
static class PojoWithBigInteger
31+
{
32+
public BigInteger value;
33+
}
34+
35+
static class PojoWithBigDecimal
2836
{
2937
public BigDecimal value;
3038
}
3139

32-
@Test
33-
public void handleBigDecimalAsString() throws IOException
40+
private void shouldFailToHandleBigInteger(MessagePackMapper messagePackMapper) throws JsonProcessingException
41+
{
42+
PojoWithBigInteger obj = new PojoWithBigInteger();
43+
obj.value = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(10));
44+
45+
try {
46+
messagePackMapper.writeValueAsBytes(obj);
47+
fail();
48+
}
49+
catch (IllegalArgumentException e) {
50+
// Expected
51+
}
52+
}
53+
54+
private void shouldSuccessToHandleBigInteger(MessagePackMapper messagePackMapper) throws IOException
3455
{
35-
MessagePackMapper mapper = new MessagePackMapper().handleBigDecimalAsString();
36-
Pojo obj = new Pojo();
56+
PojoWithBigInteger obj = new PojoWithBigInteger();
57+
obj.value = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(10));
58+
59+
byte[] converted = messagePackMapper.writeValueAsBytes(obj);
60+
61+
PojoWithBigInteger deserialized = messagePackMapper.readValue(converted, PojoWithBigInteger.class);
62+
assertEquals(obj.value, deserialized.value);
63+
}
64+
65+
private void shouldFailToHandleBigDecimal(MessagePackMapper messagePackMapper) throws JsonProcessingException
66+
{
67+
PojoWithBigDecimal obj = new PojoWithBigDecimal();
3768
obj.value = new BigDecimal("1234567890.98765432100");
3869

39-
byte[] converted = mapper.writeValueAsBytes(obj);
70+
try {
71+
messagePackMapper.writeValueAsBytes(obj);
72+
fail();
73+
}
74+
catch (IllegalArgumentException e) {
75+
// Expected
76+
}
77+
}
4078

41-
Pojo deserialized = mapper.readValue(converted, Pojo.class);
79+
private void shouldSuccessToHandleBigDecimal(MessagePackMapper messagePackMapper) throws IOException
80+
{
81+
PojoWithBigDecimal obj = new PojoWithBigDecimal();
82+
obj.value = new BigDecimal("1234567890.98765432100");
83+
84+
byte[] converted = messagePackMapper.writeValueAsBytes(obj);
85+
86+
PojoWithBigDecimal deserialized = messagePackMapper.readValue(converted, PojoWithBigDecimal.class);
4287
assertEquals(obj.value, deserialized.value);
4388
}
89+
90+
@Test
91+
public void handleBigIntegerAsString() throws IOException
92+
{
93+
shouldFailToHandleBigInteger(new MessagePackMapper());
94+
shouldSuccessToHandleBigInteger(new MessagePackMapper().handleBigIntegerAsString());
95+
}
96+
97+
@Test
98+
public void handleBigDecimalAsString() throws IOException
99+
{
100+
shouldFailToHandleBigDecimal(new MessagePackMapper());
101+
shouldSuccessToHandleBigDecimal(new MessagePackMapper().handleBigDecimalAsString());
102+
}
103+
104+
@Test
105+
public void handleBigIntegerAndBigDecimalAsString() throws IOException
106+
{
107+
MessagePackMapper messagePackMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();
108+
shouldSuccessToHandleBigInteger(messagePackMapper);
109+
shouldSuccessToHandleBigDecimal(messagePackMapper);
110+
}
44111
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy