-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa3ef3e
commit a39892c
Showing
10 changed files
with
200 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/fasterxml/jackson/databind/deser/impl/UnsupportedTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.fasterxml.jackson.databind.deser.impl; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
||
/** | ||
* Special bogus "serializer" that will throw | ||
* {@link com.fasterxml.jackson.databind.exc.MismatchedInputException} | ||
* if an attempt is made to deserialize a value. | ||
* This is used for "known unknown" types: types that we can recognize | ||
* but can not support easily (or support known to be added via extension | ||
* module). | ||
* | ||
* @since 2.12 | ||
*/ | ||
public class UnsupportedTypeDeserializer extends StdDeserializer<Object> | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
protected final JavaType _type; | ||
|
||
protected final String _message; | ||
|
||
public UnsupportedTypeDeserializer(JavaType t, String m) { | ||
super(t); | ||
_type = t; | ||
_message = m; | ||
} | ||
|
||
@Override | ||
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
ctxt.reportBadDefinition(_type, _message); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/fasterxml/jackson/databind/ser/impl/UnsupportedTypeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.fasterxml.jackson.databind.ser.impl; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
/** | ||
* Special bogus "serializer" that will throw | ||
* {@link com.fasterxml.jackson.databind.exc.InvalidDefinitionException} if its {@link #serialize} | ||
* gets invoked. Most commonly registered as handler for unknown types, | ||
* as well as for catching unintended usage (like trying to use null | ||
* as Map/Object key). | ||
*/ | ||
public class UnsupportedTypeSerializer | ||
extends StdSerializer<Object> | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
protected final JavaType _type; | ||
|
||
protected final String _message; | ||
|
||
public UnsupportedTypeSerializer(JavaType t, String msg) { | ||
super(Object.class); | ||
_type = t; | ||
_message = msg; | ||
} | ||
|
||
@Override | ||
public void serialize(Object value, JsonGenerator g, SerializerProvider ctxt) throws IOException { | ||
ctxt.reportBadDefinition(_type, _message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateJava8FallbacksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.fasterxml.jackson.databind.deser.jdk; | ||
|
||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; | ||
|
||
// [databind#2683]: add fallback handling for Java 8 date/time types, to | ||
// prevent accidental serialzization as POJOs, as well as give more information | ||
// on deserialization attempts | ||
public class DateJava8FallbacksTest extends BaseMapTest | ||
{ | ||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
private final OffsetDateTime DATETIME_EPOCH = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), | ||
ZoneOffset.of("Z")); | ||
|
||
// Test to prevent serialization as POJO, without Java 8 date/time module: | ||
public void testPreventSerialization() throws Exception | ||
{ | ||
try { | ||
String json = MAPPER.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(DATETIME_EPOCH); | ||
fail("Should not pass, wrote out as\n: "+json); | ||
} catch (InvalidDefinitionException e) { | ||
verifyException(e, "Java 8 date/time type `java.time.OffsetDateTime` not supported by default"); | ||
verifyException(e, "please register module `jackson-datatype-jsr310`"); | ||
} | ||
} | ||
|
||
public void testBetterDeserializationError() throws Exception | ||
{ | ||
try { | ||
OffsetDateTime result = MAPPER.readValue(" 0 ", OffsetDateTime.class); | ||
fail("Not expecting to pass, resulted in: "+result); | ||
} catch (InvalidDefinitionException e) { | ||
verifyException(e, "Java 8 date/time type `java.time.OffsetDateTime` not supported by default"); | ||
verifyException(e, "please register module `jackson-datatype-jsr310`"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters