Search in sources :

Example 16 with SmileGenerator

use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.

the class NumberBeanTest method testNumberTypeRetainingInt.

public void testNumberTypeRetainingInt() throws Exception {
    NumberWrapper result;
    ByteArrayOutputStream bytes;
    bytes = new ByteArrayOutputStream();
    try (SmileGenerator g = _smileGenerator(bytes, true)) {
        g.writeStartObject();
        g.writeNumberProperty("nr", 123);
        g.writeEndObject();
    }
    result = MAPPER.readValue(bytes.toByteArray(), NumberWrapper.class);
    assertEquals(Integer.valueOf(123), result.nr);
    bytes = new ByteArrayOutputStream();
    try (SmileGenerator g = _smileGenerator(bytes, true)) {
        g.writeStartObject();
        g.writeNumberProperty("nr", Long.MAX_VALUE);
        g.writeEndObject();
    }
    result = MAPPER.readValue(bytes.toByteArray(), NumberWrapper.class);
    assertEquals(Long.valueOf(Long.MAX_VALUE), result.nr);
    bytes = new ByteArrayOutputStream();
    try (SmileGenerator g = _smileGenerator(bytes, true)) {
        g.writeStartObject();
        g.writeNumberProperty("nr", BigInteger.valueOf(-42L));
        g.writeEndObject();
    }
    result = MAPPER.readValue(bytes.toByteArray(), NumberWrapper.class);
    assertEquals(BigInteger.valueOf(-42L), result.nr);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 17 with SmileGenerator

use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.

the class DocBoundaryTest method testExtraHeader.

public void testExtraHeader() throws Exception {
    // also; sprinkling headers can be used to segment document
    for (boolean addHeader : new boolean[] { false, true }) {
        SmileFactory f = smileFactory(false, false, false);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SmileGenerator jg = (SmileGenerator) f.createGenerator(ObjectWriteContext.empty(), out);
        jg.writeNumber(1);
        if (addHeader)
            jg.writeHeader();
        jg.writeNumber(2);
        if (addHeader)
            jg.writeHeader();
        jg.writeNumber(3);
        jg.close();
        JsonParser jp = _smileParser(out.toByteArray());
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
        assertEquals(1, jp.getIntValue());
        if (addHeader) {
            assertNull(jp.nextToken());
        }
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
        assertEquals(2, jp.getIntValue());
        if (addHeader) {
            assertNull(jp.nextToken());
        }
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
        assertEquals(3, jp.getIntValue());
        assertNull(jp.nextToken());
        jp.close();
        jg.close();
    }
}
Also used : SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 18 with SmileGenerator

use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.

the class NumberParsingTest method testArrayWithDoubles.

public void testArrayWithDoubles() throws IOException {
    final double[] values = new double[] { 0.1, 0.333, Double.POSITIVE_INFINITY, Double.NaN, -2.5, Double.NEGATIVE_INFINITY };
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    SmileGenerator g = _smileGenerator(bo, false);
    g.writeStartArray();
    for (double d : values) {
        g.writeNumber(d);
    }
    g.close();
    byte[] data = bo.toByteArray();
    // 10 bytes per double, array start, end
    // assertEquals(2 + values.length * 10, data.length);
    JsonParser p = _smileParser(data);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    for (double exp : values) {
        assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
        boolean expNaN = Double.isNaN(exp) || Double.isInfinite(exp);
        assertEquals(exp, p.getDoubleValue());
        assertEquals(expNaN, p.isNaN());
    }
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertNull(p.nextToken());
    p.close();
}
Also used : SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 19 with SmileGenerator

use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.

the class NumberParsingTest method testDoubles.

public void testDoubles() throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    SmileGenerator g = _smileGenerator(bo, false);
    double value = -12.0986;
    g.writeNumber(value);
    g.close();
    byte[] data = bo.toByteArray();
    assertEquals(11, data.length);
    JsonParser p = _smileParser(data);
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertFalse(p.isNaN());
    assertEquals(JsonParser.NumberType.DOUBLE, p.getNumberType());
    assertEquals(value, p.getDoubleValue());
    assertEquals(Double.valueOf(value), p.getNumberValue());
    assertEquals((float) value, p.getFloatValue());
    assertEquals((int) value, p.getIntValue());
    assertEquals((long) value, p.getLongValue());
    p.close();
}
Also used : SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 20 with SmileGenerator

use of com.fasterxml.jackson.dataformat.smile.SmileGenerator in project jackson-dataformats-binary by FasterXML.

the class TestGeneratorNumbers method testFloats.

public void testFloats() throws Exception {
    // float length is fixed, 6 bytes
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmileGenerator gen = _smileGenerator(out, false);
    gen.writeNumber(0.125f);
    gen.close();
    assertEquals(6, out.toByteArray().length);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Aggregations

SmileGenerator (com.fasterxml.jackson.dataformat.smile.SmileGenerator)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 BigDecimal (java.math.BigDecimal)4 JsonParser (com.fasterxml.jackson.core.JsonParser)2 SmileFactory (com.fasterxml.jackson.dataformat.smile.SmileFactory)2 SmileParser (com.fasterxml.jackson.dataformat.smile.SmileParser)2 StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)1 BigInteger (java.math.BigInteger)1