Search in sources :

Example 66 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncBinaryParseTest method _testBinaryAsArray2.

private void _testBinaryAsArray2(JsonFactory f, int offset, int readSize) throws IOException {
    for (int size : SIZES) {
        byte[] binary = _generateData(size);
        ByteArrayOutputStream bo = new ByteArrayOutputStream(size + 10);
        JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), bo);
        g.writeStartArray();
        g.writeBinary(binary);
        // just to verify there's no overrun
        g.writeNumber(1);
        g.writeEndArray();
        g.close();
        byte[] smile = bo.toByteArray();
        // and verify
        AsyncReaderWrapper p = asyncForBytes(f, readSize, smile, offset);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        byte[] result = p.getBinaryValue();
        assertArrayEquals(binary, result);
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(1, p.getIntValue());
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        assertNull(p.nextToken());
        p.close();
        // and second time around, skipping
        p = asyncForBytes(f, readSize, smile, offset);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        assertNull(p.nextToken());
        p.close();
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 67 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncBinaryParseTest method _testBinaryAsObject2.

private void _testBinaryAsObject2(JsonFactory f, int offset, int readSize) throws IOException {
    for (int size : SIZES) {
        byte[] data = _generateData(size);
        ByteArrayOutputStream bo = new ByteArrayOutputStream(size + 10);
        JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), bo);
        g.writeStartObject();
        g.writeFieldName("binary");
        g.writeBinary(data);
        g.writeEndObject();
        g.close();
        byte[] smile = bo.toByteArray();
        AsyncReaderWrapper p = asyncForBytes(f, readSize, smile, offset);
        assertToken(JsonToken.START_OBJECT, p.nextToken());
        assertToken(JsonToken.FIELD_NAME, p.nextToken());
        assertEquals("binary", p.currentName());
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        byte[] result = p.getBinaryValue();
        assertArrayEquals(data, result);
        // also, via different accessor
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(result.length);
        assertEquals(result.length, p.parser().readBinaryValue(bytes));
        assertArrayEquals(data, bytes.toByteArray());
        assertToken(JsonToken.END_OBJECT, p.nextToken());
        assertNull(p.nextToken());
        p.close();
        // and second time around, skipping
        p = asyncForBytes(f, readSize, smile, offset);
        assertToken(JsonToken.START_OBJECT, p.nextToken());
        assertToken(JsonToken.FIELD_NAME, p.nextToken());
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        assertToken(JsonToken.END_OBJECT, p.nextToken());
        assertNull(p.nextToken());
        p.close();
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 68 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncBinaryParseTest method _testBinaryAsRoot2.

/*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */
private void _testBinaryAsRoot2(JsonFactory f, int offset, int readSize) throws IOException {
    for (int size : SIZES) {
        byte[] binary = _generateData(size);
        ByteArrayOutputStream bo = new ByteArrayOutputStream(size + 10);
        JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), bo);
        g.writeBinary(binary);
        g.close();
        byte[] smile = bo.toByteArray();
        // and verify
        AsyncReaderWrapper p = asyncForBytes(f, readSize, smile, offset);
        // JSON has no native binary type so
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        byte[] result = p.getBinaryValue();
        assertArrayEquals(binary, result);
        assertNull(p.nextToken());
        p.close();
        // and second time around, skipping
        p = asyncForBytes(f, readSize, smile, offset);
        assertToken(JsonToken.VALUE_STRING, p.nextToken());
        assertNull(p.nextToken());
        p.close();
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 69 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncCommentParsingTest method _testCommentsBetweenArrayValues.

private void _testCommentsBetweenArrayValues(JsonFactory f, String comment, int bytesPerRead) throws Exception {
    for (String tmpl : new String[] { "%s,", " %s,", "\t%s,", "%s ,", "%s\t,", " %s ,", "\t%s\t,", "\n%s,", "%s\n," }) {
        String commented = String.format(tmpl, comment);
        final String DOC = "[1" + commented + "2]";
        AsyncReaderWrapper p = _createParser(f, DOC, bytesPerRead);
        assertEquals(JsonToken.START_ARRAY, p.nextToken());
        JsonToken t = null;
        try {
            t = p.nextToken();
        } catch (Exception e) {
            throw new RuntimeException("Failed on '" + DOC + "' due to " + e, e);
        }
        assertEquals(JsonToken.VALUE_NUMBER_INT, t);
        assertEquals(1, p.getIntValue());
        try {
            t = p.nextToken();
        } catch (Exception e) {
            throw new RuntimeException("Failed on '" + DOC + "' due to " + e, e);
        }
        assertEquals(JsonToken.VALUE_NUMBER_INT, t);
        assertEquals(2, p.getIntValue());
        assertEquals(JsonToken.END_ARRAY, p.nextToken());
        p.close();
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 70 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncCommentParsingTest method _testEnabled.

private void _testEnabled(String doc, int bytesPerRead) throws IOException {
    AsyncReaderWrapper p = _createParser(doc, true, bytesPerRead);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(1, p.getIntValue());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Aggregations

AsyncReaderWrapper (com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)87 Test (org.junit.Test)10 JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)5 BigDecimal (java.math.BigDecimal)4 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 NonBlockingJsonParserBase (com.fasterxml.jackson.core.json.async.NonBlockingJsonParserBase)2 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2