Skip to content

Commit

Permalink
add int to double and long to double conversions to StdValueInstantia…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoten committed Mar 28, 2024
1 parent 199d3ac commit 23a635d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,18 @@ arg, rewrapCtorProblem(ctxt, t0)
);
}
}


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

return super.createFromInt(ctxt, value);
}

Expand Down Expand Up @@ -411,6 +422,17 @@ arg, rewrapCtorProblem(ctxt, t0)
);
}
}

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

return super.createFromLong(ctxt, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

Expand All @@ -23,4 +27,27 @@ public void testDoubleValidation_invalid() {
BigDecimal value = BigDecimal.valueOf(Double.MAX_VALUE).add(BigDecimal.valueOf(Double.MAX_VALUE));
assertNull(StdValueInstantiator.tryConvertToDouble(value));
}

@Test
public void testJsonIntToDouble() throws JsonMappingException, JsonProcessingException {
ObjectMapper m = new ObjectMapper();
Stuff a = m.readValue("5", Stuff.class);
assertEquals(5, a.value);
}

@Test
public void testJsonLongToDouble() throws JsonMappingException, JsonProcessingException {
ObjectMapper m = new ObjectMapper();
long x = 12345678901L;
Stuff a = m.readValue(x + "", Stuff.class);
assertEquals(x, a.value);
}

public static class Stuff {
public final double value;

public Stuff(double value) {
this.value = value;
}
}
}

0 comments on commit 23a635d

Please sign in to comment.