Skip to content

Commit

Permalink
Merge pull request #114 from FasterXML/tatu/2.17/78-number-wrappers-n…
Browse files Browse the repository at this point in the history
…ulls

Fixes #78: add wrapper types for most numbers
  • Loading branch information
cowtowncoder authored Feb 16, 2024
2 parents 1781de7 + 8e67685 commit c100cc6
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,24 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
writeBigIntegerField(fieldName, (BigInteger) value);
return;
case SER_NUMBER_FLOAT: // fall through
case SER_NUMBER_FLOAT_WRAPPER:
writeFloatField(fieldName, ((Float) value).floatValue());
return;
case SER_NUMBER_DOUBLE:
writeDoubleField(fieldName, ((Number) value).doubleValue());
case SER_NUMBER_DOUBLE_WRAPPER:
writeDoubleField(fieldName, ((Double) value).doubleValue());
return;
case SER_NUMBER_BYTE: // fall through
case SER_NUMBER_SHORT: // fall through
case SER_NUMBER_INTEGER:
writeIntField(fieldName, ((Number) value).intValue());
return;
case SER_NUMBER_INTEGER:
case SER_NUMBER_INTEGER_WRAPPER:
writeIntField(fieldName, ((Integer) value).intValue());
return;
case SER_NUMBER_LONG:
writeLongField(fieldName, ((Number) value).longValue());
case SER_NUMBER_LONG_WRAPPER:
writeLongField(fieldName, ((Long) value).longValue());
return;

// Scalar types:
Expand Down Expand Up @@ -328,16 +336,24 @@ protected void _writeValue(Object value, int type) throws IOException
// Number types:

case SER_NUMBER_FLOAT: // fall through
case SER_NUMBER_FLOAT_WRAPPER: // fall through
writeFloatValue(((Float) value).floatValue());
return;
case SER_NUMBER_DOUBLE:
writeDoubleValue(((Number) value).doubleValue());
case SER_NUMBER_DOUBLE_WRAPPER:
writeDoubleValue(((Double) value).doubleValue());
return;
case SER_NUMBER_BYTE: // fall through
case SER_NUMBER_SHORT: // fall through
case SER_NUMBER_INTEGER:
writeIntValue(((Number) value).intValue());
return;
case SER_NUMBER_INTEGER:
case SER_NUMBER_INTEGER_WRAPPER:
writeIntValue(((Integer) value).intValue());
return;
case SER_NUMBER_LONG:
writeLongValue(((Number) value).longValue());
case SER_NUMBER_LONG_WRAPPER:
writeLongValue(((Long) value).longValue());
return;
case SER_NUMBER_BIG_DECIMAL:
writeBigDecimalValue((BigDecimal) value);
Expand Down Expand Up @@ -584,6 +600,14 @@ protected void writeLongField(String fieldName, long v) throws IOException {
_generator.writeNumberField(fieldName, v);
}

protected void writeFloatValue(float v) throws IOException {
_generator.writeNumber(v);
}

protected void writeFloatField(String fieldName, float v) throws IOException {
_generator.writeNumberField(fieldName, v);
}

protected void writeDoubleValue(double v) throws IOException {
_generator.writeNumber(v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public Object readNext(JSONReader reader, JsonParser p) throws IOException
// cases default to "standard" handling which does range checks etc

case SER_NUMBER_INTEGER:
case SER_NUMBER_INTEGER_WRAPPER:
{
int i = p.nextIntValue(-2);
if (i != -2) {
Expand All @@ -69,6 +70,7 @@ public Object readNext(JSONReader reader, JsonParser p) throws IOException
}

case SER_NUMBER_LONG:
case SER_NUMBER_LONG_WRAPPER:
{
long l = p.nextLongValue(-2L);
if (l != -2L) {
Expand Down Expand Up @@ -116,8 +118,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException

// Number types:

case SER_NUMBER_FLOAT: // fall through
case SER_NUMBER_FLOAT_WRAPPER:
if (p.hasToken(JsonToken.VALUE_NULL)) {
return null;
}
case SER_NUMBER_FLOAT:
return Float.valueOf((float) p.getValueAsDouble());
case SER_NUMBER_DOUBLE_WRAPPER:
if (p.hasToken(JsonToken.VALUE_NULL)) {
return null;
}
case SER_NUMBER_DOUBLE:
return p.getValueAsDouble();

Expand All @@ -126,8 +136,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException

case SER_NUMBER_SHORT: // fall through
return (short) p.getValueAsInt();
case SER_NUMBER_INTEGER_WRAPPER:
if (p.hasToken(JsonToken.VALUE_NULL)) {
return null;
}
case SER_NUMBER_INTEGER:
return p.getValueAsInt();
case SER_NUMBER_LONG_WRAPPER:
if (p.hasToken(JsonToken.VALUE_NULL)) {
return null;
}
case SER_NUMBER_LONG:
return p.getValueAsLong();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,39 @@ public abstract class ValueLocatorBase
public final static int SER_NUMBER_BYTE = 13;

public final static int SER_NUMBER_SHORT = 14;

public final static int SER_NUMBER_INTEGER = 15;
public final static int SER_NUMBER_INTEGER_WRAPPER = 16;

public final static int SER_NUMBER_LONG = 16;
public final static int SER_NUMBER_LONG = 17;
public final static int SER_NUMBER_LONG_WRAPPER = 18;

public final static int SER_NUMBER_FLOAT = 17;
public final static int SER_NUMBER_FLOAT = 19;
public final static int SER_NUMBER_FLOAT_WRAPPER = 20;

public final static int SER_NUMBER_DOUBLE = 18;
public final static int SER_NUMBER_DOUBLE = 21;
public final static int SER_NUMBER_DOUBLE_WRAPPER = 22;

public final static int SER_NUMBER_BIG_INTEGER = 19;
public final static int SER_NUMBER_BIG_INTEGER = 23;

public final static int SER_NUMBER_BIG_DECIMAL = 20;
public final static int SER_NUMBER_BIG_DECIMAL = 24;

// // // Other specific scalar types

public final static int SER_BOOLEAN = 21;
public final static int SER_BOOLEAN_WRAPPER = 22;
public final static int SER_CHAR = 23;
public final static int SER_BOOLEAN = 25;
public final static int SER_BOOLEAN_WRAPPER = 26;
public final static int SER_CHAR = 27;

public final static int SER_ENUM = 24;
public final static int SER_ENUM = 28;

public final static int SER_DATE = 25;
public final static int SER_CALENDAR = 26;
public final static int SER_DATE = 29;
public final static int SER_CALENDAR = 30;

public final static int SER_CLASS = 27;
public final static int SER_FILE = 28;
public final static int SER_UUID = 29;
public final static int SER_URL = 30;
public final static int SER_URI = 31;
public final static int SER_CLASS = 31;
public final static int SER_FILE = 32;
public final static int SER_UUID = 33;
public final static int SER_URL = 34;
public final static int SER_URI = 35;


// // // Iterate-able types
Expand All @@ -115,7 +119,7 @@ public abstract class ValueLocatorBase
* Anything that implements {@link java.lang.Iterable}, but not
* {@link java.util.Collection}.
*/
public final static int SER_ITERABLE = 32;
public final static int SER_ITERABLE = 36;

/*
/**********************************************************************
Expand Down Expand Up @@ -174,16 +178,16 @@ protected int _findSimpleType(Class<?> raw, boolean forSer)
return SER_BOOLEAN_WRAPPER;
}
if (Number.class.isAssignableFrom(raw)) {
if (raw == Integer.class) return SER_NUMBER_INTEGER;
if (raw == Long.class) return SER_NUMBER_LONG;
if (raw == Byte.class) return SER_NUMBER_BYTE;
if (raw == Short.class) return SER_NUMBER_SHORT;
if (raw == Double.class) return SER_NUMBER_DOUBLE;
if (raw == Float.class) return SER_NUMBER_FLOAT;
if (raw == Integer.class) return SER_NUMBER_INTEGER_WRAPPER;
if (raw == Long.class) return SER_NUMBER_LONG_WRAPPER;
if (raw == Double.class) return SER_NUMBER_DOUBLE_WRAPPER;
if (raw == Float.class) return SER_NUMBER_FLOAT_WRAPPER;
if (raw == BigDecimal.class) return SER_NUMBER_BIG_DECIMAL;
if (raw == BigInteger.class) {
return SER_NUMBER_BIG_INTEGER;
}
if (raw == Byte.class) return SER_NUMBER_BYTE;
if (raw == Short.class) return SER_NUMBER_SHORT;
// What numeric type is this? Could consider "string-like" but...
return SER_UNKNOWN;
}
Expand Down

This file was deleted.

Loading

0 comments on commit c100cc6

Please sign in to comment.