Skip to content

Commit 52ee6f0

Browse files
committed
Last refactoring wrt DeserializationContext.extractScalarFromObject(), tests
1 parent 3d256f1 commit 52ee6f0

File tree

7 files changed

+57
-12
lines changed

7 files changed

+57
-12
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,20 +854,24 @@ public Calendar constructCalendar(Date d) {
854854
* "simple" unstructured value type.
855855
*
856856
* @param p Actual parser to read content from
857-
* @param ctxt Deserialization context
858857
* @param deser Deserializer that needs extracted String value
858+
* @param scalarType Immediate type of scalar to extract; usually type deserializer
859+
* handles but not always (for example, deserializer for {@code int[]} would pass
860+
* scalar type of {@code int})
859861
*
860862
* @return String value found; not {@code null} (exception should be thrown if no suitable
861863
* value found)
862864
*
863865
* @throws IOException If there are problems either reading content (underlying parser
864866
* problem) or finding expected scalar value
865867
*/
866-
public String extractScalarFromObject(JsonParser p, DeserializationContext ctxt,
867-
JsonDeserializer<?> deser)
868+
public String extractScalarFromObject(JsonParser p, JsonDeserializer<?> deser,
869+
Class<?> scalarType)
868870
throws IOException
869871
{
870-
return null;
872+
return reportInputMismatch(scalarType, String.format(
873+
"Cannot deserialize value of type %s from %s (token `JsonToken.START_OBJECT`)",
874+
ClassUtil.getClassDescription(scalarType), _shapeForToken(JsonToken.START_OBJECT)));
871875
}
872876

873877
/*
@@ -1350,7 +1354,7 @@ public Object handleUnexpectedToken(JavaType targetType, JsonToken t,
13501354
}
13511355
reportBadDefinition(targetType, String.format(
13521356
"DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s",
1353-
ClassUtil.getClassDescription(targetType),
1357+
ClassUtil.getTypeDescription(targetType),
13541358
ClassUtil.classNameOf(instance)
13551359
));
13561360
}

src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt)
535535
{
536536
String text;
537537
switch (p.currentTokenId()) {
538-
case JsonTokenId.ID_STRING: // let's do implicit re-parse
538+
case JsonTokenId.ID_STRING:
539539
text = p.getText();
540540
break;
541541
case JsonTokenId.ID_NUMBER_FLOAT: // coercing may work too

src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ protected final int _parseIntPrimitive(JsonParser p, DeserializationContext ctxt
674674
case JsonTokenId.ID_NULL:
675675
_verifyNullForPrimitive(ctxt);
676676
return 0;
677+
678+
// 29-Jun-2020, tatu: New! "Scalar from Object" to support tricky case of
679+
// XML element with attributes
680+
case JsonTokenId.ID_START_OBJECT:
681+
text = ctxt.extractScalarFromObject(p, this, Integer.TYPE);
682+
break;
677683
case JsonTokenId.ID_START_ARRAY:
678684
if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
679685
p.nextToken();
@@ -683,7 +689,6 @@ protected final int _parseIntPrimitive(JsonParser p, DeserializationContext ctxt
683689
}
684690
// fall through to fail
685691
default:
686-
// Otherwise, no can do:
687692
return ((Number) ctxt.handleUnexpectedToken(Integer.TYPE, p)).intValue();
688693
}
689694

src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ protected static String aposToQuotes(String json) {
359359
return json.replace("'", "\"");
360360
}
361361

362+
protected static String a2q(String json) {
363+
return json.replace("'", "\"");
364+
}
365+
362366
protected static String quotesToApos(String json) {
363367
return json.replace("\"", "'");
364368
}

src/test/java/com/fasterxml/jackson/databind/deser/TestArrayDeserialization.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/jdk/ArrayDeserializationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.databind.deser;
1+
package com.fasterxml.jackson.databind.deser.jdk;
22

33
import java.io.*;
44
import java.util.*;
@@ -16,7 +16,7 @@
1616
* This unit test suite tries to verify that the "Native" java type
1717
* mapper can properly re-construct Java array objects from Json arrays.
1818
*/
19-
public class TestArrayDeserialization
19+
public class ArrayDeserializationTest
2020
extends BaseMapTest
2121
{
2222
public final static class Bean1

src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.*;
44
import java.lang.reflect.Array;
5+
import java.math.BigDecimal;
6+
import java.math.BigInteger;
57
import java.util.List;
68

79
import org.junit.Assert;
@@ -12,6 +14,7 @@
1214
import com.fasterxml.jackson.databind.*;
1315
import com.fasterxml.jackson.databind.JsonMappingException.Reference;
1416
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
17+
import com.fasterxml.jackson.databind.util.ClassUtil;
1518

1619
/**
1720
* Unit tests for verifying handling of simple basic non-structured
@@ -787,9 +790,38 @@ private void _testInvalidStringCoercionFail(Class<?> cls, String targetTypeName)
787790

788791
try {
789792
MAPPER.readerFor(cls).readValue(JSON);
790-
fail("Should not pass");
793+
fail("Should MismatchedInputException pass");
791794
} catch (JsonMappingException e) {
792795
verifyException(e, "Cannot deserialize value of type `"+targetTypeName+"` from String \"foobar\"");
793796
}
794797
}
798+
799+
/*
800+
/**********************************************************
801+
/* Tests for mismatch: JSON Object for scalars (not supported
802+
/* for JSON
803+
/**********************************************************
804+
*/
805+
806+
public void testFailForScalarFromObject() throws Exception
807+
{
808+
_testFailForNumberFromObject(Byte.TYPE);
809+
_testFailForNumberFromObject(Short.TYPE);
810+
_testFailForNumberFromObject(Long.TYPE);
811+
_testFailForNumberFromObject(Float.TYPE);
812+
_testFailForNumberFromObject(Double.TYPE);
813+
_testFailForNumberFromObject(BigInteger.class);
814+
_testFailForNumberFromObject(BigDecimal.class);
815+
}
816+
817+
private void _testFailForNumberFromObject(Class<?> targetType) throws Exception
818+
{
819+
try {
820+
MAPPER.readValue(a2q("{'value':12}"), targetType);
821+
fail("Should not pass");
822+
} catch (MismatchedInputException e) {
823+
verifyException(e, "from Object value");
824+
verifyException(e, ClassUtil.getClassDescription(targetType));
825+
}
826+
}
795827
}

src/test/java/com/fasterxml/jackson/databind/deser/TestGenericMapDeser.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapWithGenericValuesDeserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.databind.deser;
1+
package com.fasterxml.jackson.databind.deser.jdk;
22

33
import java.util.*;
44

@@ -9,7 +9,7 @@
99
import com.fasterxml.jackson.databind.type.TypeFactory;
1010

1111
@SuppressWarnings("serial")
12-
public class TestGenericMapDeser
12+
public class MapWithGenericValuesDeserTest
1313
extends BaseMapTest
1414
{
1515
/*

0 commit comments

Comments
 (0)