Issue Details (XML | Word | Printable)

Key: JETTISON-22
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Dejan Bosanac
Reporter: John Wang
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Jettison

array serilaization is not consistent

Created: 26/Apr/07 11:15 PM   Updated: 19/Feb/08 09:35 AM   Resolved: 19/Feb/08 09:35 AM
Return to search
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 1.0

Time Tracking:
Not Specified

File Attachments: 1. File issue22.diff (2 kB)

Environment: java 1.5, macOSX

Testcase included: yes


 Description  « Hide

array serialization inconsistent between array size=1 and size>1, see the following test snippet

public static void main(String[] args) {
class A{ int n; }

A a =new A();
a.n=5;

A b =new A();
b.n=6;

A[] array=new A[]{a};

XStream xstream=XStreamDispenser.getJSONXStream();

System.out.println(xstream.toXML(array));

A[] array2=new A[]{a,b};
System.out.println(xstream.toXML(array2));

}

The first output is missing the '[' ']' which indedicates to the reader of an array of size 1.



Dan Diephouse added a comment - 27/Apr/07 08:21 AM

Any chance you can submit tests/code that show this behavior using Jettison directly instead of XStream?


Dan Diephouse made changes - 27/Apr/07 08:21 AM
Field Original Value New Value
Fix Version/s 1.0 [ 13412 ]
John Wang added a comment - 27/Apr/07 10:20 AM

yes:

public static void main(String[] args) {
class A{ int n; }

A a =new A();
a.n=5;

A b =new A();
b.n=6;

A[] array=new A[]{a};

XStream xstream=new XStream(new JettisonDriver());

System.out.println(xstream.toXML(array));

A[] array2=new A[]{a,b};
System.out.println(xstream.toXML(array2));

xstream=new XStream(new DomDriver());

System.out.println(xstream.toXML(array));

System.out.println(xstream.toXML(array2));

}

The above test also compares what is returned by the DomDriver.


Dan Diephouse added a comment - 27/Apr/07 10:23 AM

I think you missed my point - Can you show this bug using just the org.codehaus.jettison.* classes? i.e. MappedXMLStreamWriter?


John Wang added a comment - 29/Apr/07 02:16 PM

Sorry for misunderstanding you.

JettionsDriver.createWriter methods are implemented as following:

where mof is an instance of MappedXMLOutputFactory. <-- Jettison class.

public HierarchicalStreamWriter createWriter(Writer writer) {
try { return new StaxWriter(new QNameMap(), mof.createXMLStreamWriter(writer)); } catch (Exception e) { throw new StreamException(e); }
}

public HierarchicalStreamWriter createWriter(OutputStream output) {
try { return new StaxWriter(new QNameMap(), mof.createXMLStreamWriter(output)); } catch (Exception e) { throw new StreamException(e); } }
}

Thanks

-John


John Wang added a comment - 29/May/07 11:27 AM

Hi Dan:

Any updates on this bug? The project we are working on really depends on this fix.

Thanks

-John


Joerg Schaible added a comment - 23/Jan/08 04:02 PM

Hi Dan,

I've investigated in this and the unit test below shows what XStream is calling:

{format:java}
public void testIssue22() throws XMLStreamException, IOException {
StringWriter strWriter = new StringWriter();
MappedNamespaceConvention con = new MappedNamespaceConvention();
AbstractXMLStreamWriter w = new MappedXMLStreamWriter(con, strWriter);

w.writeStartDocument();
w.writeStartElement("", "array-a", "");

w.writeStartElement("", "a", "");
w.writeStartElement("", "n", "");
w.writeCharacters("1");
w.writeEndElement();
w.writeEndElement();
/*
w.writeStartElement("", "a", "");
w.writeStartElement("", "n", "");
w.writeCharacters("2");
w.writeEndElement();
w.writeEndElement();
*/
w.writeEndElement();
w.writeEndDocument();

w.close();
strWriter.close();

System.out.println(strWriter.toString());

assertEquals("{\"array-a\":{\"a\":[{\"n\":\"1\"}]}}", strWriter.toString());
}{format}

It boils down to the question, how can the MappedXMLStreamWriter be forced to write an array containing a single element?

  • Jörg

Joerg Schaible added a comment - 23/Jan/08 04:08 PM

Since the JIRA formatting went wrong, here's the code as diff.


Joerg Schaible made changes - 23/Jan/08 04:08 PM
Attachment issue22.diff [ 32099 ]
Dejan Bosanac added a comment - 19/Feb/08 09:35 AM

fixed by adding a a special seriliazeAsArray() method that could be used to force appropriate serialization. See the following use case for example. In order to make XStream support this, the driver should be changed, which I will do after the release

public void testSingleArrayElement() throws Exception {
StringWriter strWriter = new StringWriter();
MappedNamespaceConvention con = new MappedNamespaceConvention();
AbstractXMLStreamWriter w = new MappedXMLStreamWriter(con, strWriter);
w.seriliazeAsArray(con.createKey("", "", "array-a"));

w.writeStartDocument();
w.writeStartElement("", "array-a", "");

w.writeStartElement("", "a", "");
w.writeStartElement("", "n", "");
w.writeCharacters("1");
w.writeEndElement();
w.writeEndElement();

w.writeEndElement();
w.writeEndDocument();

w.close();
strWriter.close();

System.out.println(strWriter.toString());

assertEquals("{\"array-a\":{\"a\":[{\"n\":1}]}}", strWriter.toString());
}


Dejan Bosanac made changes - 19/Feb/08 09:35 AM
Assignee Dan Diephouse [ dandiep ] Dejan Bosanac [ dejanb ]
Resolution Fixed [ 1 ]
Status Open [ 1 ] Resolved [ 5 ]