Search in sources :

Example 1 with SmileParser

use of com.fasterxml.jackson.dataformat.smile.SmileParser 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();
    }
}
Also used : SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) SmileParser(com.fasterxml.jackson.dataformat.smile.SmileParser) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 2 with SmileParser

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

the class TestGeneratorWithRawUtf8 method doTestIssue492.

/*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */
private void doTestIssue492(boolean asUtf8String) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmileGenerator generator = (SmileGenerator) MAPPER.createGenerator(out);
    generator.writeStartObject();
    generator.writeName("name");
    if (asUtf8String) {
        byte[] text = "PojoFoo".getBytes("ASCII");
        generator.writeUTF8String(text, 0, text.length);
    } else {
        generator.writeString("PojoFoo");
    }
    generator.writeName("collection");
    generator.writeStartObject();
    generator.writeName("v");
    generator.writeStartArray();
    if (asUtf8String) {
        byte[] text = "1".getBytes("ASCII");
        generator.writeUTF8String(text, 0, text.length);
    } else {
        generator.writeString("1");
    }
    generator.writeEndArray();
    generator.writeEndObject();
    generator.writeEndObject();
    generator.close();
    byte[] data = out.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    SmileParser parser = (SmileParser) MAPPER.createParser(in);
    assertToken(parser.nextToken(), JsonToken.START_OBJECT);
    assertToken(parser.nextToken(), JsonToken.PROPERTY_NAME);
    assertEquals(parser.currentName(), "name");
    assertToken(parser.nextToken(), JsonToken.VALUE_STRING);
    assertEquals(parser.getText(), "PojoFoo");
    assertToken(parser.nextToken(), JsonToken.PROPERTY_NAME);
    assertEquals(parser.currentName(), "collection");
    assertToken(parser.nextToken(), JsonToken.START_OBJECT);
    assertToken(parser.nextToken(), JsonToken.PROPERTY_NAME);
    assertEquals("Should have property with name 'v'", parser.currentName(), "v");
    assertToken(parser.nextToken(), JsonToken.START_ARRAY);
    assertToken(parser.nextToken(), JsonToken.VALUE_STRING);
    assertEquals("Should get String value '1'", parser.getText(), "1");
    assertToken(parser.nextToken(), JsonToken.END_ARRAY);
    assertToken(parser.nextToken(), JsonToken.END_OBJECT);
    assertToken(parser.nextToken(), JsonToken.END_OBJECT);
    parser.close();
}
Also used : SmileParser(com.fasterxml.jackson.dataformat.smile.SmileParser) SmileGenerator(com.fasterxml.jackson.dataformat.smile.SmileGenerator)

Example 3 with SmileParser

use of com.fasterxml.jackson.dataformat.smile.SmileParser in project hetu-core by openlookeng.

the class SmileMapper method readFrom.

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    Object object;
    try {
        SmileParser smileParser = new SmileFactory().createParser(inputStream);
        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        smileParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
        object = objectMapper.readValue(smileParser, objectMapper.getTypeFactory().constructType(genericType));
    } catch (Exception e) {
        // we want to return a 400 for bad JSON but not for a real IO exception
        if (e instanceof IOException && !(e instanceof JsonProcessingException) && !(e instanceof EOFException)) {
            throw (IOException) e;
        }
        // log the exception at debug so it can be viewed during development
        // Note: we are not logging at a higher level because this could cause a denial of service
        log.debug(e, "Invalid json for Java type %s", type);
        // Invalid json request. Throwing exception so the response code can be overridden using a mapper.
        throw new JsonMapperParsingException(type, e);
    }
    return object;
}
Also used : SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) SmileParser(com.fasterxml.jackson.dataformat.smile.SmileParser) EOFException(java.io.EOFException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Aggregations

SmileParser (com.fasterxml.jackson.dataformat.smile.SmileParser)3 SmileFactory (com.fasterxml.jackson.dataformat.smile.SmileFactory)2 SmileGenerator (com.fasterxml.jackson.dataformat.smile.SmileGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1