Skip to content

Commit

Permalink
Fix: Use Big Decimal for Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Mar 16, 2024
1 parent 0833785 commit 023fd94
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/
public class JacksonJrsTreeCodec extends TreeCodec
{
public static JrsMissing MISSING = JrsMissing.instance;
protected final ObjectCodec _objectCodec;

// @since 2.17
protected boolean _failOnDuplicateKeys;

protected boolean _useBigDouble;

public JacksonJrsTreeCodec() {
this(null);
}
Expand All @@ -28,10 +28,11 @@ public JacksonJrsTreeCodec(ObjectCodec codec) {
}

// @since 2.17
public void setFailOnDuplicateKeys(boolean state) {
_failOnDuplicateKeys = state;
public void setFailOnDuplicateKeys(boolean failOnDuplicateKeys,boolean useBigDouble) {
_failOnDuplicateKeys = failOnDuplicateKeys;
_useBigDouble = useBigDouble;
}

@SuppressWarnings("unchecked")
@Override
public <T extends TreeNode> T readTree(JsonParser p) throws IOException {
Expand All @@ -50,7 +51,16 @@ private JrsValue nodeFrom(JsonParser p) throws IOException
return JrsBoolean.FALSE;
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NUMBER_FLOAT:
return new JrsNumber(p.getNumberValue());
if (_useBigDouble) {
JsonParser.NumberType n = p.getNumberType();
if (n == JsonParser.NumberType.FLOAT) {
return new JrsNumber(p.getFloatValue());
}
if (n == JsonParser.NumberType.DOUBLE) {
return new JrsNumber(p.getDoubleValue());
}
}
return new JrsNumber(p.getDecimalValue());
case JsonTokenId.ID_STRING:
return new JrsString(p.getText());
case JsonTokenId.ID_START_ARRAY: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public JrSimpleTreeExtension(JacksonJrsTreeCodec tc) {

@Override
protected void register(ExtensionContext ctxt) {
_codec.setFailOnDuplicateKeys(ctxt.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS));
_codec.setFailOnDuplicateKeys(ctxt.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS), ctxt.isEnabled(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS));
ctxt.setTreeCodec(_codec);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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
{
Expand Down

0 comments on commit 023fd94

Please sign in to comment.