Skip to content

Commit

Permalink
Backport #2220 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 26, 2019
1 parent 21314d4 commit e3f005b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Project: jackson-databind
#2211: Change of behavior (2.8 -> 2.9) with `ObjectMapper.readTree(input)` with no content
#2217: Suboptimal memory allocation in `TextNode.getBinaryValue()`
(reported by Christoph B)
#2220: Force serialization always for `convertValue()`; avoid short-cuts
#2223: Add `missingNode()` method in `JsonNodeFactory`
#2227: Minor cleanup of exception message for `Enum` binding failure
(reported by RightHandedMonkey@github)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public enum DeserializationFeature implements ConfigFeature
* or {@link java.util.Collection} context) is available.
* If enabled such values will be deserialized as {@link java.math.BigDecimal}s;
* if disabled, will be deserialized as {@link Double}s.
* <p>
*<p>
* NOTE: one aspect of {@link java.math.BigDecimal} handling that may need
* configuring is whether trailing zeroes are trimmed:
* {@link com.fasterxml.jackson.databind.node.JsonNodeFactory} has
* {@link com.fasterxml.jackson.databind.node.JsonNodeFactory#withExactBigDecimals} for
* changing default behavior (default is for trailing zeroes to be trimmed).
*<p>
* Feature is disabled by default, meaning that "untyped" floating
* point numbers will by default be deserialized as {@link Double}s
* (choice is for performance reason -- BigDecimals are slower than
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2688,9 +2688,10 @@ public <T> T treeToValue(TreeNode n, Class<T> valueType)
throws JsonProcessingException
{
try {
// 25-Jan-2019, tatu: [databind#2220] won't prevent existing coercions here
// Simple cast when we just want to cast to, say, ObjectNode
// ... one caveat; while everything is Object.class, let's not take shortcut
if (valueType != Object.class && valueType.isAssignableFrom(n.getClass())) {
if (TreeNode.class.isAssignableFrom(valueType)
&& valueType.isAssignableFrom(n.getClass())) {
return (T) n;
}
// 20-Apr-2016, tatu: Another thing: for VALUE_EMBEDDED_OBJECT, assume similar
Expand Down Expand Up @@ -3648,20 +3649,7 @@ public <T> T convertValue(Object fromValue, JavaType toValueType)
protected Object _convert(Object fromValue, JavaType toValueType)
throws IllegalArgumentException
{
// [databind#1433] Do not shortcut null values.
// This defaults primitives and fires deserializer getNullValue hooks.
if (fromValue != null) {
// also, as per [databind#11], consider case for simple cast
// But with caveats: one is that while everything is Object.class, we don't
// want to "optimize" that out; and the other is that we also do not want
// to lose conversions of generic types.
Class<?> targetType = toValueType.getRawClass();
if (targetType != Object.class
&& !toValueType.hasGenericTypes()
&& targetType.isAssignableFrom(fromValue.getClass())) {
return fromValue;
}
}
// 25-Jan-2019, tatu: [databind#2220] Let's NOT try to short-circuit anything

// Then use TokenBuffer, which is a JsonGenerator:
TokenBuffer buf = new TokenBuffer(this, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,10 @@ private void _convertAndVerifyPoint(ObjectMapper m)
*/
public void testIssue11() throws Exception
{
// First the expected use case, Node specification
ObjectNode root = MAPPER.createObjectNode();
JsonNode n = root;
ObjectNode ob2 = MAPPER.convertValue(n, ObjectNode.class);
assertSame(root, ob2);

JsonNode n2 = MAPPER.convertValue(n, JsonNode.class);
assertSame(root, n2);

// then some other no-op conversions
String STR = "test";
CharSequence seq = MAPPER.convertValue(STR, CharSequence.class);
assertSame(STR, seq);
StringBuilder SB = new StringBuilder("test");
CharSequence seq = MAPPER.convertValue(SB, CharSequence.class);
assertNotSame(SB, seq);

// and then something that should NOT use short-cut
Leaf l = new Leaf(13);
Expand Down

0 comments on commit e3f005b

Please sign in to comment.