Add elements to initialized custom collection

5 views
Skip to first unread message

nli...@gmail.com

unread,
Oct 17, 2024, 3:04:49 PM (11 days ago) Oct 17
to jackson-user
My class contains an initialized custom collection

class MyClass {

    MyList<Integer> myList = MyList.create();
}

where MyList is a j.u.List. When deserializing, I get an exception saying that there is no known deserializer for MyList ("Cannot find a deserializer for non-concrete List type"), which is generally understandable, but I already initialized it, so there's no need for one. Instead, I just want to add the items to the list.
To demonstrate, I can add a private setter inside the class:

private void setMyList(List<Integer> ints) { ints.forEach(MyList::add); }

this works, but I don't think it should be needed. Specifically, Jackson should not create any list. It needs to deserialize the values of the items anyway; then instead of creating a list, add them to the existing one. It's a List, so it should know to call 'add' ('put' for map).

I know there is @JsonMerge, and I thought that that's what I needed, but just adding it to the field doesn't work, I still get the same exception. Am I not understanding @JsonMerge or am I doing something wrong?

Thanks!

Joo Hyuk Kim

unread,
Oct 17, 2024, 6:47:07 PM (10 days ago) Oct 17
to jackso...@googlegroups.com
Jackson should not create any list.

To be more specific, You do not want to create any list and ur looking for a such feature.

What we can do here  is write a custom deserializer to do such thing and there is already plenty references on the web

2024년 10월 18일 (금) 오전 4:04, nli...@gmail.com <nli...@gmail.com>님이 작성:
--
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 jackson-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/af74c8b0-1516-4cc8-9117-be6808cf9223n%40googlegroups.com.

nli...@gmail.com

unread,
Oct 18, 2024, 3:06:15 AM (10 days ago) Oct 18
to jackson-user
So @JsonMerge is not appropriate here?

Joo Hyuk Kim

unread,
Oct 18, 2024, 4:53:37 AM (10 days ago) Oct 18
to jackso...@googlegroups.com
Your implementation Does not seem too off , But not sure :/

2024년 10월 18일 (금) 오후 4:06, nli...@gmail.com <nli...@gmail.com>님이 작성:

nli...@gmail.com

unread,
Oct 18, 2024, 1:00:52 PM (10 days ago) Oct 18
to jackson-user
Perhaps Tatu can have a look?

Joo Hyuk Kim

unread,
Oct 19, 2024, 2:02:48 AM (9 days ago) Oct 19
to jackso...@googlegroups.com
Actually, I think we will get more answers moving the discussion to Github discussions.
WDYT?

nli...@gmail.com

unread,
Oct 19, 2024, 5:57:37 AM (9 days ago) Oct 19
to jackson-user
Didn't know they exist. Last time I was told to use the mailing lists.
Reply all
Reply to author
Forward
0 new messages