Skip to content
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

support null data in nodes #4380

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Objects;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand Down Expand Up @@ -113,11 +114,12 @@ public boolean equals(Object o)
if (!(o instanceof BigIntegerNode)) {
return false;
}
return ((BigIntegerNode) o)._value.equals(_value);
BigIntegerNode otherNode = (BigIntegerNode) o;
return Objects.equals(otherNode._value, _value);
}

@Override
public int hashCode() {
return _value.hashCode();
return Objects.hashCode(_value);
}
}
16 changes: 14 additions & 2 deletions src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,23 @@ public boolean equals(Object o)
if (o == this) return true;
if (o == null) return false;
if (o instanceof DecimalNode) {
return ((DecimalNode) o)._value.compareTo(_value) == 0;
DecimalNode otherNode = (DecimalNode) o;
if (otherNode._value == null) {
return _value == null;
} else if (_value == null) {
return false;
}
return otherNode._value.compareTo(_value) == 0;
}
return false;
}

@Override
public int hashCode() { return Double.valueOf(doubleValue()).hashCode(); }
public int hashCode() {
if (_value == null) {
// we need a stable hash code for _value == null
return 0;
}
return Double.valueOf(doubleValue()).hashCode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh? Oh, that's how code already was. Ok. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But not sure why not use BigDecimal.hashCode(), fwtw.
Maybe something else to change in.... 2.17 I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-existing equals method is odd as well. It would be easier to use .equals on the BigDecimal instances.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compareTo and the special hashCode are necessary here because BigDecimal equality is weird wrt scaling. 1.9 and 1.90 are considered unequal by BigDecimal but equal by this DecimalNode impl.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok; will then leave that as-is.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fasterxml.jackson.databind.node;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class NullDataEqualsTest {
@Test
void testNullBinaryNode() {
assertEquals(new BinaryNode(null), new BinaryNode(null));
assertNotEquals(new BinaryNode(new byte[8]), new BinaryNode(null));
assertNotEquals(new BinaryNode(null), new BinaryNode(new byte[8]));
assertEquals(-1, new BinaryNode(null).hashCode());
}

@Test
void testNullBigIntegerNode() {
assertEquals(new BigIntegerNode(null), new BigIntegerNode(null));
assertNotEquals(new BigIntegerNode(BigInteger.ZERO), new BigIntegerNode(null));
assertNotEquals(new BigIntegerNode(null), new BigIntegerNode(BigInteger.ZERO));
assertEquals(0, new BigIntegerNode(null).hashCode());
}

@Test
void testNullDecimalNode() {
assertEquals(new DecimalNode(null), new DecimalNode(null));
assertNotEquals(new DecimalNode(BigDecimal.ZERO), new DecimalNode(null));
assertNotEquals( new DecimalNode(null), new DecimalNode(BigDecimal.ZERO));
assertEquals(0, new DecimalNode(null).hashCode());
}
}
Loading