use of io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks in project helidon by oracle.
the class JsonpBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
context.contentType(contentType);
// we do not have join operator
AtomicBoolean first = new AtomicBoolean(true);
JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
return Multi.create(publisher).map(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));
}
use of io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks in project helidon by oracle.
the class JsonpNdBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(MediaType.APPLICATION_X_NDJSON);
context.contentType(contentType);
JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
AtomicBoolean first = new AtomicBoolean(true);
return Multi.create(publisher).map(jsonToChunks).flatMap(dataChunk -> {
if (first.getAndSet(false)) {
return Single.just(dataChunk);
} else {
return Multi.just(DataChunk.create(NL), dataChunk);
}
});
}
use of io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks in project helidon by oracle.
the class JsonpEsBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(TEXT_EVENT_STREAM_JSON);
context.contentType(contentType);
JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
return Multi.create(publisher).map(jsonToChunks).flatMap(dataChunk -> Multi.just(DataChunk.create(DATA), dataChunk, DataChunk.create(NL)));
}
Aggregations