Skip to content

Commit

Permalink
More coercions, now int (leaves long, float and double)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 13, 2020
1 parent 359ad71 commit 7678ce4
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
return Boolean.FALSE;
}
if (_primitive) {
return _parseBooleanPrimitive(ctxt, p, _valueClass);
return _parseBooleanPrimitive(ctxt, p);
}
return _parseBoolean(ctxt, p, _valueClass);
}
Expand All @@ -240,7 +240,7 @@ public Boolean deserializeWithType(JsonParser p, DeserializationContext ctxt,
return Boolean.FALSE;
}
if (_primitive) {
return _parseBooleanPrimitive(ctxt, p, _valueClass);
return _parseBooleanPrimitive(ctxt, p);
}
return _parseBoolean(ctxt, p, _valueClass);
}
Expand All @@ -267,7 +267,7 @@ public Byte deserialize(JsonParser p, DeserializationContext ctxt) throws IOExce
return p.getByteValue();
}
if (_primitive) {
return _parseBytePrimitive(ctxt, p, _valueClass);
return _parseBytePrimitive(ctxt, p);
}
return _parseByte(ctxt, p, _valueClass);
}
Expand Down Expand Up @@ -295,7 +295,7 @@ public Short deserialize(JsonParser p, DeserializationContext ctxt)
return p.getShortValue();
}
if (_primitive) {
return _parseShortPrimitive(ctxt, p, _valueClass);
return _parseShortPrimitive(ctxt, p);
}
return _parseShort(ctxt, p, _valueClass);
}
Expand Down Expand Up @@ -384,7 +384,10 @@ public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return p.getIntValue();
}
return _parseInteger(p, ctxt);
if (_primitive) {
return _parseIntPrimitive(ctxt, p);
}
return _parseInteger(ctxt, p, _valueClass);
}

// Since we can never have type info ("natural type"; String, Boolean, Integer, Double):
Expand All @@ -396,56 +399,10 @@ public Integer deserializeWithType(JsonParser p, DeserializationContext ctxt,
if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return p.getIntValue();
}
return _parseInteger(p, ctxt);
}

protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt) throws IOException
{
switch (p.currentTokenId()) {
// NOTE: caller assumed to usually check VALUE_NUMBER_INT in fast path
case JsonTokenId.ID_NUMBER_INT:
return Integer.valueOf(p.getIntValue());
case JsonTokenId.ID_NUMBER_FLOAT: // coercing may work too
if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
_failDoubleToIntCoercion(p, ctxt, "Integer");
}
return Integer.valueOf(p.getValueAsInt());
case JsonTokenId.ID_STRING: // let's do implicit re-parse
String text = p.getText();
CoercionAction act = _checkFromStringCoercion(ctxt, text);
if (act == CoercionAction.AsNull) {
return (Integer) getNullValue(ctxt);
}
if (act == CoercionAction.AsEmpty) {
return (Integer) getEmptyValue(ctxt);
}
text = text.trim();
if (_hasTextualNull(text)) {
return (Integer) _coerceTextualNull(ctxt, _primitive);
}
final int len = text.length();
try {
if (len > 9) {
long l = Long.parseLong(text);
if (_intOverflow(l)) {
return (Integer) ctxt.handleWeirdStringValue(_valueClass, text, String.format(
"Overflow: numeric value (%s) out of range of Integer (%d - %d)",
text, Integer.MIN_VALUE, Integer.MAX_VALUE));
}
return Integer.valueOf((int) l);
}
return Integer.valueOf(NumberInput.parseInt(text));
} catch (IllegalArgumentException iae) {
return (Integer) ctxt.handleWeirdStringValue(_valueClass, text,
"not a valid Integer value");
}
case JsonTokenId.ID_NULL:
return (Integer) _coerceNullToken(ctxt, _primitive);
case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(p, ctxt);
if (_primitive) {
return _parseIntPrimitive(ctxt, p);
}
// Otherwise, no can do:
return (Integer) ctxt.handleUnexpectedToken(_valueClass, p);
return _parseInteger(ctxt, p, _valueClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public boolean[] deserialize(JsonParser p, DeserializationContext ctxt)
_verifyNullForPrimitive(ctxt);
value = false;
} else {
value = _parseBooleanPrimitive(ctxt, p, Boolean.TYPE);
value = _parseBooleanPrimitive(ctxt, p);
}
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
Expand All @@ -409,7 +409,7 @@ public boolean[] deserialize(JsonParser p, DeserializationContext ctxt)
@Override
protected boolean[] handleSingleElementUnwrapped(JsonParser p,
DeserializationContext ctxt) throws IOException {
return new boolean[] { _parseBooleanPrimitive(ctxt, p, Boolean.TYPE) };
return new boolean[] { _parseBooleanPrimitive(ctxt, p) };
}

@Override
Expand Down Expand Up @@ -508,7 +508,7 @@ public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
_verifyNullForPrimitive(ctxt);
value = (byte) 0;
} else {
value = _parseBytePrimitive(ctxt, p, Byte.TYPE);
value = _parseBytePrimitive(ctxt, p);
}
}
if (ix >= chunk.length) {
Expand Down Expand Up @@ -602,7 +602,7 @@ public short[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
_verifyNullForPrimitive(ctxt);
value = (short) 0;
} else {
value = _parseShortPrimitive(ctxt, p, Short.TYPE);
value = _parseShortPrimitive(ctxt, p);
}
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
Expand All @@ -619,7 +619,7 @@ public short[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
@Override
protected short[] handleSingleElementUnwrapped(JsonParser p,
DeserializationContext ctxt) throws IOException {
return new short[] { _parseShortPrimitive(ctxt, p, Short.TYPE) };
return new short[] { _parseShortPrimitive(ctxt, p) };
}

@Override
Expand Down Expand Up @@ -680,7 +680,7 @@ public int[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOExc
_verifyNullForPrimitive(ctxt);
value = 0;
} else {
value = _parseIntPrimitive(p, ctxt);
value = _parseIntPrimitive(ctxt, p);
}
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
Expand All @@ -697,7 +697,7 @@ public int[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOExc
@Override
protected int[] handleSingleElementUnwrapped(JsonParser p,
DeserializationContext ctxt) throws IOException {
return new int[] { _parseIntPrimitive(p, ctxt) };
return new int[] { _parseIntPrimitive(ctxt, p) };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public StackTraceElement deserialize(JsonParser p, DeserializationContext ctxt)
if (t.isNumeric()) {
lineNumber = p.getIntValue();
} else {
lineNumber = _parseIntPrimitive(p, ctxt);
lineNumber = _parseIntPrimitive(ctxt, p);
}
} else if ("methodName".equals(propName)) {
methodName = p.getText();
Expand Down
Loading

0 comments on commit 7678ce4

Please sign in to comment.