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

Serialization of "JacksonXmlElementWrapper"-annotated List with NULL-Value #103

Closed
5kilodope opened this issue Feb 11, 2014 · 4 comments
Closed
Milestone

Comments

@5kilodope
Copy link

I have defined a List of Objects in a POJO:

public class Order  {

  @JacksonXmlElementWrapper(localName = "line_items")
  @JacksonXmlProperty(localName = "line_item")  
  private List<LineItem> line_items = null;  

}

Normally this Element will be serialized like this if it is not null (this works as expected and is ok):

<order> 
  <line_items>
    <line_item>...</line_item>
  </line_items>
</order>

But if the List is provided as null it will be serialized like this (tested with Jackson 2.3.1):

<order> 
      <line_item/>
</order>

But correct would be:

<order> 
      <line_items/>
</order>

I also found a hint in the comments of the jackson-code :-)
See XMLBeanPropertyWriter:

        /* Hmmh. Does the default null serialization work ok here? For now let's assume
         * it does; can change later if not.
         */
        if (value == null) {
            if (_nullSerializer != null) {
                jgen.writeFieldName(_name);
                _nullSerializer.serialize(null, jgen, prov);
            }
            return;
        }
@cowtowncoder
Copy link
Member

Thanks -- will try to figure what goes on.

cowtowncoder added a commit that referenced this issue Feb 14, 2014
@cowtowncoder cowtowncoder added this to the 2, milestone Feb 14, 2014
@cowtowncoder
Copy link
Member

Fixed: my take is that ideally entry should completely not exist, that is, have:

which is now produced.

@5kilodope
Copy link
Author

you can see it that way, so that the entry should completely not exist ...

here just some thoughts about this:

the other side, that will decode this xml will not know that the encoded list was an empty list. Sometimes it could be important to differ between those three states:

  • undefined
  • null
  • defined

For example:

if i export data, the other side that decodes this data needs to be able to decide if the data should be processed.

  • If data is missing it should not be processed.
  • if data is defined it should be inserted in the database with the defined value (for an empty list this makes not much sense, because only primitive values can be inserted in sql-database).
  • if data is null it should be inserted in the database as null (this also does not make much sense for empty lists but for primitive values)

But this is some specific use-case i have at the moment ;)

@cowtowncoder
Copy link
Member

The way I see it, if input for List is null, non-existent is the way; empty List will be serialized differently, as empty List. So I did not change behavior of actual empty Lists, as far as I can see.
That would be a flaw.

I am open to discussion for this case, but there is one potential problem with including empty List for null: handling of wrapped vs unwrapped Lists would differ (I think) if null Lists were serialized as empty ones.

Although I guess this actually ties to another issue; that of how to express XML null ("nil"). So I think question needs to be in context of that issue -- when using "isNillable" marker, Lists, too, should use that facility.

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

2 participants