Skip to content

Commit

Permalink
Last piece of #443: Smile (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 7, 2024
1 parent da95300 commit 2268a91
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.math.BigInteger;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonParser.NumberTypeFP;
import com.fasterxml.jackson.core.base.ParserMinimalBase;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.ContentReference;
Expand Down Expand Up @@ -486,6 +487,26 @@ public final NumberType getNumberType() throws IOException
return _numberType;
}

@Override // since 2.17
public NumberTypeFP getNumberTypeFP() throws IOException {
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
// Some decoding is done lazily so need to:
if (_numTypesValid == NR_UNKNOWN) {
_parseNumericValue(); // will also check event type
}
if (_numberType == NumberType.BIG_DECIMAL) {
return NumberTypeFP.BIG_DECIMAL;
}
if (_numberType == NumberType.DOUBLE) {
return NumberTypeFP.DOUBLE64;
}
if (_numberType == NumberType.FLOAT) {
return NumberTypeFP.FLOAT32;
}
}
return NumberTypeFP.UNKNOWN;
}

@Override
public final int getIntValue() throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileParser;

public class NumberParsingTest
public class SmileNumberParsingTest
extends BaseTestForSmile
{
public void testIntsMedium() throws IOException
Expand All @@ -35,6 +35,7 @@ public void testIntsMedium() throws IOException
p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.INT, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(-999, p.getIntValue());
assertEquals("-999", p.getText());
p.close();
Expand All @@ -43,6 +44,7 @@ public void testIntsMedium() throws IOException
p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.INT, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(123456789, p.getIntValue());
p.close();
}
Expand All @@ -53,12 +55,14 @@ public void testMinMaxInts() throws IOException
SmileParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.INT, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(Integer.MAX_VALUE, p.getIntValue());
p.close();
data = _smileDoc(String.valueOf(Integer.MIN_VALUE));
p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.INT, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(Integer.MIN_VALUE, p.getIntValue());
p.close();
}
Expand Down Expand Up @@ -88,6 +92,7 @@ public void testBorderLongs() throws IOException
SmileParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.LONG, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(l, p.getLongValue());
p.close();

Expand All @@ -103,6 +108,7 @@ public void testBorderLongs() throws IOException
p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.LONG, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(l, p.getLongValue());

assertEquals((double) l, p.getDoubleValue());
Expand All @@ -127,6 +133,7 @@ public void testLongs() throws IOException
SmileParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.LONG, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(l, p.getLongValue());
assertEquals(String.valueOf(l), p.getText());
p.close();
Expand All @@ -142,6 +149,7 @@ public void testLongs() throws IOException
p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.LONG, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(l, p.getLongValue());
assertEquals(String.valueOf(l), p.getText());
p.close();
Expand Down Expand Up @@ -210,6 +218,7 @@ public void testFloats() throws IOException
SmileParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(JsonParser.NumberType.FLOAT, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.FLOAT32, p.getNumberTypeFP());
assertEquals(value, p.getFloatValue());
assertFalse(p.isNaN());
assertEquals(Float.valueOf(value), p.getNumberValue());
Expand All @@ -235,6 +244,7 @@ public void testDoubles() throws IOException
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertFalse(p.isNaN());
assertEquals(JsonParser.NumberType.DOUBLE, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.DOUBLE64, p.getNumberTypeFP());
assertEquals(value, p.getDoubleValue());
assertEquals(Double.valueOf(value), p.getNumberValue());

Expand Down Expand Up @@ -329,6 +339,7 @@ public void testBigInteger() throws IOException
SmileParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.BIG_INTEGER, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(BigInteger.class, p.getNumberValue().getClass());
assertEquals(in, p.getBigIntegerValue());
p.close();
Expand Down Expand Up @@ -360,6 +371,7 @@ public void testBigDecimal() throws IOException
SmileParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(JsonParser.NumberType.BIG_DECIMAL, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.BIG_DECIMAL, p.getNumberTypeFP());
assertFalse(p.isNaN());
assertEquals(BigDecimal.class, p.getNumberValue().getClass());
assertEquals(in, p.getDecimalValue());
Expand Down Expand Up @@ -430,6 +442,7 @@ public void testVeryBigDecimalWithUnlimitedNumLength() throws IOException
try (SmileParser p = _smileParser(f, data)) {
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(JsonParser.NumberType.BIG_DECIMAL, p.getNumberType());
assertEquals(JsonParser.NumberTypeFP.BIG_DECIMAL, p.getNumberTypeFP());
assertEquals(value, p.getDecimalValue());
assertFalse(p.isNaN());
assertEquals(value, p.getNumberValue());
Expand Down

0 comments on commit 2268a91

Please sign in to comment.