Skip to content

Add MessagePackMapper#handleBigIntegerAndBigDecimalAsString #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add MessagePackMapper#handleBigIntegerAndBigDecimalAsString
  • Loading branch information
komamitsu committed Sep 30, 2023
commit b9ccdf93cf81f8c72748ec31d8f9675c23bd9ac3
11 changes: 6 additions & 5 deletions msgpack-jackson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ Or more easily:
ObjectMapper objectMapper = new MessagePackMapper();
```

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.
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.

```java
ObjectMapper objectMapper = new MessagePackMapper().handleBigDecimalAsString();
ObjectMapper objectMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();
```

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

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

`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.
`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.

```java
ObjectMapper objectMapper = new MessagePackMapper().handleBigDecimalAsString();
ObjectMapper objectMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();

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

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

```java
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.configOverride(BigInteger.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
objectMapper.configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.cfg.MapperBuilder;

import java.math.BigDecimal;
import java.math.BigInteger;

public class MessagePackMapper extends ObjectMapper
{
Expand All @@ -43,12 +44,23 @@ public MessagePackMapper(MessagePackFactory f)
super(f);
}

public MessagePackMapper handleBigIntegerAsString()
{
configOverride(BigInteger.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
return this;
}

public MessagePackMapper handleBigDecimalAsString()
{
configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
return this;
}

public MessagePackMapper handleBigIntegerAndBigDecimalAsString()
{
return handleBigIntegerAsString().handleBigDecimalAsString();
}

public static Builder builder()
{
return new Builder(new MessagePackMapper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,97 @@
//
package org.msgpack.jackson.dataformat;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class MessagePackMapperTest
{
static class Pojo
static class PojoWithBigInteger
{
public BigInteger value;
}

static class PojoWithBigDecimal
{
public BigDecimal value;
}

@Test
public void handleBigDecimalAsString() throws IOException
private void assertToFailToHandleBigInteger(MessagePackMapper messagePackMapper) throws JsonProcessingException
{
PojoWithBigInteger obj = new PojoWithBigInteger();
obj.value = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(10));

try {
messagePackMapper.writeValueAsBytes(obj);
fail();
}
catch (IllegalArgumentException e) {
// Expected
}
}

private void assertToSuccessToHandleBigInteger(MessagePackMapper messagePackMapper) throws IOException
{
MessagePackMapper mapper = new MessagePackMapper().handleBigDecimalAsString();
Pojo obj = new Pojo();
PojoWithBigInteger obj = new PojoWithBigInteger();
obj.value = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(10));

byte[] converted = messagePackMapper.writeValueAsBytes(obj);

PojoWithBigInteger deserialized = messagePackMapper.readValue(converted, PojoWithBigInteger.class);
assertEquals(obj.value, deserialized.value);
}

private void assertToFailToHandleBigDecimal(MessagePackMapper messagePackMapper) throws JsonProcessingException
{
PojoWithBigDecimal obj = new PojoWithBigDecimal();
obj.value = new BigDecimal("1234567890.98765432100");

byte[] converted = mapper.writeValueAsBytes(obj);
try {
messagePackMapper.writeValueAsBytes(obj);
fail();
}
catch (IllegalArgumentException e) {
// Expected
}
}

Pojo deserialized = mapper.readValue(converted, Pojo.class);
private void assertToSuccessToHandleBigDecimal(MessagePackMapper messagePackMapper) throws IOException
{
PojoWithBigDecimal obj = new PojoWithBigDecimal();
obj.value = new BigDecimal("1234567890.98765432100");

byte[] converted = messagePackMapper.writeValueAsBytes(obj);

PojoWithBigDecimal deserialized = messagePackMapper.readValue(converted, PojoWithBigDecimal.class);
assertEquals(obj.value, deserialized.value);
}

@Test
public void handleBigIntegerAsString() throws IOException
{
assertToFailToHandleBigInteger(new MessagePackMapper());
assertToSuccessToHandleBigInteger(new MessagePackMapper().handleBigIntegerAsString());
}

@Test
public void handleBigDecimalAsString() throws IOException
{
assertToFailToHandleBigDecimal(new MessagePackMapper());
assertToSuccessToHandleBigDecimal(new MessagePackMapper().handleBigDecimalAsString());
}

@Test
public void handleBigIntegerAndBigDecimalAsString() throws IOException
{
MessagePackMapper messagePackMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();
assertToSuccessToHandleBigInteger(messagePackMapper);
assertToSuccessToHandleBigDecimal(messagePackMapper);
}
}
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