Search in sources :

Example 1 with StreamingSerializer

use of io.servicetalk.serializer.api.StreamingSerializer in project servicetalk by apple.

the class JacksonSerializerMessageBodyReaderWriter method writeTo.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void writeTo(final Object o, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws WebApplicationException {
    final boolean oldJacksonProviderPresent = providers.getContextResolver(JacksonSerializationProvider.class, mediaType) != null;
    if (oldJacksonProviderPresent) {
        // FIXME 0.43 - Remove deprecation
        writeToOld(o, type, genericType, annotations, mediaType, httpHeaders, entityStream);
        return;
    }
    final BufferAllocator allocator = ctxRefProvider.get().get().executionContext().bufferAllocator();
    final Publisher<Buffer> bufferPublisher;
    if (o instanceof Single) {
        final Class<?> clazz = genericType instanceof Class ? (Class) genericType : getSourceClass(genericType);
        Serializer serializer = getJacksonSerializerFactory(mediaType).serializerDeserializer(clazz);
        bufferPublisher = ((Single) o).map(t -> serializer.serialize(t, allocator)).toPublisher();
    } else if (o instanceof Publisher) {
        final Class<?> clazz = genericType instanceof Class ? (Class) genericType : getSourceClass(genericType);
        StreamingSerializer serializer = getJacksonSerializerFactory(mediaType).streamingSerializerDeserializer(clazz);
        bufferPublisher = serializer.serialize((Publisher) o, allocator);
    } else {
        Serializer serializer = getJacksonSerializerFactory(mediaType).serializerDeserializer(o.getClass());
        bufferPublisher = Publisher.from(serializer.serialize(o, allocator));
    }
    setResponseBufferPublisher(bufferPublisher, requestCtxProvider.get());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) Single(io.servicetalk.concurrent.api.Single) JacksonSerializationProvider(io.servicetalk.data.jackson.JacksonSerializationProvider) Publisher(io.servicetalk.concurrent.api.Publisher) RequestProperties.setResponseBufferPublisher(io.servicetalk.http.router.jersey.internal.RequestProperties.setResponseBufferPublisher) StreamingSerializer(io.servicetalk.serializer.api.StreamingSerializer) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) Serializer(io.servicetalk.serializer.api.Serializer) StreamingSerializer(io.servicetalk.serializer.api.StreamingSerializer) DefaultSerializer(io.servicetalk.serialization.api.DefaultSerializer)

Aggregations

Buffer (io.servicetalk.buffer.api.Buffer)1 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 Single (io.servicetalk.concurrent.api.Single)1 JacksonSerializationProvider (io.servicetalk.data.jackson.JacksonSerializationProvider)1 RequestProperties.setResponseBufferPublisher (io.servicetalk.http.router.jersey.internal.RequestProperties.setResponseBufferPublisher)1 DefaultSerializer (io.servicetalk.serialization.api.DefaultSerializer)1 Serializer (io.servicetalk.serializer.api.Serializer)1 StreamingSerializer (io.servicetalk.serializer.api.StreamingSerializer)1