Skip to content

Commit 4f6579f

Browse files
authored
Merge pull request #651 from msgpack/doc-jackson-bigdecimal-str
Add how to internally represent BigDecimal as str to the doc
2 parents ec9b958 + 4c5d180 commit 4f6579f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

msgpack-jackson/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,22 @@ When you want to use non-String value as a key of Map, use `MessagePackKeySerial
224224
System.out.println(deserialized); // => {42=Hello}
225225
```
226226

227+
### Serialize and deserialize BigDecimal as str type internally in MessagePack format
228+
229+
`jackson-dataformat-msgpack` represents BigDecimal values as float type in MessagePack format by default. When you want to handle BigDeciaml values as str type with arbitrary precision in MessagePack format, you can use `com.fasterxml.jackson.databind.cfg.MutableConfigOverride#setFormat` like this:
230+
231+
```java
232+
ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
233+
mapper.configOverride(BigDecimal.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
234+
235+
Pojo obj = new Pojo();
236+
obj.value = new BigDecimal("1234567890.98765432100");
237+
238+
byte[] converted = mapper.writeValueAsBytes(obj);
239+
240+
System.out.println(mapper.readValue(converted, Pojo.class)); // => Pojo{value=1234567890.98765432100}
241+
```
242+
227243
### Deserialize extension types with ExtensionTypeCustomDeserializers
228244

229245
`ExtensionTypeCustomDeserializers` helps you to deserialize extension types easily.

0 commit comments

Comments
 (0)