Search in sources :

Example 1 with SmileGenerator

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

the class TestGeneratorNumbers method testSmallInts.

public void testSmallInts() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmileGenerator gen = _smileGenerator(out, false);
    gen.writeNumber(3);
    gen.close();
    _verifyBytes(out.toByteArray(), (byte) (0xC0 + SmileUtil.zigzagEncode(3)));
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(0);
    gen.close();
    _verifyBytes(out.toByteArray(), (byte) (0xC0 + SmileUtil.zigzagEncode(0)));
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(-6);
    gen.close();
    _verifyBytes(out.toByteArray(), (byte) (0xC0 + SmileUtil.zigzagEncode(-6)));
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(15);
    gen.close();
    _verifyBytes(out.toByteArray(), (byte) (0xC0 + SmileUtil.zigzagEncode(15)));
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(-16);
    gen.close();
    _verifyBytes(out.toByteArray(), (byte) (0xC0 + SmileUtil.zigzagEncode(-16)));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 2 with SmileGenerator

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

the class TestGeneratorNumbers method testOtherInts.

public void testOtherInts() throws Exception {
    // beyond tiny ints, 6-bit values take 2 bytes
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmileGenerator gen = _smileGenerator(out, false);
    gen.writeNumber(16);
    gen.close();
    assertEquals(2, out.toByteArray().length);
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(-17);
    gen.close();
    assertEquals(2, out.toByteArray().length);
    // and up to 13-bit values take 3 bytes
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(0xFFF);
    gen.close();
    assertEquals(3, out.toByteArray().length);
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(-4096);
    gen.close();
    assertEquals(3, out.toByteArray().length);
    // up to 20, 4 bytes... and so forth
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(0x1000);
    gen.close();
    assertEquals(4, out.toByteArray().length);
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(500000);
    gen.close();
    assertEquals(4, out.toByteArray().length);
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(Integer.MAX_VALUE);
    gen.close();
    assertEquals(6, out.toByteArray().length);
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(Integer.MIN_VALUE);
    gen.close();
    assertEquals(6, out.toByteArray().length);
    // up to longest ones, taking 11 bytes
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(Long.MAX_VALUE);
    gen.close();
    assertEquals(11, out.toByteArray().length);
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(Long.MIN_VALUE);
    gen.close();
    assertEquals(11, out.toByteArray().length);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 3 with SmileGenerator

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

the class TestGeneratorNumbers method testNumbersAsString.

// #16: Problems with 'Stringified' numbers
public void testNumbersAsString() throws Exception {
    ByteArrayOutputStream out;
    SmileGenerator gen;
    // first int
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber("15");
    gen.close();
    _verifyBytes(out.toByteArray(), (byte) (0xC0 + SmileUtil.zigzagEncode(15)));
    // then long. Note: cut-off to BigInteger not exact, so...
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber(String.valueOf(-1L + Integer.MIN_VALUE));
    gen.close();
    assertEquals(6, out.toByteArray().length);
    // and then just BigDecimal...
    out = new ByteArrayOutputStream();
    gen = _smileGenerator(out, false);
    gen.writeNumber("-50.00000000125");
    gen.close();
    assertEquals(10, out.toByteArray().length);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 4 with SmileGenerator

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

the class TestGeneratorSymbols method _testSharedNameSimpleLong.

private void _testSharedNameSimpleLong(String suffix) throws Exception {
    String digits = "01234567899";
    // Base is 76 chars; loop over couple of shorter ones too
    final String LONG_NAME = "a" + digits + "b" + digits + "c" + digits + "d" + digits + "e" + digits + "f" + digits + suffix;
    for (int i = 0; i < 4; ++i) {
        int strLen = LONG_NAME.length() - i;
        String field = LONG_NAME.substring(0, strLen);
        // false, no header (or frame marker)
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SmileGenerator gen = _smileGenerator(out, false);
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeNumberProperty(field, 1);
        gen.writeEndObject();
        gen.writeStartObject();
        gen.writeNumberProperty(field, 2);
        gen.writeEndObject();
        gen.writeEndArray();
        gen.close();
        byte[] result = out.toByteArray();
        assertEquals(11 + field.length(), result.length);
        // better also parse it back...
        JsonParser parser = _smileParser(result);
        assertToken(JsonToken.START_ARRAY, parser.nextToken());
        assertToken(JsonToken.START_OBJECT, parser.nextToken());
        assertToken(JsonToken.PROPERTY_NAME, parser.nextToken());
        assertEquals(field, parser.currentName());
        assertToken(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
        assertEquals(1, parser.getIntValue());
        assertToken(JsonToken.END_OBJECT, parser.nextToken());
        assertToken(JsonToken.START_OBJECT, parser.nextToken());
        assertToken(JsonToken.PROPERTY_NAME, parser.nextToken());
        assertEquals(field, parser.currentName());
        assertToken(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
        assertEquals(2, parser.getIntValue());
        assertToken(JsonToken.END_OBJECT, parser.nextToken());
        assertToken(JsonToken.END_ARRAY, parser.nextToken());
        parser.close();
    }
}
Also used : SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 5 with SmileGenerator

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

the class NumberParsingTest method testFloats.

public void testFloats() throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    SmileGenerator g = _smileGenerator(bo, false);
    float value = 0.37f;
    g.writeNumber(value);
    g.close();
    byte[] data = bo.toByteArray();
    assertEquals(6, data.length);
    JsonParser p = _smileParser(data);
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals(JsonParser.NumberType.FLOAT, p.getNumberType());
    assertEquals(value, p.getFloatValue());
    assertFalse(p.isNaN());
    assertEquals(Float.valueOf(value), p.getNumberValue());
    assertEquals((double) value, p.getDoubleValue());
    assertEquals((int) value, p.getIntValue());
    assertEquals((long) value, p.getLongValue());
    p.close();
}
Also used : 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