Apologies for opening a zombie thread. I am also interested in deep merge
of 2 arrays based on index.
If I have 2 JSON strings *JSON string1 *and *JSON string2* as below, then I
would like the output merged property like *Merged JSON*
*JSON string1:*
{
"property1": [
{
"property2": "value1",
},
{
"property2": "value2",
}
]
}
*JSON string2:*
{
"property1": [
{
"property3": "value3"
},
{
"property3": "value4"
}
]
}
*Merged JSON:*
{
"property1": [
{
"property2": "value1",
"property3": "value3"
},
{
"property2": "value2",
"property3": "value4"
}
]
}
As one of the solutions, I tried registering custom deserializer (which can
look at array index position for merging) by extending
CollectionDeserializer like below. This code gets executed but the custom
deserializer doesn't get called while deserializing my class. Jackson still
calls CollectionDeserializer.
*private* *static* *void* registerDeserializer(ObjectMapper mapper) {
SimpleModule module = *new* SimpleModule();
module.setDeserializerModifier(*new* BeanDeserializerModifier() {
@Override
*public* JsonDeserializer<?>
modifyCollectionDeserializer(DeserializationConfig config,
CollectionType type, BeanDescription beanDesc,
JsonDeserializer<?> deserializer) {
*if* (List.*class*.isAssignableFrom(beanDesc.getBeanClass())
&& deserializer *instanceof* CollectionDeserializer) {
CollectionDeserializer collectionDeserializer =
(CollectionDeserializer)deserializer;
*if* (*LIST_CLASSES*.stream().anyMatch(clazz ->
clazz.isAssignableFrom(type.getContentType().getRawClass())))
{
*return* *new* CustomCollectionDeserializer(type, *null*
, *null*, collectionDeserializer.getValueInstantiator());
}
}
*return* deserializer;
}});
mapper.registerModule(module);
}
*public* *static* <T> T mergeObjects(T obj1, String obj2) *throws*
JsonMappingException, JsonProcessingException {
ObjectReader objectReader = *mapper*.readerForUpdating(obj1);
return objectReader.readValue(obj2);
}
Can you help me with:
- how to register custom CollectionDeserializer, so that it gets invoked? or
- suggest if there are better ways to achieve index based deep merge of 2
arrays
Regards,
Saurin
On Sunday, October 28, 2018 at 6:00:00 PM UTC-7 Tatu Saloranta wrote:
> Merging for arrays does mean concatenation (append for Collections),
> and nothing else. This because while there are many possible ways
> users might
> want to handle merging of two sequence values, it is surprisingly
> difficult to figure out declarative way that covers enough without
> being very complicated.
>
> So: instead of trying to figure out a complex solution that might not
> cover all cases, I went with simple, predictable solution, which does
> not cover all cases.
>
> In future it could be possible to extend `@JsonMerge` to perhaps allow
> custom handler of some sort that would allow custom logic, but this
> does not yet exist.
>
> -+ Tatu +-
> On Fri, Oct 26, 2018 at 3:32 PM Dzmitry Sankouski
> <[email protected]> wrote:
> >
> > I have two Jsons I want to merge. Arrays in json should be also merged,
> not concatenated.
> >
> > Example:
> > Json 1:
> > {
> > "level1": {
> > "value": "a",
> > "anotherValue": "z",
> > "array1": [
> > {
> > "value": "b",
> > "anotherValue": "z"
> > }
> > ]
> > }
> > }
> >
> >
> > Json 2:
> > {
> > "level1": {
> > "value": "c",
> > "array1": [
> > {
> > "value": "d"
> > }
> > ]
> > }
> > }
> >
> > After merge, I want to get:
> > {
> > "level1": {
> > "value": "c",
> > "anotherValue": "z",
> > "array1": [
> > {
> > "value": "d",
> > "anotherValue": "z"
> > }
> > ]
> > }
> > }
> >
> >
> > Using code from this suggestion about json merge, I got array1 not
> merged, but concatenated:
> > {
> > "level1": {
> > "value": "c",
> > "anotherValue": "z",
> > "array1": [
> > {
> > "value": "b",
> > "anotherValue": "z"
> > },
> > {
> > "value": "d",
> > "anotherValue": "z"
> > }
> > ]
> > }
> > }
> >
> >
> > How do I achieve arrays in json being merged, not concatenated?
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "jackson-user" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> > To post to this group, send email to [email protected].
> > For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/f225b362-f8ca-4000-ad2a-61c2801012e8n%40googlegroups.com.