Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 24, 2024
2 parents 1c2d16d + 8dd6927 commit eb5207f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@
*/
public class JacksonJrsTreeCodec implements TreeCodec
{
public static JrsMissing MISSING = JrsMissing.instance;
public static final JrsMissing MISSING = JrsMissing.instance;

protected boolean _failOnDuplicateKeys;

protected boolean _useBigDecimalForDouble;

public JacksonJrsTreeCodec() { }

public void setFailOnDuplicateKeys(boolean state) {
_failOnDuplicateKeys = state;
}


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

@SuppressWarnings("unchecked")
@Override
public JrsValue readTree(JsonParser p) throws JacksonException {
Expand All @@ -40,6 +47,9 @@ private JrsValue nodeFrom(JsonParser p) throws JacksonException
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 @@ -26,6 +26,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 tools.jackson.jr.stree;

import tools.jackson.core.TreeNode;
import tools.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 @@ -15,6 +15,12 @@ Modules:

No changes since 2.17

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 eb5207f

Please sign in to comment.