Why is Double with zero decimal value is omitted during jackson serialization?
If you really need exactly two decimal places, you may try writing your own number serializer, like
public class DecimalJsonSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeString( String.format("%.2f", value));
}
}
Then use it annotating each field with it
@DecimalJsonSerializer(using=PriceJsonSerializer.class)
private Double _exclVat;
@DecimalJsonSerializer(using=PriceJsonSerializer.class)
private Double _inclVat;
No custom deserializer should be needed. Default deserializer should be able to handle numbers formatted in such way just fine.
How to display decimal in specific format in Jackson (for JSON)
One way of doing this is to use custom json serializer and specify in @JsonSerialize
.
@JsonSerialize(using = CustomDoubleSerializer.class)
public Double getAmount()
public class CustomDoubleSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
if (null == value) {
jgen.writeNull();
} else {
final String pattern = ".####";
final DecimalFormat myFormatter = new DecimalFormat(pattern);
final String output = myFormatter.format(value);
jgen.writeNumber(output);
}
}
}
Format a float as fixed point with Jackson
Building on @Veselin's answers I'm using
public class DoubleDecimalSerializerWithSixDigitPrecisionAndDotSeparator
extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator generator, SerializerProvider serializers)
throws IOException {
generator.writeNumber(String.format(Locale.US, "%.6f", value));
}
}
The use case is the generation of CSVs in Germany, so I don't care for JSON formatting and want a "." as a decimal separator.
Spring + Jackson, require trailing 0 on serialisation of double
in your custom serializer, just create a bigdecimal from your double, and specify the precision you want. Then just call writeNumber in the jsonGenerator.
Edit: Depending on the client being used to test results, 0s may be automatically removed from the "view". OP found that it was Postman (tool he was using to check results) the one removing 0s when displaying content, but the json he was generating was ok.
Jackson Mapper integer from json parsed as double with drong precision
There is no precision problem here. Every digit of the input appears in the output. You are merely seeing, but not recognizing, scientific notation for floating-point. The reason for that lies on your own code: you have an itemPrice
column declared as NUMERIC(10,2)
but you have mapped that to a Java Double
. This is not correct: it is causing this problem; and it will cause untold others further down the track.
The correct Java type to use for that is not Double
, or double
, but BigDecimal
.
Never use floating-point for money.
Related Topics
403 Forbidden When I Try to Post to My Spring API
Printing a Statement Only Once in a While Loop
How to Test Code Dependent on Environment Variables Using Junit
How to Fix Expected Begin_Object But Was String in Retrofit
How to Enable Request Scope in Async Task Executor
How to Format a String Number to Have Commas and Round
Java:How to Determine the Correct Charset Encoding of a Stream
How to Solve the "Failed to Lazily Initialize a Collection of Role" Hibernate Exception
Calculating the Sum of Number and Its Reverse in Java
Is There a Java Setting for Disabling Certificate Validation
Redirecting to a Servlet from a Jsp on Button Click
How to Create Unique Column With Jpa
Hibernate Error Executing Ddl Via Jdbc Statement
Netbeans Tomcat Build Failed - the Module Has Not Been Deployed