I'm unable to @JsonDeserialize using List
Test case:
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.immutables.value.Value;
import java.util.Collection;
import java.util.List;
@Value.Immutable
@JsonDeserialize(as = ImmutableA.class)
public abstract class A {
// List<Foo> fails to deserialize. Error message given:
// Can not find a deserializer for non-concrete Collection type
// [collection type; class com.google.common.collect.ImmutableList,
// contains [simple type, class com.browserup.runDefinition.A$Foo]]
@JsonProperty("foos")
public abstract List<Foo> foos();
// Collection<Foo> can deserialize without error:
// public abstract Collection<Foo> foos();
@Value.Immutable
@JsonDeserialize(as = ImmutableFoo.class)
public static abstract class Foo {
@JsonProperty("id")
public abstract Integer id();
}
public static void main(String[] args) throws Exception {
String data = "{ \"foos\": [ { \"id\": 0 } ] }";
ObjectMapper mapper = new ObjectMapper();
A a = mapper.readValue(data, ImmutableA.class); // Fails
}
}
There are 2 options how to handle it. The first option is to use jdkOnly=true style attribute to generate collection attributes without using Guava collections. The second option is to register module to handle Guava types https://github.com/FasterXML/jackson-datatype-guava . Just to mention, for java8 types there's similar module https://github.com/FasterXML/jackson-datatype-jdk8.
Hope this helps, thank you!
Adding @Value.Style(jdkOnly=true) to the top of the class worked.
Using the jackson-datatype-jdk8 module did not work for me. I registered the module using:
mapper.registerModule(new Jdk8Module());
While the JDK8 module helps for resolving Optional types, it doesn't have any impact on List types.
I'll proceed using the jdkOnly types approach.
Thank you so much Eugene!
I hope this works for you, I also checked that mentioned https://github.com/FasterXML/jackson-datatype-guava should also work if you stick with Guava collection. Jdk-only is also a good choice, so you can mix and match.
Worked for me with JdkOnly=true. Thanks !
Most helpful comment
There are 2 options how to handle it. The first option is to use
jdkOnly=truestyle attribute to generate collection attributes without using Guava collections. The second option is to register module to handle Guava types https://github.com/FasterXML/jackson-datatype-guava . Just to mention, for java8 types there's similar module https://github.com/FasterXML/jackson-datatype-jdk8.Hope this helps, thank you!