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

There is no possibility to set jsonNodeFactory when working with patches. #110

Open
gabrielsson opened this issue Oct 18, 2021 · 3 comments

Comments

@gabrielsson
Copy link

gabrielsson commented Oct 18, 2021

I would like to use the library withExactBigDecimals, i.e keeping 25.000 when patching a node.

@Test
void testDecimalsKeptAfterPatch() {

    ObjectMapper mapper = new ObjectMapper()
        .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
        .setNodeFactory(JsonNodeFactory.withExactBigDecimals(true));

    JsonNode newer = mapper.readTree("25.000");
    JsonNode older = mapper.readTree("24.444");
    JsonNode apply = JsonMergePatch.fromJson(newer).apply(older);

    assertEquals("25.000", apply.asText());
}
org.junit.ComparisonFailure: expected:<25[.000]> but was:<25[]>
Expected :25.000
Actual   :25

Is it possible to inject the behaviour I am after?
The JacksonUtils that JsonMergePatch is using sets the factory to JsonNodeFactory.instance and I cannot see a way around this. I would prefer to use the JsonNodeFactory.decimalsAsIs for my specific case.

This also applies to the JsonDiff.asJson(older, newer) with the same behaviour as the above example.

@MCakrt
Copy link

MCakrt commented Nov 4, 2021

I've just come across this problem, too :(
Let me know if you find a solution.

@gabrielsson
Copy link
Author

gabrielsson commented Nov 4, 2021

No unfortunately not. I changed my schemas to string instead of number.
However I did successfully make a version without JacksonUtils but it did not feel like the correct solution.

@MCakrt
Copy link

MCakrt commented Nov 4, 2021

Hm... in my case I serialize and deserialize some classes with BigDecimal fields using objectmapper. I never had any problems with that, no loss of trailing zeroes.

I only encountered the problem when converting the object to JsonNode (to be used with JsonPatch)

JsonNode node = objectMapper.convertValue(someObjectWithBigDecimalField, JsonNode.class);

Then the trailing zeroes were trimmed.

But in this case, I just added .setNodeFactory(JsonNodeFactory.withExactBigDecimals(true) and everything works as expected.

So my problem is different from yours. My problems was that BigDecimals got trimmed while you have problem with string->BigDecimal deserialization.

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

No branches or pull requests

2 participants