use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberParsingTest method testMixedAccessForInts.
public void testMixedAccessForInts() throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
SmileGenerator g = _smileGenerator(bo, false);
g.writeNumber(123);
g.writeNumber(123);
g.writeNumber(123);
g.writeNumber(123);
final long LONG_VALUE = 100L + Integer.MAX_VALUE;
g.writeNumber(LONG_VALUE);
g.writeNumber(LONG_VALUE);
g.writeNumber(LONG_VALUE);
g.writeNumber(LONG_VALUE);
g.writeNumber(LONG_VALUE);
g.writeNumber(BigInteger.TEN);
g.writeNumber(BigInteger.TEN);
g.writeNumber(BigInteger.TEN);
g.writeNumber(BigInteger.TEN);
g.writeNumber(BigInteger.TEN);
g.close();
byte[] data = bo.toByteArray();
JsonParser p = _smileParser(data);
// for ints
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(123L, p.getLongValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(123d, p.getDoubleValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(123f, p.getFloatValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(Integer.valueOf(123), p.getNumberValue());
// for longs
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals((double) LONG_VALUE, p.getDoubleValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals((float) LONG_VALUE, p.getFloatValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigInteger.valueOf(LONG_VALUE), p.getBigIntegerValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(Long.valueOf(LONG_VALUE), p.getNumberValue());
// and can't represent as int so:
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
try {
p.getIntValue();
fail("Should not pass");
} catch (StreamReadException e) {
verifyException(e, "Numeric value");
verifyException(e, "out of range of int");
}
// for BigIntegers
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(10, p.getIntValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(10L, p.getLongValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(10f, p.getFloatValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(10d, p.getDoubleValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigInteger.valueOf(10L), p.getNumberValue());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberParsingTest method testBigDecimal.
public void testBigDecimal() throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
BigDecimal in = new BigDecimal("32599.00001");
SmileGenerator g = _smileGenerator(bo, false);
g.writeNumber(in);
g.close();
byte[] data = bo.toByteArray();
JsonParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(JsonParser.NumberType.BIG_DECIMAL, p.getNumberType());
assertFalse(p.isNaN());
assertEquals(BigDecimal.class, p.getNumberValue().getClass());
assertEquals(in, p.getDecimalValue());
p.close();
// second test; verify skipping works
bo = new ByteArrayOutputStream();
g = _smileGenerator(bo, false);
g.writeStartArray();
g.writeNumber(in);
g.writeEndArray();
g.close();
data = bo.toByteArray();
p = _smileParser(data);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertNull(p.nextToken());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberParsingTest method testMixedAccessForFloats.
public void testMixedAccessForFloats() throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
SmileGenerator g = _smileGenerator(bo, false);
g.writeNumber(2.25d);
g.writeNumber(3.25d);
g.writeNumber(4.25d);
g.writeNumber(4.75f);
g.writeNumber(5.75f);
g.writeNumber(6.75f);
g.close();
byte[] data = bo.toByteArray();
JsonParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(2, p.getIntValue());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(3L, p.getLongValue());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals((float) 4.25d, p.getFloatValue());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(4, p.getIntValue());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(5L, p.getLongValue());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(6.75, p.getDoubleValue());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberParsingTest method testBigInteger.
public void testBigInteger() throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
BigInteger in = new BigInteger(String.valueOf(Long.MIN_VALUE) + "0012575934");
SmileGenerator g = _smileGenerator(bo, false);
g.writeNumber(in);
g.close();
byte[] data = bo.toByteArray();
JsonParser p = _smileParser(data);
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(JsonParser.NumberType.BIG_INTEGER, p.getNumberType());
assertEquals(BigInteger.class, p.getNumberValue().getClass());
assertEquals(in, p.getBigIntegerValue());
p.close();
// second test; verify skipping works
bo = new ByteArrayOutputStream();
g = _smileGenerator(bo, false);
g.writeStartArray();
g.writeNumber(in);
g.writeEndArray();
g.close();
data = bo.toByteArray();
p = _smileParser(data);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertNull(p.nextToken());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberBeanTest method testNumberTypeRetainingFP.
public void testNumberTypeRetainingFP() throws Exception {
NumberWrapper result;
ByteArrayOutputStream bytes;
bytes = new ByteArrayOutputStream();
try (SmileGenerator g = _smileGenerator(bytes, true)) {
g.writeStartObject();
g.writeNumberProperty("nr", 0.25f);
g.writeEndObject();
}
result = MAPPER.readValue(bytes.toByteArray(), NumberWrapper.class);
assertEquals(Float.valueOf(0.25f), result.nr);
bytes = new ByteArrayOutputStream();
try (SmileGenerator g = _smileGenerator(bytes, true)) {
g.writeStartObject();
g.writeNumberProperty("nr", 0.5);
g.writeEndObject();
}
result = MAPPER.readValue(bytes.toByteArray(), NumberWrapper.class);
assertEquals(Double.valueOf(0.5), result.nr);
bytes = new ByteArrayOutputStream();
try (SmileGenerator g = _smileGenerator(bytes, true)) {
g.writeStartObject();
g.writeNumberProperty("nr", new BigDecimal("0.100"));
g.writeEndObject();
}
result = MAPPER.readValue(bytes.toByteArray(), NumberWrapper.class);
assertEquals(new BigDecimal("0.100"), result.nr);
}
Aggregations