I would initialize null or absent lists with empty collections when deserializing. For simple data types like string, long etc. this works with this setting.
However, as soon as I define my own class this no longer works.
class FooTest {
@Autowired
private ObjectMapper objectMapper;
@Test
void empty() throws JsonProcessingException {
objectMapper.setDefaultSetterInfo( JsonSetter.Value.forValueNulls( Nulls.AS_EMPTY ) );
final Foo foo = objectMapper.readValue( "{}", Foo.class );
final Foo foo1 = objectMapper.readValue( "{\"items\": null,\"items1\": null}", Foo.class );
}
@JsonIgnoreProperties( ignoreUnknown = true )
public class Foo {
@JsonDeserialize( as = LinkedHashSet.class )
private Set<String> items;
@JsonDeserialize( as = LinkedHashSet.class )
private Set<Bar> items1;
@JsonCreator
public Foo(
@JsonProperty( "items" ) final Set<String> items,
@JsonProperty( "items1" ) final Set<Bar> items1 ) {
this.items = items;
this.items1 = items1;
}
}
@JsonIgnoreProperties( ignoreUnknown = true )
public class Bar {
@JsonCreator
public Bar( ){
}
}
}
Does anyone know why?