質問

I currently use JAXB for a project i'm working on and looking to convert my libraries archived xml to archived json, To act in my project. I figured I would use Jettison as it seems it would be easier to implement since it actually works with JAXB; however, looking at Older benchmarks in which Jettison was not included I have found Kryo produces smaller files and Serialized and DeSerialized quicker than some alternatives.

Can anyone inform me of the key difference or otherwise how Jettison stacks up to Kryo, especially for future projects such as android applications.

EDIT:

I guess i'm looking for what produces smaller files and operates faster. Human readability can be sacrificed since I don't plan on reading the files only processing them

役に立ちましたか?

解決

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

Since you already have established JAXB mappings and are converting XML to JSON, you may be interested in EclipseLink JAXB (MOXy) which offers both object-to-XML and object-to-JSON mapping using the same JAXB metadata.

Customer

Below is a sample model with JAXB annotations.

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

To use MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: 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>

Demo

The following demo code will populate the objects from XML and then output JSON. Note how there are no compile time dependencies on 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 Ouput

Below is the output from running the demo code.

{
   "customer" : {
      "id" : 123,
      "firstName" : "Jane",
      "lastName" : null,
      "email" : [ "jdoe@example.com" ]
   }
}

A few things to note about the output:

  1. Since the id field is a numeric type it was marshalled to JSON without quotes.
  2. Even though the id field was mapped with @XmlAttribute there are no special indication of this in the JSON message.
  3. The email property had a List of size one, this is properly represented in the JSON output.
  4. The xsi:nil mechanism was used to specify that the lastName field had a null value, this has been translated to the proper null representation in the JSON output.

For More Information

他のヒント

They are for somewhat different purposes:

  • Jettison is for reading/writing JSON. Use it if you need to interoperte with a JSON (human-readable) data format
  • Kryo is for efficient binary serialisation. Use it if you need high performance and small encoded object sizes (e.g. communication of messages in a realtime game).

Since it sounds like you are using the format to archive data, human-readability and the use of a standard long-lived format is probably more important than efficiency, so I suspect you will want to choose the JSON route.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top