use of io.helidon.common.GenericType in project helidon by oracle.
the class JacksonBodyStreamWriter 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);
JacksonBodyWriter.ObjectToChunks objectToChunks = new JacksonBodyWriter.ObjectToChunks(objectMapper, context.charset());
return Multi.create(publisher).flatMap(objectToChunks).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.common.GenericType in project helidon by oracle.
the class JsonpStreamWriterTest method write.
private JsonArray write(Multi<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WRITER.write(publisher, type, CONTEXT).map(DataChunk::bytes).forEach(it -> {
try {
baos.write(it);
} catch (IOException ignored) {
// ignored
}
}).await();
return JSON_PARSER.createReader(new ByteArrayInputStream(baos.toByteArray())).readArray();
}
use of io.helidon.common.GenericType 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.common.GenericType in project helidon by oracle.
the class JsonbNdBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
MediaType contentType = MediaType.APPLICATION_X_NDJSON;
context.contentType(contentType);
AtomicBoolean first = new AtomicBoolean(true);
return Multi.create(publisher).map(object -> DataChunk.create(jsonb.toJson(object).getBytes(StandardCharsets.UTF_8))).flatMap(dataChunk -> {
if (first.getAndSet(false)) {
return Single.just(dataChunk);
} else {
return Multi.just(DataChunk.create(NL), dataChunk);
}
});
}
use of io.helidon.common.GenericType in project helidon by oracle.
the class FormParamsBodyReader method read.
@Override
@SuppressWarnings("unchecked")
public <U extends FormParams> Single<U> read(Flow.Publisher<DataChunk> publisher, GenericType<U> type, MessageBodyReaderContext context) {
MediaType mediaType = context.contentType().orElseThrow();
Charset charset = mediaType.charset().map(Charset::forName).orElse(StandardCharsets.UTF_8);
Function<String, String> decoder = decoder(mediaType, charset);
return (Single<U>) ContentReaders.readString(publisher, charset).map(formStr -> create(formStr, mediaType, decoder));
}
Aggregations