【问题标题】:Jettison or KryoJettison 或 Kryo
【发布时间】:2012-07-22 09:39:47
【问题描述】:

我目前正在将 JAXB 用于我正在处理的项目,并希望将我的库归档 xml 转换为归档 json,以便在我的项目中发挥作用。我想我会使用Jettison,因为它似乎是easier to implement,因为它实际上适用于JAXB;但是,查看不包含 Jettison 的 Older benchmarks,我发现 Kryo 生成的文件更小,并且比某些替代方法更快地进行序列化和反序列化。

谁能告诉我关键区别或 Jettison 如何与 Kryo 相提并论,尤其是对于未来的项目,例如 android 应用程序。

编辑:

我想我正在寻找生成更小文件和更快运行的方法。可以牺牲人类的可读性,因为我不打算只读取文件来处理它们

【问题讨论】:

    标签: java json serialization deserialization kryo


    【解决方案1】:

    它们的用途有些不同:

    • Jettison 用于读取/写入 JSON。如果您需要与 JSON(人类可读)数据格式进行互操作,请使用它
    • Kryo 用于高效的二进制序列化。如果您需要高性能和小型编码对象(例如实时游戏中的消息通信),请使用它。

    由于听起来您正在使用该格式来存档数据,因此人类可读性和使用标准的长寿命格式可能比效率更重要,所以我怀疑您会想要选择 JSON 路线。

    【讨论】:

    • 这两个项目实际上都是游戏,这就是 Kryo 吸引我的原因。
    【解决方案2】:

    注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222) 专家组的成员。

    由于您已经建立了 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.JAXBContextFactory
    

    input.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" ]
       }
    }
    

    关于输出的几点注意事项:

    1. 由于 id 字段是数字类型,它被编组为不带引号的 JSON。
    2. 即使 id 字段与 @XmlAttribute 映射,JSON 消息中也没有对此的特殊指示。
    3. email 属性的 List 大小为 1,这在 JSON 输出中得到了正确表示。
    4. xsi:nil 机制用于指定 lastName 字段具有 null 值,这已在 JSON 输出中转换为正确的 null 表示。

    更多信息

    【讨论】:

    • 他们是导出混合 HashMap 列表的好方法吗?即HashMap
    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    相关资源
    最近更新 更多