Skip to content

Commit

Permalink
support null data in nodes (#4380)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Feb 14, 2024
1 parent 901ba02 commit a9e9a8d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
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();
}
}
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());
}
}

0 comments on commit a9e9a8d

Please sign in to comment.