Search in sources :

Example 6 with MessageBodyWriterContext

use of io.helidon.media.common.MessageBodyWriterContext in project helidon by oracle.

the class JsonbBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
    MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
    context.contentType(contentType);
    AtomicBoolean first = new AtomicBoolean(true);
    ObjectToChunks jsonToChunks = new ObjectToChunks(jsonb, context.charset());
    return Multi.create(publisher).flatMap(jsonToChunks).flatMap(it -> {
        if (first.getAndSet(false)) {
            // first record, do not prepend a comma
            return Multi.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES), it);
        } else {
            // any subsequent record starts with a comma
            return Multi.just(DataChunk.create(COMMA_BYTES), it);
        }
    }).onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
Also used : Objects(java.util.Objects) Jsonb(jakarta.json.bind.Jsonb) Flow(java.util.concurrent.Flow) DataChunk(io.helidon.common.http.DataChunk) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) ObjectToChunks(io.helidon.media.jsonb.JsonbBodyWriter.ObjectToChunks) GenericType(io.helidon.common.GenericType) StandardCharsets(java.nio.charset.StandardCharsets) Multi(io.helidon.common.reactive.Multi) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ObjectToChunks(io.helidon.media.jsonb.JsonbBodyWriter.ObjectToChunks) MediaType(io.helidon.common.http.MediaType)

Aggregations

MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)6 DataChunk (io.helidon.common.http.DataChunk)5 GenericType (io.helidon.common.GenericType)4 MediaType (io.helidon.common.http.MediaType)4 Multi (io.helidon.common.reactive.Multi)4 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Flow (java.util.concurrent.Flow)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Objects (java.util.Objects)3 Jsonb (jakarta.json.bind.Jsonb)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Single (io.helidon.common.reactive.Single)1 MessageBodyReaderContext (io.helidon.media.common.MessageBodyReaderContext)1 ObjectToChunks (io.helidon.media.jsonb.JsonbBodyWriter.ObjectToChunks)1 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)1 JsonStructure (jakarta.json.JsonStructure)1 JsonWriterFactory (jakarta.json.JsonWriterFactory)1 URL (java.net.URL)1 Path (java.nio.file.Path)1