-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
readerForUpdating in 2.9.0.pr3 merge list instead of replace #1625
Comments
Could you please provide piece of sample code to show exactly what you are doing, so I can reproduce the issue. |
Thanks cowtowncoder for your response. HashMap variables = new HashMap(); I expect list = ["b"], but it is ["a","b"] |
Actually, this is the way merging should work as per #1399. I'll see whether default merge settings take effect here, will add notes. |
Ok, so, yes, merging is occurring here. Part of the problem is that this is root-level value, and since call via Unfortunately settings that would otherwise be applicable (such as disabling mergeability of types like I'll have to see what could be done. |
Ok, so... I added support for disabling merging for root-value mapper.setDefaultMergeable(false); // global default, no merging except where annotated
mapper.configOverride(Object.class) // prevent merging of values with type `Object`
.setMergeable(false); This is somewhat limited, and similar handling could be added for other types. |
Has there been an update since this was initially implemented? We are dealing with a JSON list that has many requirements for unique properties, so ideally we'd just be able to replace an entire list instead of having to worry about merging it correctly. This (pseudo-) code is my current situation: ObjectMapper mapper = new ObjectMapper();
JsonNode listA = mapper.readTree("{\"list\": [ \"A\" ] }");
JsonNode listB = mapper.readTree("{\"list\": [ \"B\" ] }");
JsonNode result = mapper.readerForUpdating(listA).readValue(listB);
System.out.println(result.toPrettyString()); // prints {"list":["A","B"]} But ideally, I would only have a single element in the list (being B). I've tried adding the mapper options from the previous comment, but without any luck. Any suggestions? We're currently using Jackson 2.13.3. |
@SAJLinders No, the default behavior is addition. For overrides you could try applying those to If that does not work I don't think there are other options at this point. |
@cowtowncoder thanks a lot, I wasn't aware that it had to be exact type, not super type. Since yesterday I was able to figure out what was going on though. This behaviour was only patched in Jackson 2.14 as per #3338, so I just had to use that version. For anyone reading this in the future, the following test will pass: @Test
public void testMergeArrays_shouldReplace() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configOverride(ArrayNode.class).setMergeable(false);
JsonNode listA = mapper.readTree("{\"list\": [ \"A\" ] }");
JsonNode listB = mapper.readTree("{\"list\": [ \"B\" ] }");
JsonNode result = mapper.readerForUpdating(listA).readValue(listB);
assertEquals(listB, result);
} |
@SAJLinders Ok great! Thank you for adding an update, that should help others who might have hit the issue. And yes, the requirement for exact type match is non-obvious. |
I'm using code objectMapper.readerForUpdating(variables).readValue(jsonData)
variables is an existing HashMap, If there is list in the Map and jsonData, the lists will get merged, instead of replacing the existing list in the map.
The text was updated successfully, but these errors were encountered: