Skip to content

Commit

Permalink
Fix: Use Big Decimal for Tree (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Mar 24, 2024
1 parent 4a2e934 commit 961480c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
*/
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;


// @since 2.17.1
protected boolean _useBigDecimalForDouble;

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


// @since 2.17.1
public void setUseBigDecimalForDouble(boolean state) {
_useBigDecimalForDouble = state;
}

@SuppressWarnings("unchecked")
@Override
public <T extends TreeNode> T readTree(JsonParser p) throws IOException {
Expand All @@ -50,6 +59,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
@@ -0,0 +1,48 @@
package com.fasterxml.jackson.jr.stree;

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

import java.math.BigDecimal;

// [jackson-jr#90]: JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS should work
public class ReadAsBigDecimal90Test extends JacksonJrTreeTestBase
{
// [jackson-jr#90]
public void testDefaultBehaviourReadAsDouble() throws Exception
{
JSON json = JSON.builder()
.disable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.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());
}

// [jackson-jr#90]
public void testReadAsBigDecimal() throws Exception
{
JSON json = JSON.builder()
.enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
.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(BigDecimal.class,
((JrsNumber) elemNode).getValue().getClass());
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ Julian Honnen (@jhonnen)
(2.17.0)
* Contributed impl for #100: Add support for `java.time` (Java 8 date/time) types
(2.17.0)
* Contributed fix for #90: `USE_BIG_DECIMAL_FOR_FLOATS` feature not working
when using `JSON.treeFrom()`
(2.17.1)

6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.17.1 (not yet released)

#90: `USE_BIG_DECIMAL_FOR_FLOATS` feature not working when using `JSON.treeFrom()`
(reported by @jvdsandt)
(fix contributed by @Shounaks)

2.17.0 (12-Mar-2024)

#7: Support deserialization of `int[]`
Expand Down

0 comments on commit 961480c

Please sign in to comment.