Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson XML serialization drops elements of Integer type if their value is zero #174

Closed
rpatrick00 opened this issue Nov 23, 2015 · 4 comments

Comments

@rpatrick00
Copy link

Carrying on with my little test project to isolate the issues we are seeing with Jackson, I have the following class:

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
  @XmlAttribute
  @XmlID
  private String id;

  @XmlElement
  private String name;

  @XmlElement
  private EmployeeType type;

  @XmlElement
  private Integer extension;
  ...
}

And the object structure is being put together like this:

  public RunThis() {
    Company company = new Company();

    Employee employee1 = new Employee();
    employee1.setId("emp-1");
    employee1.setName("Robert Patrick");
    employee1.setType(EmployeeType.FULLTIME);
    employee1.setExtension(new Integer(32872));
    company.addEmployee(employee1);

    Employee employee2 = new Employee();
    employee2.setId("emp-2");
    employee2.setName("Michael Smith");
    employee2.setType(EmployeeType.CONTRACTOR);
    employee2.setExtension(new Integer(0));
    company.addEmployee(employee2);

    Employee employee3 = new Employee();
    employee3.setId("emp-3");
    employee3.setName("Nick D'Attoma");
    company.addEmployee(employee3);

In JAXB, the output is what you would expect:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
    <employees>
        <employee id="emp-1">
            <name>Robert Patrick</name>
            <type>FULLTIME</type>
            <extension>32872</extension>
        </employee>
        <employee id="emp-2">
            <name>Michael Smith</name>
            <type>CONTRACTOR</type>
            <extension>0</extension>
        </employee>
        <employee id="emp-3">
            <name>Nick D'Attoma</name>
        </employee>
    </employees>
</company>

With Jackson, the <extension> element in missing from the emp-2 employee.

<?xml version='1.1' encoding='UTF-8'?>
<company>
  <employees>
    <employee id="emp-1">
      <name>Robert Patrick</name>
      <type>FULLTIME</type>
      <extension>32872</extension>
    </employee>
    <employee id="emp-2">
      <name>Michael Smith</name>
      <type>CONTRACTOR</type>
    </employee>
    <employee id="emp-3">
      <name>Nick D'Attoma</name>
    </employee>
  </employees>
</company>

If I set the value to anything non-zero, the element appears. How can I get Jackson to not remove Integer elements set to 0?

@rpatrick00
Copy link
Author

Apparently, setting the following Serialization configuration causes this:

mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

But I am not sure why you would want to define a java.lang.Integer(0) as an empty field and turning this off is not an option for us since that makes every empty element show up...

@cowtowncoder
Copy link
Member

On inclusion criteria, this is

FasterXML/jackson-databind#952

and will be reverted for 2.7. A work-around for 2.6 would be to override serializer for java.lang.Integer.TYPE, override isEmpty().

But it sounds like there is a separate bug caused by faulty handling of exclusion in addition to that issue.

@cowtowncoder
Copy link
Member

Actually, I think this may well be just the "too wide applicability for emptiness check", so I assume it is fixed for 2.7.

@yuvrajhshinde
Copy link

I am not geeting following header if I use Jackson approach , It is working properly with JAXB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants