Immutables: Guava immutable collections and JSON serialization

Created on 9 Nov 2015  Â·  2Comments  Â·  Source: immutables/immutables

Consider the following value object:

@Value.Immutable
@JsonDeserialize(as = ImmutableFloorplan.class)
public interface Floorplan {
    Integer getId();
    String getName();
    byte[] getBackground();
    Dimensions getDimensions();
    List<Container> getRacks();
    List<Container> getCrahs();
    List<Container> getVerticalTemps();
    List<Container> getGenericTemps();
    List<Container> getPressures();
}

With Guava on the classpath, the lists will be modeled with ImmutableList (as expected), but the JSON deserialization fails with the following error: _com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.ImmutableList, contains [simple type, class com.panduit.sz.ss.assets.Container]]_

Looking at the generated code, this makes sense. There's no Jackson annotation specifying a concrete implementation:

  @JsonProperty
  @Override
  public ImmutableList<Container> getRacks() {
    return racks;
  }

Would it be possible to have the annotation processor add all necessary annotations for deserializing Guava collections?

Sure, we could add extra annotations to the value interface, but that's a lot of boilerplate (which we wanted to avoid in the first place).

question

Most helpful comment

Please, try if you can plug jackson-guava module JSON guide: Jackson Guava.
Last resort solutions would be either to disable guava collections (Value.Style(jdkOnly=true, ..)) or to add extra Jackson annotations.

All 2 comments

Please, try if you can plug jackson-guava module JSON guide: Jackson Guava.
Last resort solutions would be either to disable guava collections (Value.Style(jdkOnly=true, ..)) or to add extra Jackson annotations.

I'm sorry, I should have read the docs more carefully. Yes, that was the problem; I was missing the dependency on jackson-datatype-guava. It's all good.

Was this page helpful?
0 / 5 - 0 ratings