Hoof
Hoof

Reputation: 1758

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.

Upvotes: 5

Views: 809

Answers (1)

jjcosare
jjcosare

Reputation: 1473

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);

Upvotes: 1

Related Questions