Refine Jackson canEncode() / canDecode() implementation
This commit refine Jackson canEncode() / canDecode() implementation by using ObjectMapper canSerialize() / canDeserialize() methods. Issue: SPR-14748
This commit is contained in:
parent
b91c0b3a1a
commit
dbc86ec043
|
|
@ -42,7 +42,7 @@ public interface Decoder<T> {
|
|||
* Whether the decoder supports the given target element type and the MIME
|
||||
* type of the source stream.
|
||||
* @param elementType the target element type for the output stream
|
||||
* @param mimeType the mime type associated with the stream to decode
|
||||
* @param mimeType the mime type associated with the stream to decode, can be {@code null} if not specified.
|
||||
* @return {@code true} if supported, {@code false} otherwise
|
||||
*/
|
||||
boolean canDecode(ResolvableType elementType, MimeType mimeType);
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public interface Encoder<T> {
|
|||
* Whether the encoder supports the given source element type and the MIME
|
||||
* type for the output stream.
|
||||
* @param elementType the type of elements in the source stream
|
||||
* @param mimeType the MIME type for the output stream
|
||||
* @param mimeType the MIME type for the output stream, can be {@code null} if not specified.
|
||||
* @return {@code true} if supported, {@code false} otherwise
|
||||
*/
|
||||
boolean canEncode(ResolvableType elementType, MimeType mimeType);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import java.util.Map;
|
|||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
|
@ -63,10 +64,9 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
|
|||
|
||||
@Override
|
||||
public boolean canDecode(ResolvableType elementType, MimeType mimeType) {
|
||||
if (mimeType == null) {
|
||||
return true;
|
||||
}
|
||||
return JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType));
|
||||
JavaType javaType = this.mapper.getTypeFactory().constructType(elementType.getType());
|
||||
return this.mapper.canDeserialize(javaType) &&
|
||||
(mimeType == null || JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -68,10 +68,8 @@ public class Jackson2JsonEncoder extends AbstractJackson2Codec implements Encode
|
|||
|
||||
@Override
|
||||
public boolean canEncode(ResolvableType elementType, MimeType mimeType) {
|
||||
if (mimeType == null) {
|
||||
return true;
|
||||
}
|
||||
return JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType));
|
||||
return this.mapper.canSerialize(elementType.getRawClass()) &&
|
||||
(mimeType == null || JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -46,9 +46,10 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
|
|||
@Test
|
||||
public void canDecode() {
|
||||
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
|
||||
|
||||
assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON));
|
||||
assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML));
|
||||
ResolvableType type = ResolvableType.forClass(Pojo.class);
|
||||
assertTrue(decoder.canDecode(type, MediaType.APPLICATION_JSON));
|
||||
assertTrue(decoder.canDecode(type, null));
|
||||
assertFalse(decoder.canDecode(type, MediaType.APPLICATION_XML));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
|||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
import org.springframework.http.codec.ServerSentEvent;
|
||||
import org.springframework.tests.TestSubscriber;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
|
@ -45,8 +46,12 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
|||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
assertTrue(this.encoder.canEncode(null, MediaType.APPLICATION_JSON));
|
||||
assertFalse(this.encoder.canEncode(null, MediaType.APPLICATION_XML));
|
||||
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
|
||||
assertTrue(this.encoder.canEncode(pojoType, MediaType.APPLICATION_JSON));
|
||||
assertTrue(this.encoder.canEncode(pojoType, null));
|
||||
assertFalse(this.encoder.canEncode(pojoType, MediaType.APPLICATION_XML));
|
||||
ResolvableType sseType = ResolvableType.forClass(ServerSentEvent.class);
|
||||
assertFalse(this.encoder.canEncode(sseType, MediaType.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue