我有一个使用XStream来将类序列化和反序列化为XML的应用程序。效果很好。我有一个问题,静态内部类是否可序列化。根据{{3}},可以。但是,具有嵌套静态类的Test类似乎不像对数据进行序列化。
public static class TestInner
{
public static String param1;
public static String param2;
public static String getParam1()
{
return param1;
}
public static void setParam1(final String param1)
{
TestInner.param1 = param1;
}
public static String getParam2()
{
return param2;
}
public static void setParam2(final String param2)
{
TestInner.param2 = param2;
}
}
final XStream xstream = new XStream();
final TestInner inner = new Test.TestInner();
inner.setParam1("valueparam1");
inner.setParam2("valueparam2");
logger.info("{}", xstream.toXML(inner));
产量,
<test.Test_-TestInner/>
这是XStream的已知行为或限制吗?
答案 0 :(得分:0)
使用the Xstream tutorial(与您提供的代码完全相同)进行了测试,并且可以正常工作[您的代码和我的代码之间的区别是我的变量不是静态的],我认为这就是原因。也使用了以下依赖项
package com.stackoverflow;
public class TestRunnerOuter {
public static class TestRunner{
private String name;
public TestRunner() {}
public TestRunner(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
然后->
XStream xstream = new XStream();
TestRunner inner = new TestRunnerOuter.TestRunner();
inner.setName("Test");
System.out.println(xstream.toXML(inner));
依赖关系->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>
输出->
<com.stackoverflow.TestRunnerOuter_-TestRunner>
<name>Test</name>
</com.stackoverflow.TestRunnerOuter_-TestRunner>