Skip to content

Add MapperFeature.WRAPPERS_DEFAULT_TO_NULL to allow configuring JsonInclude.Include.NON_DEFAULT default for primitive wrapper types to be null, not wrapped default of primitive #5570

@cowtowncoder

Description

@cowtowncoder

(note: follow-up on #2105)

So: it looks like NON_DEFAULT value for Boolean -- for example -- is new Boolean(false) (similar to the default of false for primitive boolean). But this does not really make sense: for non-primitive types, default should be null.

NOTE: this is different from Class-level "Class member Default" which compares actual property values a POJO is initialized with, using default (no-args) Constructor.

Test case:

public class Test {

@Test
public void name() throws Exception {
  ObjectMapper mapper = JsonMapper.builder()
    .changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.NON_DEFAULT))
    .build();
  Pojo pojo = new Pojo();
  pojo.setValue(false);

  System.out.println(mapper.writeValueAsString(pojo));
}

public static class Pojo {
  private Boolean value;

  public Boolean getValue() {
    return value;
  }

  public void setValue(Boolean value) {
    this.value = value;
  }
}

with Jackson v 2.8.2 outputs {"value":false} (as I expected). This however changes starting with 2.8.3: it outputs {} (tested with 2.8.3, 2.8.11.2 and 2.9.6).
Default value for Boolean is null, not false.

EDIT: behavior will be controlled with a new MapperFeature.WRAPPERS_DEFAULT_TO_NULL -- planned to default to false in 3.1 (leaving existing 2.8.3 - 3.0.3 behavior as default).

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xIssues to be only tackled for Jackson 3.x, not 2.x

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions