diff --git a/src/main/java/tools/jackson/databind/exc/InvalidNullException.java b/src/main/java/tools/jackson/databind/exc/InvalidNullException.java index 7e5c018c00..778aab2d92 100644 --- a/src/main/java/tools/jackson/databind/exc/InvalidNullException.java +++ b/src/main/java/tools/jackson/databind/exc/InvalidNullException.java @@ -1,5 +1,7 @@ package tools.jackson.databind.exc; +import tools.jackson.core.JsonParser; + import tools.jackson.databind.DeserializationContext; import tools.jackson.databind.JavaType; import tools.jackson.databind.PropertyName; @@ -8,11 +10,9 @@ /** * Exception thrown if a `null` value is being encountered for a property * designed as "fail on null" property (see {@link com.fasterxml.jackson.annotation.JsonSetter}). - * - * @since 2.9 */ public class InvalidNullException - extends MismatchedInputException // since 2.9 + extends MismatchedInputException { private static final long serialVersionUID = 1L; // silly Eclipse, warnings @@ -27,11 +27,18 @@ public class InvalidNullException /********************************************************** */ + /** + * @since 2.19 + */ + protected InvalidNullException(JsonParser p, String msg, PropertyName pname) { + super(p, msg); + _propertyName = pname; + } + protected InvalidNullException(DeserializationContext ctxt, String msg, PropertyName pname) { - super(ctxt == null ? null : ctxt.getParser(), msg); - _propertyName = pname; + this(ctxt == null ? null : ctxt.getParser(), msg, pname); } public static InvalidNullException from(DeserializationContext ctxt, diff --git a/src/test/java/tools/jackson/databind/ObjectReaderTest.java b/src/test/java/tools/jackson/databind/ObjectReaderTest.java index beeb8d3382..4e22417d7b 100644 --- a/src/test/java/tools/jackson/databind/ObjectReaderTest.java +++ b/src/test/java/tools/jackson/databind/ObjectReaderTest.java @@ -462,7 +462,7 @@ public void testUnknownFields() throws Exception ObjectMapper mapper = JsonMapper.builder().addHandler(new DeserializationProblemHandler(){ @Override public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, ValueDeserializer deserializer, Object beanOrClass, String propertyName) { - p.readValueAsTree(); + ctxt.readTree(p); return true; } }).build(); diff --git a/src/test/java/tools/jackson/databind/convert/UpdateViaObjectReaderTest.java b/src/test/java/tools/jackson/databind/convert/UpdateViaObjectReaderTest.java index f41cff36fd..d9c942a627 100644 --- a/src/test/java/tools/jackson/databind/convert/UpdateViaObjectReaderTest.java +++ b/src/test/java/tools/jackson/databind/convert/UpdateViaObjectReaderTest.java @@ -76,7 +76,7 @@ public DataA deserialize(JsonParser p, DeserializationContext ctxt) +p.currentToken()); // never gets here } - /*JsonNode node =*/ p.readValueAsTree(); + /*JsonNode node =*/ ctxt.readTree(p); DataA da = new DataA(); da.i = 5; diff --git a/src/test/java/tools/jackson/databind/deser/CustomDeserializersTest.java b/src/test/java/tools/jackson/databind/deser/CustomDeserializersTest.java index 415e54b4d7..5f55b16b78 100644 --- a/src/test/java/tools/jackson/databind/deser/CustomDeserializersTest.java +++ b/src/test/java/tools/jackson/databind/deser/CustomDeserializersTest.java @@ -622,7 +622,7 @@ public void testCustomDeserializerWithReadTreeAsValue() throws Exception assertEquals(new Point(13, -4), result.point); // also, try some edge conditions - result = MAPPER.readValue(a2q("{'name':4})"), NamedPoint.class); + result = MAPPER.readValue(a2q("{'name':4}"), NamedPoint.class); assertNotNull(result); assertEquals("4", result.name); assertNull(result.point); diff --git a/src/test/java/tools/jackson/databind/deser/DeserConcurrencyTest.java b/src/test/java/tools/jackson/databind/deser/DeserConcurrencyTest.java index 6a34bf1277..9e49f047a6 100644 --- a/src/test/java/tools/jackson/databind/deser/DeserConcurrencyTest.java +++ b/src/test/java/tools/jackson/databind/deser/DeserConcurrencyTest.java @@ -41,6 +41,7 @@ public Bean deserialize(JsonParser p, DeserializationContext ctxt) ctxt.reportInputMismatch(Bean.class, "Deserializer not yet completely resolved"); } + p.skipChildren(); // consume the value Bean b = new Bean(); b.value = 13; return b; diff --git a/src/test/java/tools/jackson/databind/deser/JacksonTypesDeserTest.java b/src/test/java/tools/jackson/databind/deser/JacksonTypesDeserTest.java index 5f8eec3e58..fb62f5713b 100644 --- a/src/test/java/tools/jackson/databind/deser/JacksonTypesDeserTest.java +++ b/src/test/java/tools/jackson/databind/deser/JacksonTypesDeserTest.java @@ -78,21 +78,25 @@ public void testJavaType() throws Exception public void testTokenBufferWithSample() throws Exception { // First, try standard sample doc: - TokenBuffer result = MAPPER.readValue(SAMPLE_DOC_JSON_SPEC, TokenBuffer.class); - verifyJsonSpecSampleDoc(result.asParser(ObjectReadContext.empty()), true); - result.close(); + try (TokenBuffer result = MAPPER.readValue(SAMPLE_DOC_JSON_SPEC, TokenBuffer.class)) { + verifyJsonSpecSampleDoc(result.asParser(ObjectReadContext.empty()), true); + } } @SuppressWarnings("resource") @Test public void testTokenBufferWithSequence() throws Exception { + final ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .build(); + // and then sequence of other things - JsonParser p = MAPPER.createParser("[ 32, [ 1 ], \"abc\", { \"a\" : true } ]"); + JsonParser p = mapper.createParser("[ 32, [ 1 ], \"abc\", { \"a\" : true } ]"); assertToken(JsonToken.START_ARRAY, p.nextToken()); assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); - TokenBuffer buf = MAPPER.readValue(p, TokenBuffer.class); + TokenBuffer buf = mapper.readValue(p, TokenBuffer.class); // check manually... JsonParser bufParser = buf.asParser(ObjectReadContext.empty()); @@ -101,7 +105,7 @@ public void testTokenBufferWithSequence() throws Exception assertNull(bufParser.nextToken()); // then bind to another - buf = MAPPER.readValue(p, TokenBuffer.class); + buf = mapper.readValue(p, TokenBuffer.class); bufParser = buf.asParser(ObjectReadContext.empty()); assertToken(JsonToken.START_ARRAY, bufParser.nextToken()); assertToken(JsonToken.VALUE_NUMBER_INT, bufParser.nextToken()); @@ -110,13 +114,13 @@ public void testTokenBufferWithSequence() throws Exception assertNull(bufParser.nextToken()); // third one, with automatic binding - buf = MAPPER.readValue(p, TokenBuffer.class); - String str = MAPPER.readValue(buf.asParser(ObjectReadContext.empty()), String.class); + buf = mapper.readValue(p, TokenBuffer.class); + String str = mapper.readValue(buf.asParser(ObjectReadContext.empty()), String.class); assertEquals("abc", str); // and ditto for last one - buf = MAPPER.readValue(p, TokenBuffer.class); - Map map = MAPPER.readValue(buf.asParser(ObjectReadContext.empty()), Map.class); + buf = mapper.readValue(p, TokenBuffer.class); + Map map = mapper.readValue(buf.asParser(ObjectReadContext.empty()), Map.class); assertEquals(1, map.size()); assertEquals(Boolean.TRUE, map.get("a")); diff --git a/src/test/java/tools/jackson/databind/deser/SetterlessPropertiesDeserTest.java b/src/test/java/tools/jackson/databind/deser/SetterlessPropertiesDeserTest.java index 656595b473..e0abb6b894 100644 --- a/src/test/java/tools/jackson/databind/deser/SetterlessPropertiesDeserTest.java +++ b/src/test/java/tools/jackson/databind/deser/SetterlessPropertiesDeserTest.java @@ -23,14 +23,14 @@ public class SetterlessPropertiesDeserTest { static class CollectionBean { - List _values = new ArrayList(); + List _values = new ArrayList<>(); public List getValues() { return _values; } } static class MapBean { - Map _values = new HashMap(); + Map _values = new HashMap<>(); public Map getValues() { return _values; } } @@ -38,7 +38,7 @@ static class MapBean // testing to verify that field has precedence over getter, for lists static class Dual { - @JsonProperty("list") protected List values = new ArrayList(); + @JsonProperty("list") protected List values = new ArrayList<>(); public Dual() { } @@ -129,7 +129,7 @@ public void testSetterlessPrecedence() throws Exception ObjectMapper m = jsonMapperBuilder() .enable(MapperFeature.USE_GETTERS_AS_SETTERS) .build(); - Dual value = m.readValue("{\"list\":[1,2,3]}, valueType)", Dual.class); + Dual value = m.readValue("{\"list\":[1,2,3]}", Dual.class); assertNotNull(value); assertEquals(3, value.values.size()); } diff --git a/src/test/java/tools/jackson/databind/deser/bean/BeanDeserializerTest.java b/src/test/java/tools/jackson/databind/deser/bean/BeanDeserializerTest.java index 98290e2c8f..f16e2c2b25 100644 --- a/src/test/java/tools/jackson/databind/deser/bean/BeanDeserializerTest.java +++ b/src/test/java/tools/jackson/databind/deser/bean/BeanDeserializerTest.java @@ -100,8 +100,9 @@ public BogusBeanDeserializer(String a, String b) { } @Override - public Object deserialize(JsonParser jp, DeserializationContext ctxt) + public Object deserialize(JsonParser p, DeserializationContext ctxt) { + p.skipChildren(); return new Bean(a, b); } } @@ -168,7 +169,9 @@ public Issue1912SubBean(String a) { } } - public static class Issue1912CustomBeanDeserializer extends ValueDeserializer { + public static class Issue1912CustomBeanDeserializer + extends ValueDeserializer + { private BeanDeserializer defaultDeserializer; public Issue1912CustomBeanDeserializer(BeanDeserializer defaultDeserializer) { @@ -178,16 +181,26 @@ public Issue1912CustomBeanDeserializer(BeanDeserializer defaultDeserializer) { @Override public Issue1912Bean deserialize(JsonParser p, DeserializationContext ctxt) { - // this is need on some cases, this populate _propertyBasedCreator - defaultDeserializer.resolve(ctxt); - p.nextName(); // read subBean - p.nextToken(); // read start object + if (p.nextToken() != JsonToken.START_OBJECT) { + throw new IllegalArgumentException("Unexpected token "+p.currentToken()); + } Issue1912SubBean subBean = (Issue1912SubBean) defaultDeserializer.findProperty("subBean").deserialize(p, ctxt); + // Must also read trailing END_OBJECT + if (p.nextToken() != JsonToken.END_OBJECT) { + throw new IllegalArgumentException("Unexpected token "+p.currentToken()); + } return new Issue1912Bean(subBean); } + + @Override + public ValueDeserializer createContextual(DeserializationContext ctxt, BeanProperty property) + { + return new Issue1912CustomBeanDeserializer( + (BeanDeserializer) defaultDeserializer.createContextual(ctxt, property)); + } } public static class Issue1912CustomPropertyDeserializer extends ValueDeserializer @@ -247,8 +260,9 @@ static class ArrayDeserializerModifier extends ValueDeserializerModifier { public ValueDeserializer modifyArrayDeserializer(DeserializationConfig config, ArrayType valueType, BeanDescription beanDesc, ValueDeserializer deserializer) { return (ValueDeserializer) new StdDeserializer(Object.class) { - @Override public Object deserialize(JsonParser jp, + @Override public Object deserialize(JsonParser p, DeserializationContext ctxt) { + p.skipChildren(); return new String[] { "foo" }; } }; @@ -260,8 +274,9 @@ static class CollectionDeserializerModifier extends ValueDeserializerModifier { public ValueDeserializer modifyCollectionDeserializer(DeserializationConfig config, CollectionType valueType, BeanDescription beanDesc, ValueDeserializer deserializer) { return (ValueDeserializer) new StdDeserializer(Object.class) { - @Override public Object deserialize(JsonParser jp, + @Override public Object deserialize(JsonParser p, DeserializationContext ctxt) { + p.skipChildren(); ArrayList list = new ArrayList(); list.add("foo"); return list; @@ -275,8 +290,9 @@ static class MapDeserializerModifier extends ValueDeserializerModifier { public ValueDeserializer modifyMapDeserializer(DeserializationConfig config, MapType valueType, BeanDescription beanDesc, ValueDeserializer deserializer) { return (ValueDeserializer) new StdDeserializer(Object.class) { - @Override public Object deserialize(JsonParser jp, + @Override public Object deserialize(JsonParser p, DeserializationContext ctxt) { + p.skipChildren(); HashMap map = new HashMap(); map.put("a", "foo"); return map; diff --git a/src/test/java/tools/jackson/databind/deser/builder/BuilderSimpleTest.java b/src/test/java/tools/jackson/databind/deser/builder/BuilderSimpleTest.java index 73c309dfac..f05e1464fc 100644 --- a/src/test/java/tools/jackson/databind/deser/builder/BuilderSimpleTest.java +++ b/src/test/java/tools/jackson/databind/deser/builder/BuilderSimpleTest.java @@ -409,7 +409,7 @@ public void testBuilderMethodReturnMoreSpecific() throws Exception @Test public void testSelfBuilder777() throws Exception { - SelfBuilder777 result = MAPPER.readValue(a2q("{'x':3}'"), + SelfBuilder777 result = MAPPER.readValue(a2q("{'x':3}"), SelfBuilder777.class); assertNotNull(result); assertEquals(3, result.x); diff --git a/src/test/java/tools/jackson/databind/deser/enums/EnumDeserializationTest.java b/src/test/java/tools/jackson/databind/deser/enums/EnumDeserializationTest.java index aae60bf098..5bd8d520c6 100644 --- a/src/test/java/tools/jackson/databind/deser/enums/EnumDeserializationTest.java +++ b/src/test/java/tools/jackson/databind/deser/enums/EnumDeserializationTest.java @@ -283,11 +283,16 @@ public void testSimple() throws Exception { // First "good" case with Strings String JSON = "\"OK\" \"RULES\" null"; - // multiple main-level mappings, need explicit parser: - JsonParser p = MAPPER.createParser(JSON); + // multiple main-level mappings, need explicit parser + // (and possibly prevent validation of trailing tokens) + ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .build(); + + JsonParser p = mapper.createParser(JSON); - assertEquals(TestEnum.OK, MAPPER.readValue(p, TestEnum.class)); - assertEquals(TestEnum.RULES, MAPPER.readValue(p, TestEnum.class)); + assertEquals(TestEnum.OK, mapper.readValue(p, TestEnum.class)); + assertEquals(TestEnum.RULES, mapper.readValue(p, TestEnum.class)); // should be ok; nulls are typeless; handled by mapper, not by deserializer assertNull(MAPPER.readValue(p, TestEnum.class)); @@ -296,11 +301,11 @@ public void testSimple() throws Exception assertFalse(p.hasCurrentToken()); // Then alternative with index (0 means first entry) - assertEquals(TestEnum.JACKSON, MAPPER.readValue(" 0 ", TestEnum.class)); + assertEquals(TestEnum.JACKSON, mapper.readValue(" 0 ", TestEnum.class)); // Then error case: unrecognized value try { - /*Object result =*/ MAPPER.readValue("\"NO-SUCH-VALUE\"", TestEnum.class); + /*Object result =*/ mapper.readValue("\"NO-SUCH-VALUE\"", TestEnum.class); fail("Expected an exception for bogus enum value..."); } catch (MismatchedInputException jex) { verifyException(jex, "not one of the values accepted for Enum class"); @@ -309,8 +314,8 @@ public void testSimple() throws Exception } /** - * Enums are considered complex if they have code (and hence sub-classes)... an - * example is TimeUnit + * Enums are considered complex if they have code (and hence sub-classes)... + * an example is TimeUnit */ @Test public void testComplexEnum() throws Exception diff --git a/src/test/java/tools/jackson/databind/deser/filter/ProblemHandlerTest.java b/src/test/java/tools/jackson/databind/deser/filter/ProblemHandlerTest.java index f97c95631c..e643547254 100644 --- a/src/test/java/tools/jackson/databind/deser/filter/ProblemHandlerTest.java +++ b/src/test/java/tools/jackson/databind/deser/filter/ProblemHandlerTest.java @@ -137,6 +137,7 @@ public Object handleUnexpectedToken(DeserializationContext ctxt, JavaType targetType, JsonToken t, JsonParser p, String failureMsg) { + p.skipChildren(); return value; } } @@ -368,6 +369,10 @@ public void testInstantiationExceptionHandling() throws Exception public void testMissingInstantiatorHandling() throws Exception { ObjectMapper mapper = jsonMapperBuilder() + // 14-Jan-2025, tatu: Need to disable trailing tokens (for 3.0) + // for this to work (handler not consuming all tokens as it should + // but no time to fully fix right now) + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) .addHandler(new MissingInstantiationHandler(new NoDefaultCtor(13))) .build(); NoDefaultCtor w = mapper.readValue("{ \"x\" : true }", NoDefaultCtor.class); diff --git a/src/test/java/tools/jackson/databind/deser/jdk/JDKScalarsDeserTest.java b/src/test/java/tools/jackson/databind/deser/jdk/JDKScalarsDeserTest.java index fbf419cc24..b5d0740978 100644 --- a/src/test/java/tools/jackson/databind/deser/jdk/JDKScalarsDeserTest.java +++ b/src/test/java/tools/jackson/databind/deser/jdk/JDKScalarsDeserTest.java @@ -14,16 +14,16 @@ import tools.jackson.databind.*; import tools.jackson.databind.exc.MismatchedInputException; import tools.jackson.databind.util.ClassUtil; +import tools.jackson.databind.testutil.DatabindTestUtil; import static org.junit.jupiter.api.Assertions.*; -import static tools.jackson.databind.testutil.DatabindTestUtil.*; - /** * Unit tests for verifying handling of simple basic non-structured * types; primitives (and/or their wrappers), Strings. */ public class JDKScalarsDeserTest + extends DatabindTestUtil { private final static String NAN_STRING = "NaN"; @@ -548,7 +548,7 @@ public void testBase64Variants() throws Exception /** * Then a unit test to verify that we can conveniently bind sequence of - * space-separate simple values + * space-separated simple values */ @Test public void testSequenceOfInts() throws Exception @@ -560,12 +560,15 @@ public void testSequenceOfInts() throws Exception sb.append(" "); sb.append(i); } - JsonParser p = MAPPER.createParser(sb.toString()); - for (int i = 0; i < NR_OF_INTS; ++i) { - Integer result = MAPPER.readValue(p, Integer.class); - assertEquals(Integer.valueOf(i), result); + ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .build(); + try (JsonParser p = mapper.createParser(sb.toString())) { + for (int i = 0; i < NR_OF_INTS; ++i) { + Integer result = mapper.readValue(p, Integer.class); + assertEquals(Integer.valueOf(i), result); + } } - p.close(); } /* diff --git a/src/test/java/tools/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java b/src/test/java/tools/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java index 366bea7cfe..d91e2854ac 100644 --- a/src/test/java/tools/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java +++ b/src/test/java/tools/jackson/databind/deser/jdk/JDKStringLikeTypeDeserTest.java @@ -416,12 +416,14 @@ public void testUUIDAux() throws Exception byte[] data = bytes.toByteArray(); assertEquals(16, data.length); - buf.writePOJO(data); - - UUID value2 = MAPPER.readValue(buf.asParser(ObjectReadContext.empty()), UUID.class); - - assertEquals(value, value2); - buf.close(); + // Let's create fresh TokenBuffer, not reuse one + try (TokenBuffer buf2 = TokenBuffer.forGeneration()) { + buf2.writePOJO(data); + + UUID value2 = MAPPER.readValue(buf2.asParser(), UUID.class); + + assertEquals(value, value2); + } } } } diff --git a/src/test/java/tools/jackson/databind/deser/jdk/LocaleDeserTest.java b/src/test/java/tools/jackson/databind/deser/jdk/LocaleDeserTest.java index 4540deb12b..57af2b1318 100644 --- a/src/test/java/tools/jackson/databind/deser/jdk/LocaleDeserTest.java +++ b/src/test/java/tools/jackson/databind/deser/jdk/LocaleDeserTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import tools.jackson.core.type.TypeReference; +import tools.jackson.databind.DeserializationFeature; import tools.jackson.databind.ObjectMapper; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -299,8 +300,9 @@ private void assertLocale(Locale expected, Locale actual) { @Test public void testLocaleFuzz47034() throws Exception { - Locale loc = MAPPER.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47034.json"), - Locale.class); + Locale loc = MAPPER.readerFor(Locale.class) + .without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47034.json")); assertNotNull(loc); } @@ -309,8 +311,9 @@ public void testLocaleFuzz47034() throws Exception @Test public void testLocaleFuzz47036() throws Exception { - Locale loc = MAPPER.readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47036.json"), - Locale.class); + Locale loc = MAPPER.readerFor(Locale.class) + .without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .readValue(getClass().getResourceAsStream("/fuzz/oss-fuzz-47036.json")); assertNotNull(loc); } } diff --git a/src/test/java/tools/jackson/databind/deser/validate/FullStreamReadTest.java b/src/test/java/tools/jackson/databind/deser/validate/FullStreamReadTest.java index ee7761bcee..bf20dabc68 100644 --- a/src/test/java/tools/jackson/databind/deser/validate/FullStreamReadTest.java +++ b/src/test/java/tools/jackson/databind/deser/validate/FullStreamReadTest.java @@ -9,16 +9,14 @@ import tools.jackson.databind.*; import tools.jackson.databind.exc.MismatchedInputException; import tools.jackson.databind.json.JsonMapper; +import tools.jackson.databind.testutil.DatabindTestUtil; import static org.junit.jupiter.api.Assertions.*; -import static tools.jackson.databind.testutil.DatabindTestUtil.newJsonMapper; -import static tools.jackson.databind.testutil.DatabindTestUtil.verifyException; - /** * Test for validating {@link tools.jackson.databind.DeserializationFeature#FAIL_ON_TRAILING_TOKENS}. */ -public class FullStreamReadTest +public class FullStreamReadTest extends DatabindTestUtil { private final static String JSON_OK_ARRAY = " [ 1, 2, 3] "; private final static String JSON_OK_ARRAY_WITH_COMMENT = JSON_OK_ARRAY + " // stuff "; @@ -40,27 +38,31 @@ public class FullStreamReadTest @Test public void testMapperAcceptTrailing() { - assertFalse(MAPPER.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)); + ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .build(); + + assertFalse(mapper.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)); // by default, should be ok to read, all - _verifyArray(MAPPER.readTree(JSON_OK_ARRAY)); - _verifyArray(MAPPER.readTree(JSON_OK_ARRAY_WITH_COMMENT)); - _verifyArray(MAPPER.readTree(JSON_FAIL_ARRAY)); + _verifyArray(mapper.readTree(JSON_OK_ARRAY)); + _verifyArray(mapper.readTree(JSON_OK_ARRAY_WITH_COMMENT)); + _verifyArray(mapper.readTree(JSON_FAIL_ARRAY)); // and also via "untyped" - _verifyCollection(MAPPER.readValue(JSON_OK_ARRAY, List.class)); - _verifyCollection(MAPPER.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class)); - _verifyCollection(MAPPER.readValue(JSON_FAIL_ARRAY, List.class)); + _verifyCollection(mapper.readValue(JSON_OK_ARRAY, List.class)); + _verifyCollection(mapper.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class)); + _verifyCollection(mapper.readValue(JSON_FAIL_ARRAY, List.class)); // ditto for getting `null` and some other token - assertTrue(MAPPER.readTree(JSON_OK_NULL).isNull()); - assertTrue(MAPPER.readTree(JSON_OK_NULL_WITH_COMMENT).isNull()); - assertTrue(MAPPER.readTree(JSON_FAIL_NULL).isNull()); + assertTrue(mapper.readTree(JSON_OK_NULL).isNull()); + assertTrue(mapper.readTree(JSON_OK_NULL_WITH_COMMENT).isNull()); + assertTrue(mapper.readTree(JSON_FAIL_NULL).isNull()); - assertNull(MAPPER.readValue(JSON_OK_NULL, Object.class)); - assertNull(MAPPER.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class)); - assertNull(MAPPER.readValue(JSON_FAIL_NULL, Object.class)); + assertNull(mapper.readValue(JSON_OK_NULL, Object.class)); + assertNull(mapper.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class)); + assertNull(mapper.readValue(JSON_FAIL_NULL, Object.class)); } @Test @@ -176,8 +178,8 @@ public void testMapperFailOnTrailingWithNull() @Test public void testReaderAcceptTrailing() { - ObjectReader R = MAPPER.reader(); - assertFalse(R.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)); + ObjectReader R = MAPPER.reader() + .without(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); _verifyArray(R.readTree(JSON_OK_ARRAY)); _verifyArray(R.readTree(JSON_OK_ARRAY_WITH_COMMENT)); diff --git a/src/test/java/tools/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java b/src/test/java/tools/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java index 03a5ecffd9..d81695e9bb 100644 --- a/src/test/java/tools/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java +++ b/src/test/java/tools/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java @@ -8,8 +8,7 @@ import com.fasterxml.jackson.annotation.*; -import tools.jackson.core.JsonGenerator; -import tools.jackson.core.JsonParser; +import tools.jackson.core.*; import tools.jackson.databind.*; import tools.jackson.databind.annotation.*; import tools.jackson.databind.cfg.MapperConfig; @@ -95,20 +94,22 @@ public void setEnumProperty(EnumExample enumProperty) public static class QNameSerializer extends ValueSerializer { @Override - public void serialize(QName value, JsonGenerator g, SerializationContext provider) + public void serialize(QName value, JsonGenerator g, SerializationContext ctxt) { g.writeString(value.toString()); } } - public static class QNameDeserializer extends StdDeserializer { public QNameDeserializer() { super(QName.class); } @Override public QName deserialize(JsonParser p, DeserializationContext ctxt) { - return QName.valueOf(p.readValueAs(String.class)); + if (!p.hasToken(JsonToken.VALUE_STRING)) { + throw new IllegalArgumentException("Unexpected token "+p.currentToken()); + } + return QName.valueOf(p.getText()); } } diff --git a/src/test/java/tools/jackson/databind/misc/TestBlocking.java b/src/test/java/tools/jackson/databind/misc/TestBlocking.java index 59afc2e7dd..5f896186b9 100644 --- a/src/test/java/tools/jackson/databind/misc/TestBlocking.java +++ b/src/test/java/tools/jackson/databind/misc/TestBlocking.java @@ -26,21 +26,24 @@ public class TestBlocking @Test public void testEagerAdvance() throws IOException { - ObjectMapper mapper = new ObjectMapper(); - JsonParser jp = createParserUsingReader("[ 1 "); - assertToken(JsonToken.START_ARRAY, jp.nextToken()); - assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken()); + ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .build(); + JsonParser p = createParserUsingReader("[ 1 "); + assertToken(JsonToken.START_ARRAY, p.nextToken()); + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // And then try to map just a single entry: shouldn't fail: - Integer I = mapper.readValue(jp, Integer.class); + Integer I = mapper.readValue(p, Integer.class); assertEquals(Integer.valueOf(1), I); // and should fail only now: try { - jp.nextToken(); + p.nextToken(); + fail("Should not pass"); } catch (JacksonException ioe) { verifyException(ioe, "Unexpected end-of-input: expected close marker for ARRAY"); } - jp.close(); + p.close(); } } diff --git a/src/test/java/tools/jackson/databind/module/SimpleModuleTest.java b/src/test/java/tools/jackson/databind/module/SimpleModuleTest.java index 158e5d2786..a30375cfe3 100644 --- a/src/test/java/tools/jackson/databind/module/SimpleModuleTest.java +++ b/src/test/java/tools/jackson/databind/module/SimpleModuleTest.java @@ -168,6 +168,7 @@ static class Test3787Bean { static class Deserializer3787A extends ValueDeserializer { @Override public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) { + p.skipChildren(); // important to consume value Test3787Bean simpleTestBean = new Test3787Bean(); simpleTestBean.value = "I am A"; return simpleTestBean; @@ -177,6 +178,7 @@ public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) { static class Deserializer3787B extends ValueDeserializer { @Override public Test3787Bean deserialize(JsonParser p, DeserializationContext ctxt) { + p.skipChildren(); // important to consume value Test3787Bean simpleTestBean = new Test3787Bean(); simpleTestBean.value = "I am B"; return simpleTestBean; diff --git a/src/test/java/tools/jackson/databind/node/JsonNodeConversionsTest.java b/src/test/java/tools/jackson/databind/node/JsonNodeConversionsTest.java index 50d1228ddf..53e94f848c 100644 --- a/src/test/java/tools/jackson/databind/node/JsonNodeConversionsTest.java +++ b/src/test/java/tools/jackson/databind/node/JsonNodeConversionsTest.java @@ -161,7 +161,7 @@ public static class LeafDeserializer extends ValueDeserializer @Override public Leaf deserialize(JsonParser p, DeserializationContext ctxt) { - JsonNode tree = (JsonNode) p.readValueAsTree(); + JsonNode tree = ctxt.readTree(p); Leaf leaf = new Leaf(); leaf.value = tree.get("value").intValue(); return leaf; diff --git a/src/test/java/tools/jackson/databind/node/TreeReadViaMapperTest.java b/src/test/java/tools/jackson/databind/node/TreeReadViaMapperTest.java index 9ba55244fd..fdb909c35d 100644 --- a/src/test/java/tools/jackson/databind/node/TreeReadViaMapperTest.java +++ b/src/test/java/tools/jackson/databind/node/TreeReadViaMapperTest.java @@ -146,7 +146,9 @@ public void testNullViaParser() throws Exception @Test public void testMultiple() throws Exception { - final ObjectMapper mapper = objectMapper(); + ObjectMapper mapper = jsonMapperBuilder() + .disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) + .build(); String JSON = "12 \"string\" [ 1, 2, 3 ]"; JsonParser p = mapper.createParser(new StringReader(JSON)); JsonNode result = mapper.readTree(p); diff --git a/src/test/java/tools/jackson/databind/objectid/JSOGDeserialize622Test.java b/src/test/java/tools/jackson/databind/objectid/JSOGDeserialize622Test.java index 8453e456d0..ac5c3cbed3 100644 --- a/src/test/java/tools/jackson/databind/objectid/JSOGDeserialize622Test.java +++ b/src/test/java/tools/jackson/databind/objectid/JSOGDeserialize622Test.java @@ -87,15 +87,15 @@ public JSOGRef generateId(Object forPojo) { static class JSOGRefDeserializer extends ValueDeserializer { @Override - public JSOGRef deserialize(JsonParser p, DeserializationContext ctx) + public JSOGRef deserialize(JsonParser p, DeserializationContext ctxt) { - JsonNode node = p.readValueAsTree(); + JsonNode node = ctxt.readTree(p); if (node.isString()) { return new JSOGRef(node.asInt()); } JsonNode n = node.get(REF_KEY); if (n == null) { - ctx.reportInputMismatch(JSOGRef.class, "Could not find key '"+REF_KEY + ctxt.reportInputMismatch(JSOGRef.class, "Could not find key '"+REF_KEY +"' from ("+node.getClass().getName()+"): "+node); } return new JSOGRef(n.asInt());