使用转换器时,XStream 1.4删除双引号

时间:2013-03-12 00:18:39

标签: java json xstream

我正在将Xstream 1.3升级到1.4。但是我发现生成的JSON不正确,我不确定我需要做什么来解决这个问题。我写了一个JUNIT测试,并能够重现这个问题。这是测试的代码

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;
            }

         }
}

生成的输出是以下无效JSON(无效,因为值不在双引号内)

{"com.xyz.XstreamTest$BasicVO": {
  "property1": atttr1,
  "property2": attr2,
  "property3": madeupAttribute
}}

我想指出,当我使用JettisonMappedXmlDriver时,这不是问题,但是,我不能使用它,因为我放弃了一些其他重要的东西,我用JsonHierarchicalStreamDriver。

使用Xstream 1.3,我得到以下内容

 {"com.xyz.XstreamTest$BasicVO": {
      "property1": "atttr1",
      "property2": "attr2",
      "property3": "madeupAttribute"
    }}

我浏览了Xstream 1.4和1.3代码,发现了显着的差异。另外,我发现引用逻辑的方式存在差异。在1.3中,更广泛地添加了引号。但是在1.4中,仅为“String”类型添加了引号,我不确定如何确定它。

我倾向于xstream 1.4代码中的一些错误?这似乎太过分了,我可能错过了一些步骤。任何人都可以帮我解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:0)

这似乎是当前的错误,并将在下一个版本中修复

https://jira.codehaus.org/browse/XSTR-728

由于