diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-06-25 16:37:40 -0700 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-06-25 16:37:40 -0700 |
commit | a83b73ffce156a5c51805f5736828fcc1e1305be (patch) | |
tree | d96bce696f91f67108dd3db2b63014057cc0bcda /src | |
parent | b28ffa202e0e177d9f8b6b57bd00eb206f37b933 (diff) | |
download | jackson-databind-a83b73ffce156a5c51805f5736828fcc1e1305be.tar.gz |
Fixed #1852
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java | 62 | ||||
-rw-r--r-- | src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java | 16 |
2 files changed, 63 insertions, 15 deletions
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java index 2d6ed0e7f..1d13bfc9f 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java @@ -398,20 +398,25 @@ public abstract class StdDeserializer<T> return false; } text = text.trim(); - // [databind#422]: Allow aliases - // 13-Jun-2020, tatu: ... should we consider "lenient" vs "strict" here? - if ("true".equals(text) || "True".equals(text)) { - return true; - } - if ("false".equals(text) || "False".equals(text)) { - return false; + final int len = text.length(); + + // For [databind#1852] allow some case-insensitive matches (namely, + // true/True/TRUE, false/False/FALSE + if (len == 4) { + if (_isTrue(text)) { + return true; + } + } else if (len == 5) { + if (_isFalse(text)) { + return false; + } } if (_hasTextualNull(text)) { _verifyNullForPrimitiveCoercion(ctxt, text); return false; } Boolean b = (Boolean) ctxt.handleWeirdStringValue(Boolean.TYPE, text, - "only \"true\" or \"false\" recognized"); + "only \"true\"/\"True\"/\"TRUE\" or \"false\"/\"False\"/\"FALSE\" recognized"); return Boolean.TRUE.equals(b); } // 12-Jun-2020, tatu: For some reason calling `_deserializeFromArray()` won't work so: @@ -424,6 +429,29 @@ public abstract class StdDeserializer<T> return ((Boolean) ctxt.handleUnexpectedToken(Boolean.TYPE, p)).booleanValue(); } + // [databind#1852] + protected boolean _isTrue(String text) { + char c = text.charAt(0); + if (c == 't') { + return "true".equals(text); + } + if (c == 'T') { + return "TRUE".equals(text) || "True".equals(text); + } + return false; + } + + protected boolean _isFalse(String text) { + char c = text.charAt(0); + if (c == 'f') { + return "false".equals(text); + } + if (c == 'F') { + return "FALSE".equals(text) || "False".equals(text); + } + return false; + } + /** * Helper method called for cases where non-primitive, boolean-based value * is to be deserialized: result of this method will be {@link java.lang.Boolean}, @@ -458,12 +486,18 @@ public abstract class StdDeserializer<T> return false; } text = text.trim(); - // [databind#422]: Allow aliases - if ("true".equals(text) || "True".equals(text)) { - return true; - } - if ("false".equals(text) || "False".equals(text)) { - return false; + final int len = text.length(); + + // For [databind#1852] allow some case-insensitive matches (namely, + // true/True/TRUE, false/False/FALSE + if (len == 4) { + if (_isTrue(text)) { + return true; + } + } else if (len == 5) { + if (_isFalse(text)) { + return false; + } } if (_checkTextualNull(ctxt, text)) { return null; diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java index 3b5610901..0fbade77e 100644 --- a/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/convert/CoerceJDKScalarsTest.java @@ -113,7 +113,7 @@ public class CoerceJDKScalarsTest extends BaseMapTest /********************************************************** */ - public void testStringCoercionOk() throws Exception + public void testStringCoercionOkBoolean() throws Exception { // first successful coercions. Boolean has a ton... _verifyCoerceSuccess("1", Boolean.TYPE, Boolean.TRUE); @@ -122,13 +122,20 @@ public class CoerceJDKScalarsTest extends BaseMapTest _verifyCoerceSuccess(quote("true"), Boolean.class, Boolean.TRUE); _verifyCoerceSuccess(quote("True"), Boolean.TYPE, Boolean.TRUE); _verifyCoerceSuccess(quote("True"), Boolean.class, Boolean.TRUE); + _verifyCoerceSuccess(quote("TRUE"), Boolean.TYPE, Boolean.TRUE); + _verifyCoerceSuccess(quote("TRUE"), Boolean.class, Boolean.TRUE); _verifyCoerceSuccess("0", Boolean.TYPE, Boolean.FALSE); _verifyCoerceSuccess("0", Boolean.class, Boolean.FALSE); _verifyCoerceSuccess(quote("false"), Boolean.TYPE, Boolean.FALSE); _verifyCoerceSuccess(quote("false"), Boolean.class, Boolean.FALSE); _verifyCoerceSuccess(quote("False"), Boolean.TYPE, Boolean.FALSE); _verifyCoerceSuccess(quote("False"), Boolean.class, Boolean.FALSE); + _verifyCoerceSuccess(quote("FALSE"), Boolean.TYPE, Boolean.FALSE); + _verifyCoerceSuccess(quote("FALSE"), Boolean.class, Boolean.FALSE); + } + public void testStringCoercionOkNumbers() throws Exception + { _verifyCoerceSuccess(quote("123"), Byte.TYPE, Byte.valueOf((byte) 123)); _verifyCoerceSuccess(quote("123"), Byte.class, Byte.valueOf((byte) 123)); _verifyCoerceSuccess(quote("123"), Short.TYPE, Short.valueOf((short) 123)); @@ -154,6 +161,13 @@ public class CoerceJDKScalarsTest extends BaseMapTest { _verifyRootStringCoerceFail("true", Boolean.TYPE); _verifyRootStringCoerceFail("true", Boolean.class); + _verifyRootStringCoerceFail("True", Boolean.TYPE); + _verifyRootStringCoerceFail("True", Boolean.class); + _verifyRootStringCoerceFail("TRUE", Boolean.TYPE); + _verifyRootStringCoerceFail("TRUE", Boolean.class); + + _verifyRootStringCoerceFail("false", Boolean.TYPE); + _verifyRootStringCoerceFail("false", Boolean.class); } public void testStringCoercionFailInteger() throws Exception |