Search in sources :

Example 11 with Jackson2JsonEncoder

use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.

the class CodecConfigurerTests method cloneDefaultCodecs.

@Test
void cloneDefaultCodecs() {
    CodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
    ProtobufDecoder protoDecoder = new ProtobufDecoder();
    ProtobufEncoder protoEncoder = new ProtobufEncoder();
    clone.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
    clone.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
    clone.defaultCodecs().protobufDecoder(protoDecoder);
    clone.defaultCodecs().protobufEncoder(protoEncoder);
    // Clone has the customized the customizations
    List<Decoder<?>> decoders = clone.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    List<Encoder<?>> encoders = clone.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    // Original does not have the customizations
    decoders = this.configurer.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    encoders = this.configurer.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) Decoder(cn.taketoday.core.codec.Decoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) StringDecoder(cn.taketoday.core.codec.StringDecoder) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) Collectors(java.util.stream.Collectors) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Test(org.junit.jupiter.api.Test) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) List(java.util.List) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Encoder(cn.taketoday.core.codec.Encoder) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Encoder(cn.taketoday.core.codec.Encoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) Decoder(cn.taketoday.core.codec.Decoder) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) StringDecoder(cn.taketoday.core.codec.StringDecoder) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 12 with Jackson2JsonEncoder

use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.

the class ServerCodecConfigurerTests method jackson2EncoderOverride.

@Test
public void jackson2EncoderOverride() {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
    this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
    assertThat(actualDecoder).isSameAs(decoder);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
    assertThat(actualEncoder).isSameAs(encoder);
    assertThat(findCodec(writers, ServerSentEventHttpMessageWriter.class).getEncoder()).isSameAs(encoder);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) PartEventHttpMessageReader(cn.taketoday.http.codec.multipart.PartEventHttpMessageReader) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) PartHttpMessageWriter(cn.taketoday.http.codec.multipart.PartHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 13 with Jackson2JsonEncoder

use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.

the class CancelWithoutDemandCodecTests method cancelWithJackson.

// gh-22107
@Test
public void cancelWithJackson() {
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    Flux<DataBuffer> flux = encoder.encode(Flux.just(new Pojo("foofoo", "barbar"), new Pojo("bar", "baz")), this.bufferFactory, ResolvableType.fromClass(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap());
    BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
    // Assume sync execution (e.g. encoding with Flux.just)..
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 14 with Jackson2JsonEncoder

use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.

the class ClientCodecConfigurerTests method objectMapperCustomization.

@Test
public void objectMapperCustomization() {
    ObjectMapper objectMapper = new ObjectMapper();
    this.configurer.defaultCodecs().configureDefaultCodec(codec -> {
        if (codec instanceof Jackson2CodecSupport) {
            ((Jackson2CodecSupport) codec).setObjectMapper(objectMapper);
        }
    });
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
    assertThat(actualDecoder.getObjectMapper()).isSameAs(objectMapper);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
    assertThat(actualEncoder.getObjectMapper()).isSameAs(objectMapper);
    MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
    actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
    assertThat(actualEncoder.getObjectMapper()).isSameAs(objectMapper);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Jackson2CodecSupport(cn.taketoday.http.codec.json.Jackson2CodecSupport) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 15 with Jackson2JsonEncoder

use of cn.taketoday.http.codec.json.Jackson2JsonEncoder in project today-framework by TAKETODAY.

the class CodecConfigurerTests method cloneCustomCodecs.

@Test
void cloneCustomCodecs() {
    this.configurer.registerDefaults(false);
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    this.configurer.customCodecs().register(new Jackson2JsonEncoder());
    this.configurer.customCodecs().register(new Jackson2JsonDecoder());
    this.configurer.customCodecs().register(new ServerSentEventHttpMessageReader());
    this.configurer.customCodecs().register(new ServerSentEventHttpMessageWriter());
    assertThat(this.configurer.getReaders()).hasSize(2);
    assertThat(this.configurer.getWriters()).hasSize(2);
    CodecConfigurer clone = this.configurer.clone();
    assertThat(this.configurer.getReaders()).hasSize(2);
    assertThat(this.configurer.getWriters()).hasSize(2);
    assertThat(clone.getReaders()).hasSize(2);
    assertThat(clone.getWriters()).hasSize(2);
}
Also used : ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) Test(org.junit.jupiter.api.Test)

Aggregations

Jackson2JsonEncoder (cn.taketoday.http.codec.json.Jackson2JsonEncoder)28 Test (org.junit.jupiter.api.Test)24 EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)14 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)14 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)12 ResourceHttpMessageWriter (cn.taketoday.http.codec.ResourceHttpMessageWriter)12 ServerSentEventHttpMessageWriter (cn.taketoday.http.codec.ServerSentEventHttpMessageWriter)12 ServerSentEventHttpMessageReader (cn.taketoday.http.codec.ServerSentEventHttpMessageReader)10 ProtobufHttpMessageWriter (cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)10 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)8 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)8 FormHttpMessageWriter (cn.taketoday.http.codec.FormHttpMessageWriter)8 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)8 ResourceHttpMessageReader (cn.taketoday.http.codec.ResourceHttpMessageReader)8 MultipartHttpMessageWriter (cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter)8 CodecConfigurer (cn.taketoday.http.codec.CodecConfigurer)6 PartEventHttpMessageWriter (cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter)6 ByteBufferEncoder (cn.taketoday.core.codec.ByteBufferEncoder)4 Jackson2SmileDecoder (cn.taketoday.http.codec.json.Jackson2SmileDecoder)4 Jackson2SmileEncoder (cn.taketoday.http.codec.json.Jackson2SmileEncoder)4