Search in sources :

Example 1 with ByteBufferBackedInputStream

use of com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream in project jackson-databind by FasterXML.

the class ByteBufferSerializer method serialize.

@Override
public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException {
    // first, simple case when wrapping an array...
    if (bbuf.hasArray()) {
        gen.writeBinary(bbuf.array(), 0, bbuf.limit());
        return;
    }
    // the other case is more complicated however. Best to handle with InputStream wrapper.
    // But should we rewind it; and/or make a copy?
    ByteBuffer copy = bbuf.asReadOnlyBuffer();
    if (copy.position() > 0) {
        copy.rewind();
    }
    InputStream in = new ByteBufferBackedInputStream(copy);
    gen.writeBinary(in, copy.remaining());
    in.close();
}
Also used : ByteBufferBackedInputStream(com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream) ByteBuffer(java.nio.ByteBuffer) ByteBufferBackedInputStream(com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)

Example 2 with ByteBufferBackedInputStream

use of com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream in project chassis by Kixeye.

the class RawWebSocketMessage method deserialize.

/**
	 * Deserializes the given message.
	 * 
	 * @param action
	 * @return
	 * @throws Exception
	 */
public T deserialize(WebSocketAction action) throws Exception {
    // first deserialize
    T message = null;
    if (messageClass != null) {
        message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
    }
    // then validate
    if (message != null && action.shouldValidatePayload()) {
        SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
        BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
        validatorAdapter.validate(message, result);
        if (result.hasErrors()) {
            throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
        }
    }
    return message;
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ByteBufferBackedInputStream(com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)

Aggregations

ByteBufferBackedInputStream (com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)2 ByteBuffer (java.nio.ByteBuffer)1 MethodParameter (org.springframework.core.MethodParameter)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 SpringValidatorAdapter (org.springframework.validation.beanvalidation.SpringValidatorAdapter)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1