Hi
Mohammad Meah wrote:
> Hi,
>
> I am 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 xstream = new
> XStream(new JettisonMappedXmlDriver());
> xstream.setMode(XStream.NO_REFERENCES);
> xstream.registerConverter(new BasicVOConverter());
> System.out.println(xstream.toXML(basicVO));
> //return setupXstream(xstream);
> }
>
> 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 person = new BasicVO();
>
> reader.moveDown();
> person.setAttribute1(reader.getValue());
> reader.moveUp();
>
> reader.moveDown();
> person.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. Also, I found differences in the way the logic for quotes is
> applied. 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.
>
> I am inclining towards maybe some bug in xstream 1.4 code? This seems too
> far fetched, I am probably missing some step. Can anyone help me to
> resolve this? Thanks
well, the JsonHierarchicalSteamWriter actually uses the type of the original
value to format the value in a proper way (in contrast to the Jettison-based
driver). Therefore XStream's converters use internally everywhere the helper
method
ExtendedHierarchicalStreamWriterHelper.startNode(writer, "node",
String.class);
instead of
writer.startNode("node");
However, you're right, XStream should have used String.class as default if
no type is available. Can you open a JIRA issue for this?
Regards,
Jörg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email