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.

Deserializes "null" to "0.0" in big-D Double

See original GitHub issue

When 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bill-phastcommented, Feb 26, 2021

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:

    /**
     * Jackson Jr. by default converts null to 0.0. We need to keep it as null.
     */
    private static final ValueReader DOUBLE_READER = new ValueReader(Double.class) {
        @Override
        public Object read(JSONReader reader, JsonParser jsonParser) throws IOException {
            return jsonParser.currentToken() == JsonToken.VALUE_NULL ? null : jsonParser.getValueAsDouble();
        }
    };

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.)

0reactions
cowtowncodercommented, Apr 18, 2021

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 ValueReaders. Not necessarily very difficult, just quite a bit of work.

Read more comments on GitHub >

github_iconTop 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 >

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