Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deserialize lazylist using jackson

I have a object which uses a org.apache.commons.collections.list.LazyList for one of its fields, which is serialized ti JSON. The JSON looks like this:

        "myObject": ["org.apache.commons.collections.list.LazyList", [
            {
                "attr1": "asdasd",
                "attr2": 1234
            }
        ]],

The object field looks like this:

List<MyObject> myObject = ListUtils.lazyList(new ArrayList(), {new MyObject()} as Factory)

However trying to deserialize the above JSON using a Jackson ObjectMapper fails, since it can't find a default constructor for a LazyList - which makes sense. But how can I specify how this field can be deserialized?

Error message:

No default constructor for [collection type; class org.apache.commons.collections.list.LazyList, contains [simple type, class foo.bar.MyObject]]

Bounty-constraints:

To collect the bounty, this question needs to be answered using a custom jackson deserializer - the custom deserializer must not be field specific! Hence no solution using custom implementations of a LazyList for a specific type will answer this question adequately.

like image 990
Hoof Avatar asked Nov 13 '22 02:11

Hoof


1 Answers

The solution below worked on both List and Map collection objects, it might also work on yours.

@JsonDeserialize(contentAs=MyObject.class)
private List<MyObject> myObject = ListUtils.lazyList(new ArrayList(), {new MyObject()} as Factory);
like image 149
jjcosare Avatar answered Nov 28 '22 10:11

jjcosare