-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Precision lost on BigDecimals #2618
Comments
I don't think this has anything to do with Micronaut. You need to configure Jackson to modify this behavior |
Maybe we should include documentation at least |
In the sample application referenced above the following has been configured
The following test passes
The following test fails
|
import java.math.BigDecimal;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
var mapper = new ObjectMapper().configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
var test = new Test(new BigDecimal("0.0000000005"));
System.out.println(mapper.writeValueAsString(test));
}
}
class Test {
private final BigDecimal value;
Test(BigDecimal value) {
this.value = value;
}
@JsonFormat(shape= JsonFormat.Shape.STRING)
public BigDecimal getValue() {
return value;
}
} With With |
Firstly, thank you everyone for your responses and your help. One thing I'm confused by here is that if this was a Jackson configuration issue wouldn't the unit test where the ObjectMapper is retrieved from the context also be failing with the same error in the attached application? I think that the Micronaut JacksonProcessor is being told to parse this data as a double instead of a BigDecimal. I suspect that this may be because the JsonFactory used there is getting newed up (JsonContentProcessor) instead of supplied and losing all configured properties (like how to handle BigDecimals)?
|
Task List
Precision lost on BigDecimals on deserialization. Issues was encountered using Flowables (rx java) but below example also demonstrates the problem.
Steps to Reproduce
implementation("org.apache.httpcomponents:httpclient:4.5.10")
Expected Behaviour
The precision is retained between the POST/GET of the BigDecimals
Actual Behaviour
The BigDecimal is converted to a Double and precision is lost
Expected: <{string=string, bigDecimal=888.7794538169553400000}>
but: was <{string=string, bigDecimal=888.7794538169553}>
Environment Information
Example Application
https://github.com/chosegood/micronaut-bugreport-bigdecimal
The text was updated successfully, but these errors were encountered: