Deserializes "null" to "0.0" in big-D Double
See original GitHub issueWhen a bean has a big-d Double, and the value is null, it gets decoded as 0.0 instead. The two are not equivalent.
This code shows the problem:
package test;
import com.fasterxml.jackson.jr.ob.JSON;
import java.util.List;
public class DoubleTest {
private Double value;
public static void main(String[] args) throws Exception {
for (var testInput: List.of(
"{\"value\": 1.0}", "{\"value\": 0.0}", "{}", "{\"value\": null}")) {
System.out.println("Input JSON: " + testInput + " - Output bean: " + JSON.std.beanFrom(DoubleTest.class, testInput));
}
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
public @Override String toString() {
return "DoubleTest[" + value + "]";
}
}
Output:
Input JSON: {"value": 1.0} - Output bean: DoubleTest[1.0]
Input JSON: {"value": 0.0} - Output bean: DoubleTest[0.0]
Input JSON: {} - Output bean: DoubleTest[null]
Input JSON: {"value": null} - Output bean: DoubleTest[0.0]
The fourth JSON object should be deserialized the same as the third.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Jackson coercing to zero - java - Stack Overflow
I know that empty value "" is not valid, but client framework collect values as they are presented in UI and if user...
Read more >Converting String to BigDecimal in Java - Baeldung
The second step is to convert Double to BigDecimal: ... String pattern = "#,##0.0#"; DecimalFormat decimalFormat = new DecimalFormat(pattern ...
Read more >API Reference — Jansson 2.8 documentation
Depending on the function's signature, the error value is either NULL or -1. Invalid arguments or invalid input are apparent sources for errors....
Read more >MessagePack: It's like JSON. but fast and small.
Serialize.serialize_string (`FixArray [`PFixnum 1; `PFixnum 2; `PFixnum 3]) (* deserialize *) let obj = Msgpack.Serialize.deserialize_string bytes.
Read more >DeserializationFeature (jackson-databind 2.9.0 API) - FasterXML
Feature that determines whether encountering of JSON null is an error when deserializing into Java primitive types (like 'int' or 'double').
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m actually on 2.12.1, sorry I didn’t know 2.12.2 was out. I found a workaround; add a reader/writer provider that provides this object when a reader for Double.class is needed:
But it seems it would be best to make this workaround unnecessary, to just preserve nulls. (I have verified that yes, Integer, Boolean, Float, and Character, all have this problem, all can be fixed by the same workaround technique. I guess I have some of complex beans.)
2.13 branch is open, so PRs are possible against.
Unfortunately I don’t think I will have time to work on this problem in near future. I think that to make this change, new type constants would be needed for standard deserializers (one for each primitive/wrapper case), and then matching changes to
ValueReader
s. Not necessarily very difficult, just quite a bit of work.