use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberParsingTest method testObjectWithDoubles.
public void testObjectWithDoubles() throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
SmileGenerator g = _smileGenerator(bo, false);
g.writeStartObject();
g.writeNumberProperty("x", 0.5);
g.writeNumberProperty("y", 0.01338);
g.writeEndObject();
g.close();
byte[] data = bo.toByteArray();
// first let's just skip
JsonParser p = _smileParser(data);
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.END_OBJECT, p.nextToken());
p.close();
// and then check data too (skip codepath distinct)
p = _smileParser(data);
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
assertEquals("x", p.getText());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(0.5, p.getDoubleValue());
assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
assertEquals("y", p.getText());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(0.01338, p.getDoubleValue());
assertToken(JsonToken.END_OBJECT, p.nextToken());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class ParserBinaryHandlingTest method _testBinaryAsRoot.
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
private void _testBinaryAsRoot(boolean raw) throws IOException {
SmileFactory f = SmileFactory.builder().configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, !raw).build();
for (int size : SIZES) {
byte[] data = _generateData(size);
ByteArrayOutputStream bo = new ByteArrayOutputStream(size + 10);
SmileGenerator g = (SmileGenerator) f.createGenerator(ObjectWriteContext.empty(), bo);
g.writeBinary(data);
g.close();
byte[] smile = bo.toByteArray();
// and verify
SmileParser p = (SmileParser) _smileParser(smile);
assertEquals(raw, p.mayContainRawBinary());
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
byte[] result = p.getBinaryValue();
assertArrayEquals(data, result);
assertNull(p.nextToken());
p.close();
// and second time around, skipping
p = (SmileParser) _smileParser(smile);
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
assertNull(p.nextToken());
p.close();
}
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class ParserLocationTest method testAscendingOffsets.
// for [databind-smile#24]
public void testAscendingOffsets() throws Exception {
// need to create big enough document, say at least 64k
// but as importantly, try to create buffer boundaries by using 6-char (7-byte) ASCII strings
final int COUNT = 57000;
final int SIZE = COUNT * 7;
ByteArrayOutputStream bytes = new ByteArrayOutputStream(COUNT + 10);
SmileGenerator gen = _smileGenerator(bytes, true);
gen.disable(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES);
gen.writeStartArray();
for (int i = 0; i < COUNT; ++i) {
gen.writeString("abc123");
}
gen.writeEndArray();
gen.close();
byte[] b = bytes.toByteArray();
assertEquals(4 + 2 + SIZE, b.length);
JsonParser p = _smileParser(new ByteArrayInputStream(b));
assertToken(JsonToken.START_ARRAY, p.nextToken());
// 4 byte header, start array read, so 4 bytes down:
assertEquals(5, p.currentLocation().getByteOffset());
for (int i = 0; i < COUNT; ++i) {
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(6 + i * 7, p.currentLocation().getByteOffset());
assertEquals("abc123", p.getText());
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertEquals(SIZE + 6, p.currentLocation().getByteOffset());
assertNull(p.nextToken());
assertEquals(SIZE + 6, p.currentLocation().getByteOffset());
p.close();
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberBeanTest method testNumberTypeRetainingBuffering.
public void testNumberTypeRetainingBuffering() throws Exception {
ByteArrayOutputStream bytes;
final BigDecimal EXP_BIG_DEC = new BigDecimal("0.0100");
bytes = new ByteArrayOutputStream();
try (SmileGenerator g = _smileGenerator(bytes, true)) {
g.writeStartArray();
g.writeNumber(101);
g.writeNumber(0.25);
g.writeNumber(Long.MAX_VALUE);
g.writeNumber(0.5f);
g.writeNumber(BigInteger.valueOf(1972));
g.writeNumber(EXP_BIG_DEC);
g.writeEndArray();
}
try (JsonParser p = _smileParser(bytes.toByteArray(), true)) {
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.INT, p.getNumberType());
assertEquals(Integer.valueOf(101), p.getNumberValue());
assertEquals(Integer.valueOf(101), p.getNumberValueExact());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(NumberType.DOUBLE, p.getNumberType());
assertEquals(Double.valueOf(0.25), p.getNumberValue());
assertEquals(Double.valueOf(0.25), p.getNumberValueExact());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.LONG, p.getNumberType());
assertEquals(Long.MAX_VALUE, p.getNumberValue());
assertEquals(Long.MAX_VALUE, p.getNumberValueExact());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(NumberType.FLOAT, p.getNumberType());
assertEquals(Float.valueOf(0.5f), p.getNumberValue());
assertEquals(Float.valueOf(0.5f), p.getNumberValueExact());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.BIG_INTEGER, p.getNumberType());
assertEquals(BigInteger.valueOf(1972), p.getNumberValue());
assertEquals(BigInteger.valueOf(1972), p.getNumberValueExact());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(NumberType.BIG_DECIMAL, p.getNumberType());
assertEquals(EXP_BIG_DEC, p.getNumberValue());
assertEquals(EXP_BIG_DEC, p.getNumberValueExact());
assertToken(JsonToken.END_ARRAY, p.nextToken());
}
}
use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.
the class NumberBeanTest method testBigDecimalWithBuffering.
// [databind#2784]
public void testBigDecimalWithBuffering() throws Exception {
final BigDecimal VALUE = new BigDecimal("5.00");
// Need to generate by hand since JSON would not indicate desire for BigDecimal
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (SmileGenerator g = _smileGenerator(bytes, true)) {
g.writeStartObject();
g.writeNumberProperty("value", VALUE);
g.writeEndObject();
}
NestedBigDecimalHolder2784 result = MAPPER.readValue(bytes.toByteArray(), NestedBigDecimalHolder2784.class);
assertEquals(VALUE, result.holder.value);
}
Aggregations