use of io.servicetalk.serializer.api.Serializer 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());
}
use of io.servicetalk.serializer.api.Serializer in project servicetalk by apple.
the class JacksonSerializerMessageBodyReaderWriter method readFromOld.
// FIXME 0.43 - Remove this branch when deprecation go away.
@Deprecated
private Object readFromOld(final Class<Object> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws WebApplicationException {
final io.servicetalk.serialization.api.Serializer serializer = getSerializer(mediaType);
final ExecutionContext<?> executionContext = ctxRefProvider.get().get().executionContext();
final BufferAllocator allocator = executionContext.bufferAllocator();
final int contentLength = requestCtxProvider.get().getLength();
if (Single.class.isAssignableFrom(type)) {
return handleEntityStream(entityStream, allocator, (p, a) -> deserializeOld(p, serializer, getSourceClass(genericType), contentLength, a), (is, a) -> new SingleSource<>(deserializeOld(toBufferPublisher(is, a), serializer, getSourceClass(genericType), contentLength, a)));
} else if (Publisher.class.isAssignableFrom(type)) {
return handleEntityStream(entityStream, allocator, (p, a) -> serializer.deserialize(p, getSourceClass(genericType)), (is, a) -> new PublisherSource<>(serializer.deserialize(toBufferPublisher(is, a), getSourceClass(genericType))));
}
return handleEntityStream(entityStream, allocator, (p, a) -> deserializeObjectOld(p, serializer, type, contentLength, a), (is, a) -> deserializeObjectOld(toBufferPublisher(is, a), serializer, type, contentLength, a));
}
Aggregations