Skip to content

Commit d01e3ab

Browse files
committed
Minor cleanup for #1235, added checks for error handlers to catch type coercion issues
1 parent 68d1c17 commit d01e3ab

File tree

7 files changed

+73
-37
lines changed

7 files changed

+73
-37
lines changed

release-notes/CREDITS

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Benson Margulies:
147147
(2.6.3)
148148
* Reported #1120: String value omitted from weirdStringException
149149
(2.6.6)
150+
* Reported, fixed #1235: `java.nio.file.Path` support incomplete
151+
(2.8.0)
150152

151153
Steve Sanbeg: (sanbeg@github)
152154
* Contributed #482: Make date parsing error behavior consistent with JDK

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Project: jackson-databind
3737
#1221: Use `Throwable.addSuppressed()` directly and/or via try-with-resources
3838
#1232: Add support for `JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES`
3939
#1232: Add support for `JsonFormat.Feature.WRITE_SORTED_MAP_ENTRIES`
40+
#1235: `java.nio.file.Path` support incomplete
41+
(reported by, fix contributed by Benson M)
4042

4143
2.7.5 (not yet released)
4244

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

+32-8
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,13 @@ public Object handleWeirdKey(Class<?> keyClass, String keyValue,
871871
// Can bail out if it's handled
872872
Object key = h.value().handleWeirdKey(this, keyClass, keyValue, msg);
873873
if (key != DeserializationProblemHandler.NOT_HANDLED) {
874-
return key;
874+
// Sanity check for broken handlers, otherwise nasty to debug:
875+
if ((key == null) || keyClass.isInstance(key)) {
876+
return key;
877+
}
878+
throw weirdStringException(keyValue, keyClass, String.format(
879+
"DeserializationProblemHandler.handleWeirdStringValue() for type %s returned value of type %s",
880+
keyClass, key.getClass()));
875881
}
876882
h = h.next();
877883
}
@@ -909,9 +915,15 @@ public Object handleWeirdStringValue(Class<?> targetClass, String value,
909915
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
910916
while (h != null) {
911917
// Can bail out if it's handled
912-
Object key = h.value().handleWeirdStringValue(this, targetClass, value, msg);
913-
if (key != DeserializationProblemHandler.NOT_HANDLED) {
914-
return key;
918+
Object instance = h.value().handleWeirdStringValue(this, targetClass, value, msg);
919+
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
920+
// Sanity check for broken handlers, otherwise nasty to debug:
921+
if ((instance == null) || targetClass.isInstance(instance)) {
922+
return instance;
923+
}
924+
throw weirdStringException(value, targetClass, String.format(
925+
"DeserializationProblemHandler.handleWeirdStringValue() for type %s returned value of type %s",
926+
targetClass, instance.getClass()));
915927
}
916928
h = h.next();
917929
}
@@ -951,7 +963,13 @@ public Object handleWeirdNumberValue(Class<?> targetClass, Number value,
951963
// Can bail out if it's handled
952964
Object key = h.value().handleWeirdNumberValue(this, targetClass, value, msg);
953965
if (key != DeserializationProblemHandler.NOT_HANDLED) {
954-
return key;
966+
// Sanity check for broken handlers, otherwise nasty to debug:
967+
if ((key == null) || targetClass.isInstance(key)) {
968+
return key;
969+
}
970+
throw weirdNumberException(value, targetClass, String.format(
971+
"DeserializationProblemHandler.handleWeirdNumberValue() for type %s returned value of type %s",
972+
targetClass, key.getClass()));
955973
}
956974
h = h.next();
957975
}
@@ -1025,9 +1043,15 @@ public Object handleInstantiationProblem(Class<?> instClass, Object argument,
10251043
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
10261044
while (h != null) {
10271045
// Can bail out if it's handled
1028-
Object key = h.value().handleInstantiationProblem(this, instClass, argument, t);
1029-
if (key != DeserializationProblemHandler.NOT_HANDLED) {
1030-
return key;
1046+
Object instance = h.value().handleInstantiationProblem(this, instClass, argument, t);
1047+
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
1048+
// Sanity check for broken handlers, otherwise nasty to debug:
1049+
if ((instance == null) || instClass.isInstance(instance)) {
1050+
return instance;
1051+
}
1052+
throw instantiationException(instClass, String.format(
1053+
"DeserializationProblemHandler.handleInstantiationProblem() for type %s returned value of type %s",
1054+
instClass, instance.getClass()));
10311055
}
10321056
h = h.next();
10331057
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
package com.fasterxml.jackson.databind.ext;
22

3-
import com.fasterxml.jackson.core.JsonParser;
4-
import com.fasterxml.jackson.core.JsonToken;
5-
import com.fasterxml.jackson.databind.DeserializationContext;
6-
import com.fasterxml.jackson.databind.JsonDeserializer;
7-
83
import java.io.IOException;
94
import java.net.URI;
105
import java.net.URISyntaxException;
116
import java.nio.file.Path;
127
import java.nio.file.Paths;
138

9+
import com.fasterxml.jackson.core.JsonParser;
10+
import com.fasterxml.jackson.core.JsonToken;
11+
import com.fasterxml.jackson.databind.DeserializationContext;
12+
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
13+
1414
/**
15-
*
15+
* @since 2.8
1616
*/
17-
public class NioPathDeserializer extends JsonDeserializer<Path> {
17+
public class NioPathDeserializer extends StdScalarDeserializer<Path>
18+
{
19+
private static final long serialVersionUID = 1;
20+
21+
public NioPathDeserializer() { super(Path.class); }
22+
1823
@Override
1924
public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
20-
if (p.getCurrentToken() == JsonToken.VALUE_NULL) {
21-
return null;
22-
} else if (p.getCurrentToken() != JsonToken.VALUE_STRING) {
23-
throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "The value of a java.nio.file.Path must be a string");
25+
if (!p.hasToken(JsonToken.VALUE_STRING)) {
26+
// 19-May-2016, tatu: Need to rework as part of exception handling improvements for 2.8;
27+
// for now use simpler call only because `wrongTokenException()` will mean something
28+
// different, and mismatch will need different call
29+
// throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "The value of a java.nio.file.Path must be a string");
30+
ctxt.reportMappingException(Path.class, p.getCurrentToken());
2431
}
25-
URI uri;
32+
final String value = p.getText();
2633
try {
27-
uri = new URI(p.readValueAs(String.class));
34+
URI uri = new URI(value);
35+
return Paths.get(uri);
2836
} catch (URISyntaxException e) {
29-
throw ctxt.instantiationException(Path.class, e);
37+
return (Path) ctxt.handleInstantiationProblem(handledType(), value, e);
3038
}
31-
return Paths.get(uri);
3239
}
3340
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11

22
package com.fasterxml.jackson.databind.ext;
33

4-
import com.fasterxml.jackson.core.JsonGenerator;
5-
import com.fasterxml.jackson.databind.JsonSerializer;
6-
import com.fasterxml.jackson.databind.SerializerProvider;
7-
84
import java.io.IOException;
95
import java.nio.file.Path;
106

7+
import com.fasterxml.jackson.core.JsonGenerator;
8+
import com.fasterxml.jackson.databind.SerializerProvider;
9+
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
10+
1111
/**
12-
*
12+
* @since 2.8
1313
*/
14-
public class NioPathSerializer extends JsonSerializer<Path> {
14+
public class NioPathSerializer extends StdScalarSerializer<Path>
15+
{
16+
private static final long serialVersionUID = 1;
17+
18+
public NioPathSerializer() { super(Path.class); }
19+
1520
@Override
1621
public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
17-
if (value == null) {
18-
gen.writeNull();
19-
} else {
20-
// write the Path as a URI, always.
21-
gen.writeString(value.toUri().toString());
22-
}
22+
// write the Path as a URI, always.
23+
gen.writeString(value.toUri().toString());
2324
}
2425
}

src/main/java/com/fasterxml/jackson/databind/ext/OptionalHandlerFactory.java

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.databind.*;
44
import com.fasterxml.jackson.databind.deser.Deserializers;
55
import com.fasterxml.jackson.databind.ser.Serializers;
6-
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
76

87
/**
98
* Helper class used for isolating details of handling optional+external types

src/test/java/com/fasterxml/jackson/databind/ext/TestJdk7Types.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.nio.file.FileSystem;
44
import java.nio.file.Path;
5-
import java.nio.file.Paths;
65

76
import com.fasterxml.jackson.databind.*;
7+
88
import com.google.common.jimfs.Configuration;
99
import com.google.common.jimfs.Jimfs;
1010

@@ -28,5 +28,6 @@ public void testPathRoundtrip() throws Exception
2828

2929
assertEquals(input.toUri(), p.toUri());
3030
assertEquals(input, p);
31+
fs.close();
3132
}
3233
}

0 commit comments

Comments
 (0)