Skip to content

Commit

Permalink
Fix: Using JrSimpleTreeExtension to Provide Feature Context
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Mar 22, 2024
1 parent 0833785 commit 44c904c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
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) {
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());
}
}

0 comments on commit 44c904c

Please sign in to comment.