Details
-
Type: Bug
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: 1.4, 1.4.1, 1.4.3, 1.4.4
-
Fix Version/s: 1.4.5
-
Component/s: Compatibility, Converters
-
Labels:None
Description
I was in the midst of upgrading Xstream 1.3 to 1.4. However I am finding that the JSON generated is incorrect, and I am not sure what I need to do resolve this. I wrote a JUNIT test and was able to reproduce the problem. Here's the code for the test
public class XstreamTest { @Test public void testXstreamStuffForBasicVO(){ BasicVO basicVO = new BasicVO("atttr1", "attr2"); XStream xstream = new XStream(new JsonHierarchicalStreamDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.registerConverter(new BasicVOConverter()); System.out.println(xstream.toXML(basicVO)); } class BasicVO{ @XStreamAlias("alias1") private String attribute1; @XStreamAlias("alias2") private String attribute2; BasicVO(String att1, String att2){ setAttribute1(att1); setAttribute2(att2); } BasicVO(){ } public String getAttribute1() { return attribute1; } public void setAttribute1(String attribute1) { this.attribute1 = attribute1; } public String getAttribute2() { return this.attribute2; } public String getAttribute3(){ return "madeupAttribute"; } public void setAttribute2(String attribute2) { this.attribute2 = attribute2; } } class BasicVOConverter implements Converter{ @Override public boolean canConvert(Class type) { return BasicVO.class.isAssignableFrom(type); } @Override public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { BasicVO basicVO = (BasicVO) source; writer.startNode("property1"); writer.setValue(basicVO.getAttribute1()); writer.endNode(); writer.startNode("property2"); writer.setValue(basicVO.getAttribute2()); writer.endNode(); writer.startNode("property3"); writer.setValue(basicVO.getAttribute3()); writer.endNode(); } @Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { BasicVO basicVO = new BasicVO(); reader.moveDown(); basicVO.setAttribute1(reader.getValue()); reader.moveUp(); reader.moveDown(); basicVO.setAttribute2(reader.getValue()); reader.moveUp(); return person; } } }
The output generated is the following invalid JSON( invalid because the values are not within double quotes )
{"com.xyz.XstreamTest$BasicVO": { "property1": atttr1, "property2": attr2, "property3": madeupAttribute }}
I would like to point out that, when I use JettisonMappedXmlDriver, this isn't a problem, however, I cannot use this, since I loose some other important things which I get with JsonHierarchicalStreamDriver.
With XStream 1.3, I get the following
{"com.xyz.XstreamTest$BasicVO": { "property1": "atttr1", "property2": "attr2", "property3": "madeupAttribute" }}
I went through the XStream 1.4 and 1.3 code and found significant differences in the logic for applying double quotes In 1.3, quotes are added more generally. However in 1.4, quotes are only added for a "String" type, which I am not sure how this gets determined, but I suspect, this might be causing this bug.
I am inclining towards maybe some bug in XStream 1.4 code? This seems too far fetched, I am probably missing some step.
I meant to say "..used with custom converters". I wanted to edit the title, but seems like I can't.