Search in sources :

Example 16 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class ParserInteropTest method testSelfDescribeTagRead.

// for [Issue#6]: should be fine to have self-desc tag in general
public void testSelfDescribeTagRead() throws Exception {
    CBORParser p = cborParser(SELF_DESC_PLUS_TRUE);
    assertEquals(-1, p.getCurrentTag());
    assertToken(JsonToken.VALUE_TRUE, p.nextToken());
    assertEquals(CBORConstants.TAG_ID_SELF_DESCRIBE, p.getCurrentTag());
    assertNull(p.nextToken());
    assertEquals(-1, p.getCurrentTag());
    p.close();
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser)

Example 17 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class NumberBeanTest method testNumberTypeRetainingBuffering.

public void testNumberTypeRetainingBuffering() throws Exception {
    ByteArrayOutputStream bytes;
    ObjectWriter w = MAPPER.writer().without(CBORGenerator.Feature.WRITE_MINIMAL_INTS);
    final BigDecimal EXP_BIG_DEC = new BigDecimal("0.0100");
    bytes = new ByteArrayOutputStream();
    try (JsonGenerator g = w.createGenerator(bytes)) {
        g.writeStartArray();
        g.writeNumber(101);
        g.writeNumber(0.25);
        g.writeNumber(13117L);
        g.writeNumber(0.5f);
        g.writeNumber(BigInteger.valueOf(1972));
        g.writeNumber(EXP_BIG_DEC);
        g.writeEndArray();
    }
    try (CBORParser p = cborParser(bytes.toByteArray())) {
        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.valueOf(13117L), p.getNumberValue());
        assertEquals(Long.valueOf(13117L), 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());
    }
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BigDecimal(java.math.BigDecimal)

Example 18 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class ArrayGenerationTest method _testMinimalIntValuesForInt.

private void _testMinimalIntValuesForInt(int v1, int v2, int minLen, int fullLen) throws Exception {
    final int[] input = new int[] { v1, v2 };
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    CBORGenerator gen = (CBORGenerator) MAPPER.createGenerator(bytes);
    assertTrue(gen.isEnabled(CBORGenerator.Feature.WRITE_MINIMAL_INTS));
    gen.writeArray(input, 0, 2);
    gen.close();
    // With default settings, should get:
    byte[] encoded = bytes.toByteArray();
    assertEquals(minLen, encoded.length);
    // then verify contents
    CBORParser p = (CBORParser) MAPPER.createParser(encoded);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(NumberType.INT, p.getNumberType());
    assertEquals(input[0], p.getIntValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(NumberType.INT, p.getNumberType());
    assertEquals(input[1], p.getIntValue());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
    // but then also check without minimization
    bytes = new ByteArrayOutputStream();
    gen = (CBORGenerator) MAPPER.writer().without(CBORGenerator.Feature.WRITE_MINIMAL_INTS).createGenerator(bytes);
    assertFalse(gen.isEnabled(CBORGenerator.Feature.WRITE_MINIMAL_INTS));
    gen.writeArray(input, 0, 2);
    gen.close();
    // With default settings, should get:
    encoded = bytes.toByteArray();
    assertEquals(fullLen, encoded.length);
    // then verify contents
    p = (CBORParser) MAPPER.createParser(encoded);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(NumberType.INT, p.getNumberType());
    assertEquals(input[0], p.getIntValue());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(NumberType.INT, p.getNumberType());
    assertEquals(input[1], p.getIntValue());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Example 19 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class BasicParserTest method testNestedObject.

public void testNestedObject() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CBORGenerator generator = cborGenerator(out);
    generator.writeStartObject();
    generator.writeName("ob");
    generator.writeStartObject();
    generator.writeNumberProperty("num", 3);
    generator.writeEndObject();
    generator.writeName("arr");
    generator.writeStartArray();
    generator.writeEndArray();
    generator.writeEndObject();
    generator.close();
    CBORParser parser = cborParser(out.toByteArray());
    assertEquals(JsonToken.START_OBJECT, parser.nextToken());
    assertEquals(JsonToken.PROPERTY_NAME, parser.nextToken());
    assertEquals("ob", parser.currentName());
    assertEquals(JsonToken.START_OBJECT, parser.nextToken());
    assertEquals(JsonToken.PROPERTY_NAME, parser.nextToken());
    assertEquals("num", parser.currentName());
    assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
    assertEquals(JsonToken.END_OBJECT, parser.nextToken());
    assertEquals(JsonToken.PROPERTY_NAME, parser.nextToken());
    assertEquals("arr", parser.currentName());
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    assertEquals(JsonToken.END_ARRAY, parser.nextToken());
    assertEquals(JsonToken.END_OBJECT, parser.nextToken());
    parser.close();
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Example 20 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class BasicParserTest method testStringField.

public void testStringField() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CBORGenerator generator = cborGenerator(out);
    generator.writeStartObject();
    generator.writeStringProperty("a", "b");
    generator.writeEndObject();
    generator.close();
    CBORParser parser = cborParser(out.toByteArray());
    assertEquals(JsonToken.START_OBJECT, parser.nextToken());
    assertEquals(JsonToken.PROPERTY_NAME, parser.nextToken());
    assertEquals("a", parser.currentName());
    assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
    assertEquals("a", parser.currentName());
    assertEquals("b", parser.getText());
    assertEquals(1, parser.getTextLength());
    assertEquals(JsonToken.END_OBJECT, parser.nextToken());
    // For fun, release
    ByteArrayOutputStream extra = new ByteArrayOutputStream();
    assertEquals(0, parser.releaseBuffered(extra));
    parser.close();
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Aggregations

CBORParser (com.fasterxml.jackson.dataformat.cbor.CBORParser)22 StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)9 CBORGenerator (com.fasterxml.jackson.dataformat.cbor.CBORGenerator)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonLocation (com.fasterxml.jackson.core.JsonLocation)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 PublicKey (com.quorum.tessera.encryption.PublicKey)1 Fido2RuntimeException (io.jans.fido2.exception.Fido2RuntimeException)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1