Skip to content

Commit

Permalink
Fix #103
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 14, 2014
1 parent 4f7255f commit ebc13fd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,32 @@ public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider
throws Exception
{
Object value = get(bean);

/* Hmmh. Does the default null serialization work ok here? For now let's assume
* it does; can change later if not.

/* 13-Feb-2014, tatu: As per [#103], default handling does not really
* work here. Rather, we need just a wrapping and should NOT call
* null handler, as it does not know what to do...
*
* Question, however, is what should it be serialized as. We have two main
* choices; equivalent empty List, and "nothing" (missing). Let's start with
* empty List? But producing missing entry is non-trivial...
*/
if (value == null) {
if (_nullSerializer != null) {
jgen.writeFieldName(_name);
_nullSerializer.serialize(null, jgen, prov);
// if (_nullSerializer != null) { ... }

// For Empty List, we'd do this:
/*
@SuppressWarnings("resource")
final ToXmlGenerator xmlGen = (jgen instanceof ToXmlGenerator) ? (ToXmlGenerator) jgen : null;
if (xmlGen != null) {
xmlGen.startWrappedValue(_wrapperQName, _wrappedQName);
xmlGen.finishWrappedValue(_wrapperQName, _wrappedQName);
}
*/
// but for missing thing, well, just output nothing

return;
}

// then find serializer to use
JsonSerializer<Object> ser = _serializer;
if (ser == null) {
Expand All @@ -112,13 +126,16 @@ public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider
}
// For non-nulls: simple check for direct cycles
if (value == bean) {
_handleSelfReference(bean, ser);
// NOTE: method signature here change 2.3->2.4
if (_handleSelfReference(bean, jgen, prov, ser)) {
return;
}
}

// Ok then; addition we want to do is to add wrapper element, and that's what happens here
// 19-Aug-2013, tatu: ... except for those nasty 'convertValue()' calls...
@SuppressWarnings("resource")
final ToXmlGenerator xmlGen = (jgen instanceof ToXmlGenerator) ? (ToXmlGenerator) jgen : null;
// Ok then; addition we want to do is to add wrapper element, and that's what happens here
// 19-Aug-2013, tatu: ... except for those nasty 'convertValue()' calls...
if (xmlGen != null) {
xmlGen.startWrappedValue(_wrapperQName, _wrappedQName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fasterxml.jackson.dataformat.xml.lists;

import java.util.*;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class TestWrappedLists extends XmlTestBase
{
static class Order {
@JacksonXmlElementWrapper(localName = "line_items")
@JacksonXmlProperty(localName = "item")
private List<ListItem> line_items; // new ArrayList<ListItem>();
}

static class ListItem {
public int id;

public ListItem(int id) { this.id = id; }
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

private final XmlMapper MAPPER = xmlMapper(true);

/* For [Issue#103]
*/
public void testEmptyList() throws Exception
{
String xml = MAPPER.writeValueAsString(new Order());
assertEquals("<Order/>", xml);
// If we expected Empty list, it'd be:
// assertEquals("<Order><line_items/></Order>", xml);
}
}

0 comments on commit ebc13fd

Please sign in to comment.