Skip to content

Commit

Permalink
Fix a minor flaw in #90 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 24, 2024
1 parent 961480c commit 9211fb2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private JrsValue nodeFrom(JsonParser p) throws IOException
case JsonTokenId.ID_FALSE:
return JrsBoolean.FALSE;
case JsonTokenId.ID_NUMBER_INT:
// Important! No coercion to BigDecimal (wrt [jackson-jr#90]
return new JrsNumber(p.getNumberValue());
case JsonTokenId.ID_NUMBER_FLOAT:
if (_useBigDecimalForDouble) {
return new JrsNumber(p.getDecimalValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ public void testDefaultBehaviourReadAsDouble() throws Exception
.disable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.register(new JrSimpleTreeExtension())
.build();

String input = "[1.1]";

TreeNode node = json.treeFrom(input);
TreeNode node = json.treeFrom("[1.1]");
TreeNode elemNode = node.get(0);

assertTrue(elemNode.isValueNode());
assertTrue(elemNode instanceof JrsNumber);
assertEquals(Double.class,
((JrsNumber) elemNode).getValue().getClass());

_verifyInt(json);
}

// [jackson-jr#90]
Expand All @@ -34,15 +33,26 @@ public void testReadAsBigDecimal() throws Exception
.enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.register(new JrSimpleTreeExtension())
.build();
TreeNode node = json.treeFrom("[1.1]");
TreeNode elemNode = node.get(0);

assertTrue(elemNode.isValueNode());
assertTrue(elemNode instanceof JrsNumber);
assertEquals(BigDecimal.class,
((JrsNumber) elemNode).getValue().getClass());

String input = "[1.1]";
_verifyInt(json);
}

TreeNode node = json.treeFrom(input);
private void _verifyInt(JSON json) throws Exception
{
TreeNode node = json.treeFrom("[123]");
TreeNode elemNode = node.get(0);

assertTrue(elemNode.isValueNode());
assertTrue(elemNode instanceof JrsNumber);
assertEquals(BigDecimal.class,
assertEquals(Integer.class,
((JrsNumber) elemNode).getValue().getClass());
assertEquals(123, ((JrsNumber) elemNode).getValue().intValue());
}
}

0 comments on commit 9211fb2

Please sign in to comment.