Search in sources :

Example 11 with GenericType

use of io.helidon.common.GenericType in project helidon by oracle.

the class JacksonBodyReaderTest method testDeserializeWithGenerics.

@Test
void testDeserializeWithGenerics() throws Exception {
    JacksonBodyReader reader = JacksonBodyReader.create(new ObjectMapper());
    DataChunk dataChunk = DataChunk.create("[{\"title\":\"The Stand\"}]".getBytes(StandardCharsets.UTF_8));
    List<Book> books = reader.read(Single.just(dataChunk), new GenericType<List<Book>>() {
    }, MessageBodyReaderContext.create()).get();
    assertThat(books.size(), is(1));
    assertThat(books.get(0), notNullValue());
}
Also used : GenericType(io.helidon.common.GenericType) DataChunk(io.helidon.common.http.DataChunk) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 12 with GenericType

use of io.helidon.common.GenericType 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

GenericType (io.helidon.common.GenericType)12 DataChunk (io.helidon.common.http.DataChunk)8 MediaType (io.helidon.common.http.MediaType)6 Multi (io.helidon.common.reactive.Multi)6 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)5 Objects (java.util.Objects)5 Flow (java.util.concurrent.Flow)5 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Single (io.helidon.common.reactive.Single)3 Optional (java.util.Optional)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Routing (io.helidon.webserver.Routing)2 JsonStructure (jakarta.json.JsonStructure)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Test (org.junit.jupiter.api.Test)2 Prioritized (io.helidon.common.Prioritized)1