我目前正在为一个项目使用JAXB,希望将我的库归档xml转换为归档json,以便在我的项目中发挥作用。我认为我应该使用Jettison,因为它看起来像是easier to implement,因为它实际上可以与JAXB一起工作;但是,看看没有包括丢弃的Older benchmarks,我发现Kryo生成的文件更小,序列化和DeSerialized比一些替代方案更快。
谁能告诉我关键的区别或其他如何丢弃堆叠到Kryo,特别是对未来的项目,如android应用程序。
编辑:
我想我正在寻找什么能产生更小的文件和更快的操作。人类的可读性可能会被牺牲,因为我不打算读取文件,只打算处理它们
发布于 2012-07-22 18:28:58
注意:我是)的负责人,也是专家组的成员。
由于您已经建立了JAXB映射,并且正在将XML转换为JSON,因此您可能会对EclipseLink JAXB (MOXy)感兴趣,它使用相同的JAXB元数据提供了对象到XML和对象到JSON的映射。
客户
下面是一个带有JAXB注释的示例模型。
package forum11599191;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlAttribute
private int id;
private String firstName;
@XmlElement(nillable=true)
private String lastName;
private List<String> email;
}jaxb.properties
要使用MOXy作为JAXB提供者,需要在与域模型相同的包中包含一个名为jaxb.properties的文件,并包含以下条目(请参见:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactoryinput.xml
<?xml version="1.0" encoding="UTF-8"?>
<customer id="123" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<firstName>Jane</firstName>
<lastName xsi:nil="true"/>
<email>jdoe@example.com</email>
</customer>演示
下面的演示代码将填充来自XML的对象,然后输出JSON。注意,MOXy上没有编译时依赖项。
package forum11599191;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
// Unmarshal from XML
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11599191/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml);
// Marshal to JSON
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.marshal(customer, System.out);
}
}JSON输出
下面是运行演示代码的输出。
{
"customer" : {
"id" : 123,
"firstName" : "Jane",
"lastName" : null,
"email" : [ "jdoe@example.com" ]
}
}关于输出,有几点需要注意:
id字段是不带引号的数字类型,因此它被封送到JSON。id字段是用@XmlAttribute映射的,在JSON消息中也没有这方面的特殊指示。email属性的大小为1,这在JSON输出中得到了正确的表示。lastName字段具有null值,这已转换为JSON输出中正确的null表示形式。有关详细信息的,请访问
发布于 2012-07-22 17:54:30
它们的用途略有不同:
JSON丢弃是用于读/写的。如果您需要与JSON (人类可读的)数据格式进行互操作,请使用它。
由于听起来像是在使用这种格式来归档数据,因此人类的可读性和使用标准的长期格式可能比效率更重要,所以我怀疑您可能希望选择JSON路线。
https://stackoverflow.com/questions/11599191
复制相似问题