Search in sources :

Example 1 with JSON_VIEW_HINT

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

the class Jackson2JsonDecoderTests method fieldLevelJsonView.

@Test
public void fieldLevelJsonView() {
    Flux<DataBuffer> input = Flux.from(stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
    ResolvableType elementType = ResolvableType.fromClass(JacksonViewBean.class);
    Map<String, Object> hints = Collections.singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
    testDecode(input, elementType, step -> step.consumeNextWith(value -> {
        JacksonViewBean bean = (JacksonViewBean) value;
        assertThat(bean.getWithView1()).isEqualTo("with");
        assertThat(bean.getWithView2()).isNull();
        assertThat(bean.getWithoutView()).isNull();
    }), null, hints);
}
Also used : Arrays(java.util.Arrays) MyJacksonView1(cn.taketoday.http.codec.json.JacksonViewBean.MyJacksonView1) StepVerifier(reactor.test.StepVerifier) MyJacksonView3(cn.taketoday.http.codec.json.JacksonViewBean.MyJacksonView3) Pojo(cn.taketoday.http.codec.Pojo) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CodecException(cn.taketoday.core.codec.CodecException) APPLICATION_JSON(cn.taketoday.http.MediaType.APPLICATION_JSON) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) APPLICATION_NDJSON(cn.taketoday.http.MediaType.APPLICATION_NDJSON) BigDecimal(java.math.BigDecimal) Charset(java.nio.charset.Charset) APPLICATION_STREAM_JSON(cn.taketoday.http.MediaType.APPLICATION_STREAM_JSON) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(cn.taketoday.core.TypeReference) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ResolvableType(cn.taketoday.core.ResolvableType) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) AbstractDecoderTests(cn.taketoday.core.codec.AbstractDecoderTests) MediaType(cn.taketoday.http.MediaType) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) APPLICATION_XML(cn.taketoday.http.MediaType.APPLICATION_XML) JSON_VIEW_HINT(cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) DecodingException(cn.taketoday.core.codec.DecodingException) MimeType(cn.taketoday.util.MimeType) ResolvableType(cn.taketoday.core.ResolvableType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with JSON_VIEW_HINT

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

the class BodyExtractorsTests method toFluxWithHints.

@Test
public void toFluxWithHints() {
    BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    String text = "[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]";
    byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Flux<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertThat(user.getUsername()).isEqualTo("foo");
        assertThat(user.getPassword()).isNull();
    }).consumeNextWith(user -> {
        assertThat(user.getUsername()).isEqualTo("bar");
        assertThat(user.getPassword()).isNull();
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) JsonView(com.fasterxml.jackson.annotation.JsonView) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) StepVerifier(reactor.test.StepVerifier) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) FilePart(cn.taketoday.http.codec.multipart.FilePart) FormFieldPart(cn.taketoday.http.codec.multipart.FormFieldPart) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MultiValueMap(cn.taketoday.core.MultiValueMap) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) TypeReference(cn.taketoday.core.TypeReference) StringDecoder(cn.taketoday.core.codec.StringDecoder) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) Mono(reactor.core.publisher.Mono) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerHttpRequest(cn.taketoday.http.server.reactive.ServerHttpRequest) Optional(java.util.Optional) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) HttpStatus(cn.taketoday.http.HttpStatus) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Part(cn.taketoday.http.codec.multipart.Part) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) JSON_VIEW_HINT(cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with JSON_VIEW_HINT

use of cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT in project today-infrastructure by TAKETODAY.

the class Jackson2JsonDecoderTests method fieldLevelJsonView.

@Test
public void fieldLevelJsonView() {
    Flux<DataBuffer> input = Flux.from(stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
    ResolvableType elementType = ResolvableType.fromClass(JacksonViewBean.class);
    Map<String, Object> hints = Collections.singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
    testDecode(input, elementType, step -> step.consumeNextWith(value -> {
        JacksonViewBean bean = (JacksonViewBean) value;
        assertThat(bean.getWithView1()).isEqualTo("with");
        assertThat(bean.getWithView2()).isNull();
        assertThat(bean.getWithoutView()).isNull();
    }), null, hints);
}
Also used : Arrays(java.util.Arrays) MyJacksonView1(cn.taketoday.http.codec.json.JacksonViewBean.MyJacksonView1) StepVerifier(reactor.test.StepVerifier) MyJacksonView3(cn.taketoday.http.codec.json.JacksonViewBean.MyJacksonView3) Pojo(cn.taketoday.http.codec.Pojo) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CodecException(cn.taketoday.core.codec.CodecException) APPLICATION_JSON(cn.taketoday.http.MediaType.APPLICATION_JSON) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) APPLICATION_NDJSON(cn.taketoday.http.MediaType.APPLICATION_NDJSON) BigDecimal(java.math.BigDecimal) Charset(java.nio.charset.Charset) APPLICATION_STREAM_JSON(cn.taketoday.http.MediaType.APPLICATION_STREAM_JSON) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(cn.taketoday.core.TypeReference) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ResolvableType(cn.taketoday.core.ResolvableType) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) AbstractDecoderTests(cn.taketoday.core.codec.AbstractDecoderTests) MediaType(cn.taketoday.http.MediaType) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) APPLICATION_XML(cn.taketoday.http.MediaType.APPLICATION_XML) JSON_VIEW_HINT(cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) DecodingException(cn.taketoday.core.codec.DecodingException) MimeType(cn.taketoday.util.MimeType) ResolvableType(cn.taketoday.core.ResolvableType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with JSON_VIEW_HINT

use of cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT in project today-infrastructure by TAKETODAY.

the class BodyExtractorsTests method toFluxWithHints.

@Test
public void toFluxWithHints() {
    BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    String text = "[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]";
    byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Flux<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertThat(user.getUsername()).isEqualTo("foo");
        assertThat(user.getPassword()).isNull();
    }).consumeNextWith(user -> {
        assertThat(user.getUsername()).isEqualTo("bar");
        assertThat(user.getPassword()).isNull();
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) JsonView(com.fasterxml.jackson.annotation.JsonView) NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) StepVerifier(reactor.test.StepVerifier) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) FilePart(cn.taketoday.http.codec.multipart.FilePart) FormFieldPart(cn.taketoday.http.codec.multipart.FormFieldPart) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MultiValueMap(cn.taketoday.core.MultiValueMap) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) TypeReference(cn.taketoday.core.TypeReference) StringDecoder(cn.taketoday.core.codec.StringDecoder) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) MockClientHttpResponse(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpResponse) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) Mono(reactor.core.publisher.Mono) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerHttpRequest(cn.taketoday.http.server.reactive.ServerHttpRequest) Optional(java.util.Optional) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) HttpStatus(cn.taketoday.http.HttpStatus) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Part(cn.taketoday.http.codec.multipart.Part) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) JSON_VIEW_HINT(cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(cn.taketoday.http.ReactiveHttpInputMessage) NettyDataBuffer(cn.taketoday.core.io.buffer.NettyDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

TypeReference (cn.taketoday.core.TypeReference)4 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)4 MediaType (cn.taketoday.http.MediaType)4 JSON_VIEW_HINT (cn.taketoday.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Collections (java.util.Collections)4 List (java.util.List)4 Map (java.util.Map)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)4 Test (org.junit.jupiter.api.Test)4 Flux (reactor.core.publisher.Flux)4 Mono (reactor.core.publisher.Mono)4 StepVerifier (reactor.test.StepVerifier)4 MultiValueMap (cn.taketoday.core.MultiValueMap)2 ByteBufferDecoder (cn.taketoday.core.codec.ByteBufferDecoder)2 StringDecoder (cn.taketoday.core.codec.StringDecoder)2 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)2 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 NettyDataBuffer (cn.taketoday.core.io.buffer.NettyDataBuffer)2