Skip to content

Commit

Permalink
Fix: Use Big Decimal for JacksonJrsTreeCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Mar 17, 2024
1 parent 0833785 commit f3e98ff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSONObjectException;

/**
Expand All @@ -13,11 +14,25 @@
*/
public class JacksonJrsTreeCodec extends TreeCodec
{
public static JrsMissing MISSING = JrsMissing.instance;
public static final JrsMissing MISSING = JrsMissing.instance;
protected final ObjectCodec _objectCodec;

// @since 2.17
protected boolean _failOnDuplicateKeys;
protected boolean _useBigDecimalForFloat;

public JacksonJrsTreeCodec withFeature(JSON.Feature... features) {
//currently only 2 features but can be added as needed
for (JSON.Feature feature : features) {
if(feature == JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS) {
_failOnDuplicateKeys = true;
}
if(feature == JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS) {
_useBigDecimalForFloat = true;
}
}
return this;
}

public JacksonJrsTreeCodec() {
this(null);
Expand Down Expand Up @@ -50,6 +65,16 @@ private JrsValue nodeFrom(JsonParser p) throws IOException
return JrsBoolean.FALSE;
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NUMBER_FLOAT:
final JsonParser.NumberType n = p.getNumberType();
if (n == JsonParser.NumberType.FLOAT) {
return new JrsNumber(p.getFloatValue());
}
if (_useBigDecimalForFloat && n == JsonParser.NumberType.DOUBLE) {
return new JrsNumber(p.getDecimalValue());
}
if (n == JsonParser.NumberType.DOUBLE) {
return new JrsNumber(p.getDoubleValue());
}
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 @@ -13,8 +13,7 @@ public class ReadAsBigDecimal90Test extends JacksonJrTreeTestBase
public void testReadAsBigDecimal() throws Exception
{
JSON json = JSON.builder()
.treeCodec(new JacksonJrsTreeCodec())
.enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.treeCodec(new JacksonJrsTreeCodec().withFeature(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS))
.build();

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

public void testReadAsBigDecimal2() throws Exception
{
JSON json = JSON.builder()
.treeCodec(new JacksonJrsTreeCodec())
.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 f3e98ff

Please sign in to comment.