jettison-json/jettison

Jettison 1.3.x array handling breaks XStream

Closed this issue · 1 comments

With the latest version it is no longer possible to enfore array handling for an element. Actually the serializeAsArray flag triggers non-symmetric behavior of Jettison between its reader and writer. See following unit tests, that represent XStream's internal call sequence and which succeed with Jettison 1.2.

{code:java}
public void testJettisonArraysAreBroken() throws XMLStreamException {
final MappedXMLOutputFactory mof;
final MappedXMLInputFactory mif;
final Configuration config = new Configuration();
mof = new MappedXMLOutputFactory(config);
mif = new MappedXMLInputFactory(config);
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter writer = mof.createXMLStreamWriter(out);
((AbstractXMLStreamWriter)writer).serializeAsArray("list");
writer.writeStartDocument();
writer.writeStartElement("list");
writer.writeStartElement("string");
writer.writeCharacters("a");
writer.writeEndElement();
writer.writeStartElement("string");
writer.writeCharacters("b");
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
writer.close();
assertEquals("{'list':[{'string':['a','b']}]}".replace(''', '"'), out.toString());
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLStreamReader reader = mif.createXMLStreamReader(in);
assertEquals(XMLStreamConstants.START_ELEMENT, reader.next());
assertEquals("list", reader.getLocalName());
assertEquals(XMLStreamConstants.START_ELEMENT, reader.next());
assertEquals("string", reader.getLocalName());
assertEquals(XMLStreamConstants.CHARACTERS, reader.next());
assertEquals("a", reader.getElementText());
assertEquals(XMLStreamConstants.END_ELEMENT, reader.next());
assertEquals(XMLStreamConstants.START_ELEMENT, reader.next());
assertEquals("string", reader.getLocalName());
assertEquals(XMLStreamConstants.CHARACTERS, reader.next());
assertEquals("b", reader.getElementText());
assertEquals(XMLStreamConstants.END_ELEMENT, reader.next());
assertEquals(XMLStreamConstants.END_ELEMENT, reader.next());
}

public void testJettisonSingleElementArraysAreBroken() throws XMLStreamException {
    final MappedXMLOutputFactory mof;
    final MappedXMLInputFactory mif;
    final Configuration config = new Configuration();
    mof = new MappedXMLOutputFactory(config);
    mif = new MappedXMLInputFactory(config);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLStreamWriter writer = mof.createXMLStreamWriter(out);
    ((AbstractXMLStreamWriter)writer).serializeAsArray("list");
    writer.writeStartDocument();
    writer.writeStartElement("list");
    writer.writeStartElement("string");
    writer.writeCharacters("a");
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndDocument();
    writer.close();
    assertEquals("{'list':[{'string':'a'}]}".replace('\'', '"'), out.toString());
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLStreamReader reader = mif.createXMLStreamReader(in);
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.next());
    assertEquals("list", reader.getLocalName());
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.next());
    assertEquals("string", reader.getLocalName());
    assertEquals(XMLStreamConstants.CHARACTERS, reader.next());
    assertEquals("a", reader.getElementText());
    assertEquals(XMLStreamConstants.END_ELEMENT, reader.next());
    assertEquals(XMLStreamConstants.END_ELEMENT, reader.next());
}

{code}

dkulp commented

Resolved via a new config option to restore old behavior.