Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use Big Decimal for Tree #135

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class JacksonJrsTreeCodec extends TreeCodec

// @since 2.17
protected boolean _failOnDuplicateKeys;

protected boolean _useBigDecimalForDouble;

public JacksonJrsTreeCodec() {
this(null);
}
Expand All @@ -31,7 +32,11 @@ public JacksonJrsTreeCodec(ObjectCodec codec) {
public void setFailOnDuplicateKeys(boolean state) {
_failOnDuplicateKeys = state;
}


public void setUseBigDecimalForDouble(boolean state) {
_useBigDecimalForDouble = state;
}

@SuppressWarnings("unchecked")
@Override
public <T extends TreeNode> T readTree(JsonParser p) throws IOException {
Expand All @@ -50,6 +55,9 @@ private JrsValue nodeFrom(JsonParser p) throws IOException
return JrsBoolean.FALSE;
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NUMBER_FLOAT:
if (_useBigDecimalForDouble) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this has bug -- now all integer numbers become BigDecimals too. I'll fix it.

return new JrsNumber(p.getDecimalValue());
}
return new JrsNumber(p.getNumberValue());
case JsonTokenId.ID_STRING:
return new JrsString(p.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public JrSimpleTreeExtension(JacksonJrsTreeCodec tc) {
@Override
protected void register(ExtensionContext ctxt) {
_codec.setFailOnDuplicateKeys(ctxt.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS));
_codec.setUseBigDecimalForDouble(ctxt.isEnabled(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS));
ctxt.setTreeCodec(_codec);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.fasterxml.jackson.jr.stree.failing;

import java.math.BigDecimal;
package com.fasterxml.jackson.jr.stree;

import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.jr.ob.JSON;

import com.fasterxml.jackson.jr.ob.*;
import com.fasterxml.jackson.jr.stree.*;
import java.math.BigDecimal;

public class ReadAsBigDecimal90Test extends JacksonJrTreeTestBase
{
// [jackson-jr#90]
public void testReadAsBigDecimal() throws Exception
{
JSON json = JSON.builder()
.treeCodec(new JacksonJrsTreeCodec())
.enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.register(new JrSimpleTreeExtension())
.build();

String input = "[1.1]";
Expand All @@ -27,4 +25,21 @@ public void testReadAsBigDecimal() throws Exception
assertEquals(BigDecimal.class,
((JrsNumber) elemNode).getValue().getClass());
}

public void testDefaultBehaviourWithBigDecimalFlag() throws Exception
{
JSON json = JSON.builder()
.register(new JrSimpleTreeExtension())
.build();

String input = "[1.1]";

TreeNode node = json.treeFrom(input);
TreeNode elemNode = node.get(0);

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