Skip to content

Commit

Permalink
Minor fix for #2215 implementation, related to #2978
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 10, 2020
1 parent 9dab814 commit 866b2b4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ public Object createFromLong(DeserializationContext ctxt, long value) throws IOE
public Object createFromBigInteger(DeserializationContext ctxt, BigInteger value) throws IOException
{
return ctxt.handleMissingInstantiator(getValueClass(),this,null,
"no BigInteger-argument constructor/factory method to deserialize from Number value (%s)",
value
"no BigInteger-argument constructor/factory method to deserialize from Number value (%s)",
value
);
}

Expand All @@ -349,8 +349,8 @@ public Object createFromDouble(DeserializationContext ctxt, double value) throws
public Object createFromBigDecimal(DeserializationContext ctxt, BigDecimal value) throws IOException
{
return ctxt.handleMissingInstantiator(getValueClass(),this,null,
"no BigDecimal/double/Double-argument constructor/factory method to deserialize from Number value (%s)",
value
"no BigDecimal/double/Double-argument constructor/factory method to deserialize from Number value (%s)",
value
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public Object createFromInt(DeserializationContext ctxt, int value) throws IOExc
return _fromBigIntegerCreator.call1(arg);
} catch (Throwable t0) {
return ctxt.handleInstantiationProblem(_fromBigIntegerCreator.getDeclaringClass(),
arg, rewrapCtorProblem(ctxt, t0)
arg, rewrapCtorProblem(ctxt, t0)
);
}
}
Expand All @@ -380,24 +380,23 @@ arg, rewrapCtorProblem(ctxt, t0)
public Object createFromLong(DeserializationContext ctxt, long value) throws IOException
{
if (_fromLongCreator != null) {
Object arg = Long.valueOf(value);
Long arg = Long.valueOf(value);
try {
return _fromLongCreator.call1(arg);
} catch (Throwable t0) {
return ctxt.handleInstantiationProblem(_fromLongCreator.getDeclaringClass(),
arg,
rewrapCtorProblem(ctxt, t0)
arg, rewrapCtorProblem(ctxt, t0)
);
}
}

if (_fromBigIntegerCreator != null) {
Object arg = BigInteger.valueOf(value);
BigInteger arg = BigInteger.valueOf(value);
try {
return _fromBigIntegerCreator.call1(arg);
} catch (Throwable t0) {
return ctxt.handleInstantiationProblem(_fromBigIntegerCreator.getDeclaringClass(),
arg, rewrapCtorProblem(ctxt, t0)
arg, rewrapCtorProblem(ctxt, t0)
);
}
}
Expand All @@ -408,12 +407,12 @@ arg, rewrapCtorProblem(ctxt, t0)
@Override
public Object createFromBigInteger(DeserializationContext ctxt, BigInteger value) throws IOException
{
if (_fromBigDecimalCreator != null) {
if (_fromBigIntegerCreator != null) {
try {
return _fromBigIntegerCreator.call1(value);
} catch (Throwable t) {
return ctxt.handleInstantiationProblem(_fromBigIntegerCreator.getDeclaringClass(),
value, rewrapCtorProblem(ctxt, t)
value, rewrapCtorProblem(ctxt, t)
);
}
}
Expand All @@ -425,22 +424,22 @@ value, rewrapCtorProblem(ctxt, t)
public Object createFromDouble(DeserializationContext ctxt, double value) throws IOException
{
if(_fromDoubleCreator != null) {
Object arg = Double.valueOf(value);
Double arg = Double.valueOf(value);
try {
return _fromDoubleCreator.call1(arg);
} catch (Throwable t0) {
return ctxt.handleInstantiationProblem(_fromDoubleCreator.getDeclaringClass(),
arg, rewrapCtorProblem(ctxt, t0));
arg, rewrapCtorProblem(ctxt, t0));
}
}

if (_fromBigDecimalCreator != null) {
Object arg = BigDecimal.valueOf(value);
BigDecimal arg = BigDecimal.valueOf(value);
try {
return _fromBigDecimalCreator.call1(arg);
} catch (Throwable t0) {
return ctxt.handleInstantiationProblem(_fromBigDecimalCreator.getDeclaringClass(),
arg, rewrapCtorProblem(ctxt, t0));
arg, rewrapCtorProblem(ctxt, t0));
}
}

Expand All @@ -455,7 +454,7 @@ public Object createFromBigDecimal(DeserializationContext ctxt, BigDecimal value
return _fromBigDecimalCreator.call1(value);
} catch (Throwable t) {
return ctxt.handleInstantiationProblem(_fromBigDecimalCreator.getDeclaringClass(),
value, rewrapCtorProblem(ctxt, t)
value, rewrapCtorProblem(ctxt, t)
);
}
}
Expand Down
18 changes: 0 additions & 18 deletions src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.fasterxml.jackson.databind;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -59,29 +57,13 @@ public LongWrapper() { }
public LongWrapper(long value) { l = value; }
}

protected static class BigIntegerWrapper {
public BigInteger i;

public BigIntegerWrapper() { }

public BigIntegerWrapper(final BigInteger value) { i = value; }
}

protected static class DoubleWrapper {
public double d;

public DoubleWrapper() { }
public DoubleWrapper(double value) { d = value; }
}

protected static class BigDecimalWrapper {
public BigDecimal d;

public BigDecimalWrapper() { }

public BigDecimalWrapper(final BigDecimal value) { d = value; }
}

/**
* Simple wrapper around String type, usually to test value
* conversions or wrapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import java.util.*;

import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.util.TokenBuffer;

/**
* Unit tests for verifying that it is possible to annotate
Expand Down Expand Up @@ -172,7 +173,7 @@ static class NoArgFactoryBean {
public static NoArgFactoryBean create() { return new NoArgFactoryBean(123); }
}

// [Issue#208]
// [databind#208]
static class FromStringBean {
protected String value;

Expand All @@ -185,7 +186,25 @@ public static FromStringBean fromString(String s) {
return new FromStringBean(s, false);
}
}


// [databind#2215]
protected static class BigIntegerWrapper {
BigInteger _value;

public BigIntegerWrapper() { }

public BigIntegerWrapper(final BigInteger value) { _value = value; }
}

// [databind#2215]
protected static class BigDecimalWrapper {
BigDecimal _value;

public BigDecimalWrapper() { }

public BigDecimalWrapper(final BigDecimal value) { _value = value; }
}

/*
/**********************************************************
/* Annotated helper classes, mixed (creator and props)
Expand Down Expand Up @@ -333,14 +352,28 @@ public void testSimpleBooleanConstructor() throws Exception

public void testSimpleBigIntegerConstructor() throws Exception
{
final BigIntegerWrapper result = MAPPER.readValue("17", BigIntegerWrapper.class);
assertEquals(new BigInteger("17"), result.i);
// 10-Dec-2020, tatu: Small (magnitude) values will NOT trigger path
// we want; must use something outside of Long range...

BigInteger INPUT = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN);
final BigIntegerWrapper result = MAPPER.readValue(INPUT.toString(), BigIntegerWrapper.class);
assertEquals(INPUT, result._value);
}

public void testSimpleBigDecimalConstructor() throws Exception
{
final BigDecimalWrapper result = MAPPER.readValue("42.5", BigDecimalWrapper.class);
assertEquals(new BigDecimal("42.5"), result.d);
// 10-Dec-2020, tatu: not sure we can ever trigger this with JSON;
// but should be possible to handle via TokenBuffer?

BigDecimal INPUT = new BigDecimal("42.5");
try (TokenBuffer buf = new TokenBuffer(null, false)) {
buf.writeNumber(INPUT);
try (JsonParser p = buf.asParser()) {
final BigDecimalWrapper result = MAPPER.readValue(p,
BigDecimalWrapper.class);
assertEquals(INPUT, result._value);
}
}
}

public void testSimpleFactory() throws Exception
Expand Down

0 comments on commit 866b2b4

Please sign in to comment.