Skip to content

Commit

Permalink
Fix #4570: deprecate ObjectMapper.canDeserialize()/canSerialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 7, 2024
1 parent 894e23a commit b6d22cb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3636,7 +3636,10 @@ public <T extends JsonNode> 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);
}
Expand All @@ -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<Throwable> cause) {
return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, cause);
}
Expand All @@ -3668,7 +3674,10 @@ public boolean canSerialize(Class<?> type, AtomicReference<Throwable> 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,
Expand All @@ -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<Throwable> cause)
{
return createDeserializationContext(null,
Expand All @@ -3690,8 +3702,7 @@ public boolean canDeserialize(JavaType type, AtomicReference<Throwable> cause)

/*
/**********************************************************
/* Extended Public API, deserialization,
/* convenience methods
/* Extended Public API, deserialization, convenience methods
/**********************************************************
*/

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Throwable> cause) {
_assertNotNull("type", type);
return _serializerProvider().hasSerializerFor(type, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ public void testCustomDefaultPrettyPrinter() throws Exception
}

// For [databind#703], [databind#978]
@SuppressWarnings("deprecation")
@Test
public void testNonSerializabilityOfObject()
{
Expand All @@ -420,6 +421,7 @@ public void testNonSerializabilityOfObject()
}

// for [databind#756]
@SuppressWarnings("deprecation")
@Test
public void testEmptyBeanSerializability()
{
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void testPolymorphicWithTyping() throws Exception
assertEquals(a2q("{'type':'B','b':-5}"), json);
}

@SuppressWarnings("deprecation")
@Test
public void testCanSerialize() throws Exception
{
Expand Down

0 comments on commit b6d22cb

Please sign in to comment.