0

我有一些使用 flexjson.JSONDeserializer 和 flexjson.JSONSerializer 的 Java 代码。(简单地说,JSONDeserializer 使用 JSON 字符串中的属性值对创建一个类实例,而 JSONSerializer 接受一个类实例并创建一个 JSON 字符串。)

现在我还需要对 XML 使用类似的东西,最好的匹配是什么,是否有类似的东西但性能更好?

简单的例子

class X {
    private Integer a;
    public void setA(Integer a);
    public Integer getA(); 
}

with json equal to {"a":1} I have the following
new JSONDeserializer<X>().use(null, X.class).deserialize(json);

with json equal to [{"a":1},{"a":2}]
new JSONDeserializer<List<X>>().use(null, ArrayList.class).use("values", X.class).deserialize(json);
4

2 回答 2

0

为了对 Java 对象与 XML 进行“哑”序列化/反序列化,最好的办法是使用javax.xml.bind.JAXB类的静态方法。marshal()unmarshal()方法应该提供与您使用 flexjson 的功能类似的功能。但是,我不能谈论性能。

于 2012-12-06T21:46:13.453 回答
0

在这里编辑 XStream 和 JAXB 的比较我仍然需要再次比较它们。结束编辑

XStream似乎比 JAXB 选项更容易,因为我也在处理 X 的集合

序列化代码很简单

return new XStream().toXML(collection);

Maven依赖是

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.3</version>
</dependency>
<dependency>
    <groupId>xmlpull</groupId>
    <artifactId>xmlpull</artifactId>
    <version>1.1.3.1</version>
</dependency>

并在您的 java 文件中添加

import com.thoughtworks.xstream.XStream;
于 2012-12-06T23:29:58.107 回答