Skip to content

Commit

Permalink
Part 3 of #443: IonParser (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Jan 6, 2024
1 parent c9aae32 commit 084628e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,26 @@ public NumberType getNumberType() throws IOException
return null;
}

@Override // since 2.17
public NumberTypeFP getNumberTypeFP() throws IOException
{
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
final IonType type = _reader.getType();
if (type == IonType.FLOAT) {
// 06-Jan-2024, tatu: Existing code maps Ion `FLOAT` into Java
// `float`. But code in `IonReader` suggests `Double` might
// be more accurate mapping... odd.
return NumberTypeFP.FLOAT32;
}
if (type == IonType.DECIMAL) {
// 06-Jan-2024, tatu: Seems like `DECIMAL` is expected to map
// to `BigDecimal`, as per existing code so:
return NumberTypeFP.BIG_DECIMAL;
}
}
return NumberTypeFP.UNKNOWN;
}

@Override
public Number getNumberValue() throws IOException {
NumberType nt = getNumberType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,47 @@ public void testGetNumberTypeAndValue() throws Exception {
IonParser intParser = new IonFactory().createParser(ionInt);
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, intParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.INT, intParser.getNumberType());
Assert.assertEquals(JsonParser.NumberTypeFP.UNKNOWN, intParser.getNumberTypeFP());
Assert.assertEquals(intValue, intParser.getNumberValue());

Long longValue = Long.MAX_VALUE;
IonValue ionLong = ion.newInt(longValue);
IonParser longParser = new IonFactory().createParser(ionLong);
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, longParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.LONG, longParser.getNumberType());
Assert.assertEquals(JsonParser.NumberTypeFP.UNKNOWN, intParser.getNumberTypeFP());
Assert.assertEquals(longValue, longParser.getNumberValue());

BigInteger bigIntValue = new BigInteger(Long.MAX_VALUE + "1");
IonValue ionBigInt = ion.newInt(bigIntValue);
IonParser bigIntParser = new IonFactory().createParser(ionBigInt);
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, bigIntParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.BIG_INTEGER, bigIntParser.getNumberType());
Assert.assertEquals(JsonParser.NumberTypeFP.UNKNOWN, intParser.getNumberTypeFP());
Assert.assertEquals(bigIntValue, bigIntParser.getNumberValue());

Double decimalValue = Double.MAX_VALUE;
IonValue ionDecimal = ion.newDecimal(decimalValue);
IonParser decimalParser = new IonFactory().createParser(ionDecimal);
Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, decimalParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.BIG_DECIMAL, decimalParser.getNumberType());
Assert.assertEquals(JsonParser.NumberTypeFP.BIG_DECIMAL, decimalParser.getNumberTypeFP());
Assert.assertEquals(0, new BigDecimal("" + decimalValue).compareTo((BigDecimal) decimalParser.getNumberValue()));

Double floatValue = Double.MAX_VALUE;
IonValue ionFloat = ion.newFloat(floatValue);
IonParser floatParser = new IonFactory().createParser(ionFloat);
Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, floatParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.DOUBLE, floatParser.getNumberType());
Assert.assertEquals(JsonParser.NumberTypeFP.FLOAT32, floatParser.getNumberTypeFP());
Assert.assertEquals(floatValue, floatParser.getNumberValue());

BigDecimal bigDecimalValue = new BigDecimal(Double.MAX_VALUE + "1");
IonValue ionBigDecimal = ion.newDecimal(bigDecimalValue);
IonParser bigDecimalParser = new IonFactory().createParser(ionBigDecimal);
Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, bigDecimalParser.nextToken());
Assert.assertEquals(JsonParser.NumberType.BIG_DECIMAL, bigDecimalParser.getNumberType());
Assert.assertEquals(JsonParser.NumberTypeFP.BIG_DECIMAL, bigDecimalParser.getNumberTypeFP());
Assert.assertEquals(0, bigDecimalValue.compareTo((BigDecimal) bigDecimalParser.getNumberValue()));
}

Expand Down

0 comments on commit 084628e

Please sign in to comment.