Skip to content

DecimalNode and equality: on purpose or not, I don't know, but... #80

Closed
@fge

Description

@fge

If I create an ObjectMapper with feature DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, and create two nodes as follows:

// mapper is the ObjectMapper
final JsonNode n1 = mapper.readTree("1.0");
final JsonNode n2 = mapper.readTree("1.00");

then ·n1.equals(n2) returns false. BigDecimal is quite awkward in this regard since its .compareTo() is not consistent with .equals() (and the two BigDecimals created with the two strings above are actually not equal). But now, is this the correct thing to do?

I extracted DecimalNode from the source code and tried, in the .equals() method, to replace .equals() with .compareTo() == 0 for the decimal values -- but that did not work!

What did work and rendered these two nodes equal was to modify constructors so that they read:

    public DecimalNode(BigDecimal v)
    {
        _value = v.stripTrailingZeros();
    }

    public static DecimalNode valueOf(BigDecimal d)
    {
        return new DecimalNode(d);
    }

I am not sure whether this is the desired behaviour for DecimalNode, however. But what I don't understand is why replacing .equals(_value) with .compareTo(_value) == 0 in DecimalNode's .equals() didn't work...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions