Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USE_BIG_DECIMAL_FOR_FLOATS feature not working when using JSON.treeFrom() #90

Closed
jvdsandt opened this issue Nov 13, 2021 · 2 comments
Closed
Labels
Milestone

Comments

@jvdsandt
Copy link

This test fails because the USE_BIG_DECIMAL_FOR_FLOATS feature is ignored and Double objects are stiill used.

@Test
public void testParseDecimals() throws Exception {

    JSON json = JSON.builder()
            .treeCodec(new JacksonJrsTreeCodec())
            .enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
            .build();

    String input = "[1.1]";

    TreeNode node = json.treeFrom(input);
    TreeNode elemNode = node.get(0);

    assertTrue(elemNode instanceof JrsNumber);
    assertTrue(((JrsNumber) elemNode).getValue() instanceof BigDecimal);
}
@cowtowncoder
Copy link
Member

Thank you for reporting this. I added a failing test to reproduce the problem.

Unfortunately JacksonJrsTreeCodec has no access to feature flags of JSON, so it cannot really determine this setting at this point. To solve the problem we'd probably have to do one of:

  1. Figure out how to let codec access feature settings at time of registration (I am not sure it is quite possible to have fully dynamic link for new JSON instances)
  2. Add separate setting(s) for codec to be configured to do coercion.

@cowtowncoder cowtowncoder added 2.14 and removed 2.13 labels Jan 10, 2022
cowtowncoder added a commit that referenced this issue May 3, 2022
@cowtowncoder cowtowncoder removed the 2.14 label Feb 16, 2024
@cowtowncoder cowtowncoder changed the title USE_BIG_DECIMAL_FOR_FLOATS feature not working when using treeFrom USE_BIG_DECIMAL_FOR_FLOATS feature not working when using JSON.treeFrom() Mar 24, 2024
@cowtowncoder cowtowncoder added this to the 2.17.0 milestone Mar 24, 2024
@cowtowncoder
Copy link
Member

Fixed for 2.17.1.

NOTE: MUST register codec as extension like so:

        JSON json = JSON.builder()
                .enable(JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS)
                .register(new JrSimpleTreeExtension())
                .build();

for things to work; registering directly as TreeCodec will not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants