Skip to content

Commit

Permalink
Add (failing) tests related to continuation of #15
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 15, 2017
1 parent 1dcb9e6 commit 858fcae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.OptionalInt;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

Expand All @@ -24,6 +25,11 @@ public OptionalInt getNullValue(DeserializationContext ctxt) {

@Override
public OptionalInt deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
// minor optimization, first, for common case
if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return OptionalInt.of(p.getValueAsInt());
}
// but aside of that try to use standard handling:
return OptionalInt.of(p.getValueAsInt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public void testOptionalIntCoerceFromString() throws Exception
{
OptionalInt opt = MAPPER.readValue(quote("123"), OptionalInt.class);
assertEquals(123, opt.getAsInt());
opt = MAPPER.readValue("\"\"", OptionalInt.class);
assertNotNull(opt);
assertFalse(opt.isPresent());

OptionalIntBean bean = MAPPER.readValue(aposToQuotes("{'value':null}"),
OptionalIntBean.class);
assertNotNull(bean.value);
Expand Down Expand Up @@ -105,6 +109,12 @@ public void testOptionalLongCoerceFromString() throws Exception
{
OptionalLong opt = MAPPER.readValue(quote("123"), OptionalLong.class);
assertEquals(123L, opt.getAsLong());

// should coerce from empty String too (by default)
opt = MAPPER.readValue("\"\"", OptionalLong.class);
assertNotNull(opt);
assertFalse(opt.isPresent());

OptionalLongBean bean = MAPPER.readValue(aposToQuotes("{'value':null}"),
OptionalLongBean.class);
assertNotNull(bean.value);
Expand Down Expand Up @@ -162,6 +172,12 @@ public void testOptionalDoubleCoerceFromString() throws Exception
{
OptionalDouble opt = MAPPER.readValue(quote("0.25"), OptionalDouble.class);
assertEquals(0.25, opt.getAsDouble());

// should coerce from empty String too (by default)
opt = MAPPER.readValue("\"\"", OptionalDouble.class);
assertNotNull(opt);
assertFalse(opt.isPresent());

OptionalDoubleBean bean = MAPPER.readValue(aposToQuotes("{'value':null}"),
OptionalDoubleBean.class);
assertNotNull(bean.value);
Expand Down

0 comments on commit 858fcae

Please sign in to comment.