use of io.helidon.media.jsonb.JsonbBodyWriter.ObjectToChunks 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));
}
Aggregations