Added ShortNode
diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonNode.java b/src/main/java/com/fasterxml/jackson/databind/JsonNode.java
index ff4cbe6..8466521 100644
--- a/src/main/java/com/fasterxml/jackson/databind/JsonNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/JsonNode.java
@@ -225,6 +225,18 @@
/**
* Method that can be used to check whether contained value
+ * is a number represented as Java <code>short</code>.
+ * Note, however, that even if this method returns false, it
+ * is possible that conversion would be possible from other numeric
+ * types -- to check if this is possible, use
+ * {@link #canConvertToInt()} instead.
+ *
+ * @return True if the value contained by this node is stored as Java short
+ */
+ public boolean isShort() { return false; }
+
+ /**
+ * Method that can be used to check whether contained value
* is a number represented as Java <code>int</code>.
* Note, however, that even if this method returns false, it
* is possible that conversion would be possible from other numeric
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java b/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java
index c8a1e26..0d81624 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java
@@ -129,7 +129,7 @@
* Factory method for getting an instance of JSON numeric value
* that expresses given 16-bit integer value
*/
- public NumericNode numberNode(short v) { return IntNode.valueOf(v); }
+ public NumericNode numberNode(short v) { return ShortNode.valueOf(v); }
/**
* Alternate factory method that will handle wrapper value, which may
@@ -138,7 +138,7 @@
* {@link NumericNode}, but just {@link ValueNode}.
*/
public ValueNode numberNode(Short value) {
- return (value == null) ? nullNode() : IntNode.valueOf(value.shortValue());
+ return (value == null) ? nullNode() : ShortNode.valueOf(value);
}
/**
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ShortNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ShortNode.java
new file mode 100644
index 0000000..f9c5119
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/node/ShortNode.java
@@ -0,0 +1,109 @@
+package com.fasterxml.jackson.databind.node;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.core.io.NumberOutput;
+import com.fasterxml.jackson.databind.SerializerProvider;
+
+
+/**
+ * Numeric node that contains simple 16-bit integer values.
+ */
+public final class ShortNode
+ extends NumericNode
+{
+ final short _value;
+
+ /*
+ ************************************************
+ * Construction
+ ************************************************
+ */
+
+ public ShortNode(short v) { _value = v; }
+
+ public static ShortNode valueOf(short l) { return new ShortNode(l); }
+
+ /*
+ ************************************************
+ * Overridden JsonNode methods
+ ************************************************
+ */
+
+ @Override public JsonToken asToken() { return JsonToken.VALUE_NUMBER_INT; }
+
+ @Override
+ public JsonParser.NumberType numberType() { return JsonParser.NumberType.INT; } // TODO: should be SHORT
+
+
+ @Override
+ public boolean isIntegralNumber() { return true; }
+
+ @Override
+ public boolean isShort() { return true; }
+
+ @Override public boolean canConvertToInt() { return true; }
+ @Override public boolean canConvertToLong() { return true; }
+
+ @Override
+ public Number numberValue() {
+ return Short.valueOf(_value);
+ }
+
+ @Override
+ public short shortValue() { return _value; }
+
+ @Override
+ public int intValue() { return _value; }
+
+ @Override
+ public long longValue() { return _value; }
+
+ @Override
+ public float floatValue() { return _value; }
+
+ @Override
+ public double doubleValue() { return _value; }
+
+ @Override
+ public BigDecimal decimalValue() { return BigDecimal.valueOf(_value); }
+
+ @Override
+ public BigInteger bigIntegerValue() { return BigInteger.valueOf(_value); }
+
+ @Override
+ public String asText() {
+ return NumberOutput.toString(_value);
+ }
+
+ @Override
+ public boolean asBoolean(boolean defaultValue) {
+ return _value != 0;
+ }
+
+ @Override
+ public final void serialize(JsonGenerator jg, SerializerProvider provider)
+ throws IOException, JsonProcessingException
+ {
+ jg.writeNumber(_value);
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (o == this) return true;
+ if (o == null) return false;
+ if (o.getClass() != getClass()) { // final class, can do this
+ return false;
+ }
+ return ((ShortNode) o)._value == _value;
+ }
+
+ @Override
+ public int hashCode() {
+ return _value;
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java b/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java
index ffa9aaf..cdafa48 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java
@@ -15,7 +15,31 @@
*/
public class TestNumberNodes extends NodeTestBase
{
- public void testInt()
+ public void testShort()
+ {
+ ShortNode n = ShortNode.valueOf((short) 1);
+ assertStandardEquals(n);
+ assertTrue(0 != n.hashCode());
+// assertEquals(JsonToken.VALUE_NUMBER_SHORT, n.asToken()); // TODO: fix when available
+ assertEquals(JsonParser.NumberType.INT, n.numberType());
+ assertEquals(1, n.intValue());
+ assertEquals(1L, n.longValue());
+ assertEquals(BigDecimal.ONE, n.decimalValue());
+ assertEquals(BigInteger.ONE, n.bigIntegerValue());
+ assertEquals("1", n.asText());
+
+ assertNodeNumbers(n, 1, 1.0);
+
+ assertTrue(ShortNode.valueOf((short) 0).canConvertToInt());
+ assertTrue(ShortNode.valueOf(Short.MAX_VALUE).canConvertToInt());
+ assertTrue(ShortNode.valueOf(Short.MIN_VALUE).canConvertToInt());
+
+ assertTrue(ShortNode.valueOf((short) 0).canConvertToLong());
+ assertTrue(ShortNode.valueOf(Short.MAX_VALUE).canConvertToLong());
+ assertTrue(ShortNode.valueOf(Short.MIN_VALUE).canConvertToLong());
+ }
+
+ public void testInt()
{
IntNode n = IntNode.valueOf(1);
assertStandardEquals(n);