question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN` not taken into account when serializing `BigDecimal` as String

See original GitHub issue

The following test should convert a BigDecimal number to "3.033" string in json. But in fact it only converts it to double.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;

import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;

public class JacksonTest {
	public class MyDto {
		private BigDecimal number;

		public BigDecimal getNumber() {
			return number;
		}

		public void setNumber(BigDecimal number) {
			this.number = number;
		}
	}

	@Test
	public void testBigDecimalSerialization() throws Exception {
		ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN);
		MyDto dto = new MyDto();
		dto.number = new BigDecimal("3.033");
		String json = mapper.writeValueAsString(dto);

		assertEquals("{\"number\":\"3.033\"}", json);
	}
}

Result:

org.junit.ComparisonFailure: 
Expected :{"number":"3.033"}
Actual   :{"number":3.033}

jackson-databind-2.9.8

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jul 24, 2019

Actually, I think it already works; fixed with #2230, to be included in 2.10 (and is included in 2.10.0.pr1 already).

1reaction
cowtowncodercommented, May 11, 2019

@sddakoty Almost, but you can actually remove that if check there: serialize() method is never called with null value (null handling is separate and caller must handle it).

And yes I hope I find time to implement this in near future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to format BigDecimal as "plain text" when serializing a ...
Hi ! In Jackson, there is a settings to output a BigDecimal as a plain string instead of the scientific annotation : SerializationFeature....
Read more >
Java to Jackson JSON serialization: Money fields
In the above example, I used the to string serializer to serialize BigIntegers (since javascript can not handle such numeric values).
Read more >
SerializationFeature (jackson-databind 2.2.0 API) - FasterXML
Feature that determines whether BigDecimal entries are serialized using BigDecimal.toPlainString() to prevent values to be written using scientific notation ...
Read more >
The Configure() Method in Jackson in JSON - DZone Java
In this tutorial, learn how to use the configure() method in Jackson in JSON. ... Will write BigDecimal as plain text using the...
Read more >
DeserializationFeature (jackson-databind 2.12.3 API)
BigDecimal `) is allowed or not. ACCEPT_SINGLE_VALUE_AS_ARRAY. Feature that determines whether it is acceptable to coerce non-array (in JSON) values to work with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found