From 52ea58b5c894a406ad695d3b752eb2f2ad2d766b Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 7 Jun 2024 10:51:02 -0700 Subject: [PATCH] Fix #4570: deprecate ObjectMapper.canDeserialize()/canSerialize() (#4571) --- release-notes/VERSION-2.x | 1 + .../fasterxml/jackson/databind/ObjectMapper.java | 15 +++++++++++++-- .../fasterxml/jackson/databind/ObjectWriter.java | 10 ++++++++++ .../jackson/databind/ObjectMapperTest.java | 3 +++ .../jackson/databind/ObjectWriterTest.java | 1 + 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 429638cc4e..ea2453edd0 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -43,6 +43,7 @@ Project: jackson-databind #4545: Unexpected deserialization behavior with `@JsonCreator`, `@JsonProperty` and javac `-parameters` (reported by Alexandre J) +#4570: Deprecate `ObjectMapper.canDeserialize()`/`ObjectMapper.canSerialize()` 2.17.2 (not yet released) diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index e2843ca699..25f67bac06 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -3636,7 +3636,10 @@ public T valueToTree(Object fromValue) * @return True if mapper can find a serializer for instances of * given class (potentially serializable), false otherwise (not * serializable) + * + * @deprecated Since 2.18 use discouraged; method to be removed from Jackson 3.0 */ + @Deprecated // @since 2.18 public boolean canSerialize(Class type) { return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, null); } @@ -3647,7 +3650,10 @@ public boolean canSerialize(Class type) { * serializer: this may be useful in figuring out what the actual problem is. * * @since 2.3 + * + * @deprecated Since 2.18 use discouraged; method to be removed from Jackson 3.0 */ + @Deprecated // @since 2.18 public boolean canSerialize(Class type, AtomicReference cause) { return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, cause); } @@ -3668,7 +3674,10 @@ public boolean canSerialize(Class type, AtomicReference cause) { * @return True if mapper can find a serializer for instances of * given class (potentially serializable), false otherwise (not * serializable) + * + * @deprecated Since 2.18 use discouraged; method to be removed from Jackson 3.0 */ + @Deprecated // @since 2.18 public boolean canDeserialize(JavaType type) { return createDeserializationContext(null, @@ -3681,7 +3690,10 @@ public boolean canDeserialize(JavaType type) * serializer: this may be useful in figuring out what the actual problem is. * * @since 2.3 + * + * @deprecated Since 2.18 use discouraged; method to be removed from Jackson 3.0 */ + @Deprecated // @since 2.18 public boolean canDeserialize(JavaType type, AtomicReference cause) { return createDeserializationContext(null, @@ -3690,8 +3702,7 @@ public boolean canDeserialize(JavaType type, AtomicReference cause) /* /********************************************************** - /* Extended Public API, deserialization, - /* convenience methods + /* Extended Public API, deserialization, convenience methods /********************************************************** */ diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java index 503d1b123f..ed4fe845fd 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java @@ -1210,6 +1210,13 @@ public void acceptJsonFormatVisitor(Class type, JsonFormatVisitorWrapper visi acceptJsonFormatVisitor(_config.constructType(type), visitor); } + /** + * Method that can be called to check whether {@code ObjectWriter} thinks + * it could serialize an instance of given Class. + * + * @deprecated Since 2.18 use discouraged; method to be removed from Jackson 3.0 + */ + @Deprecated // @since 2.18 public boolean canSerialize(Class type) { _assertNotNull("type", type); return _serializerProvider().hasSerializerFor(type, null); @@ -1220,7 +1227,10 @@ public boolean canSerialize(Class type) { * and optionally why (as per {@link Throwable} returned). * * @since 2.3 + * + * @deprecated Since 2.18 use discouraged; method to be removed from Jackson 3.0 */ + @Deprecated // @since 2.18 public boolean canSerialize(Class type, AtomicReference cause) { _assertNotNull("type", type); return _serializerProvider().hasSerializerFor(type, cause); diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java index 681daf9f02..37cdbe6b9f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java @@ -401,6 +401,7 @@ public void testCustomDefaultPrettyPrinter() throws Exception } // For [databind#703], [databind#978] + @SuppressWarnings("deprecation") @Test public void testNonSerializabilityOfObject() { @@ -420,6 +421,7 @@ public void testNonSerializabilityOfObject() } // for [databind#756] + @SuppressWarnings("deprecation") @Test public void testEmptyBeanSerializability() { @@ -432,6 +434,7 @@ public void testEmptyBeanSerializability() } // for [databind#2749]: just to check there's no NPE; method really not useful + @SuppressWarnings("deprecation") @Test public void testCanDeserialize() { diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java index 1cec39e1b1..70b37caab1 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java @@ -132,6 +132,7 @@ public void testPolymorphicWithTyping() throws Exception assertEquals(a2q("{'type':'B','b':-5}"), json); } + @SuppressWarnings("deprecation") @Test public void testCanSerialize() throws Exception {