Jackson or JAXB, which one is better for JSON? [closed] Jackson or JAXB, which one is better for JSON? [closed] json json

Jackson or JAXB, which one is better for JSON? [closed]


As it says in the site JacksonFAQ:

Jackson is "100% JSON" and does not try to imitate or emulate XML. Property name mapping is based on standard Java Bean naming convention (although can be overridden using annotations or custom naming strategy).

There are some support compatibility features -- such as ability to optionally use JAXB annotations -- but fundamentally Jackson is a pure JSON/Java data mapper and tries to minimize impedance between JSON and Java data models.

And it continues JacksonFaq#JAXB:

Jackson supports some level of interoperability with JAXB API (javax.xml.bin):

With Jackson 1.1, it is possible to use JAXB annotations in addition to (or instead of) core Jackson annotations

JAXB API, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file

JAXB was part of standard JDK until JDK 1.8. From JDK 1.9 and above it is necessary to add JAXB as a separate library.


Here is a test that says that Jackson is faster but I have not checked the code and its results Test


I have used Jackson 2.9.8 and works well with both JSON and XML.My vote is for Jackson library + Jackson-dataformat-xml.jar. Have a look on the code for JSON and XML, it's just the same with bit change here and there.

  /**** MainClass ****/  import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;  import com.fasterxml.jackson.annotation.PropertyAccessor;  import com.fasterxml.jackson.core.JsonProcessingException;  import com.fasterxml.jackson.databind.ObjectMapper;  import com.fasterxml.jackson.databind.SerializationFeature;  import com.fasterxml.jackson.dataformat.xml.XmlMapper; public class MainClass {public static void main(String[] args) throws JsonProcessingException {    // Serialization: java obj to json--> writeValueAsString    // DeSerialization: json to java obj--> ReadValue    XmlMapper mapper1 = new XmlMapper();    ObjectMapper mapper2 = new ObjectMapper();    mapper1.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);    mapper2.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);    mapper1.enable(SerializationFeature.INDENT_OUTPUT);    mapper2.enable(SerializationFeature.INDENT_OUTPUT);    MyPojo mypojo = new MyPojo();    mypojo.setName("Dhani");    mypojo.setId("18082013");    mypojo.setAge(5);    String jsonStringXML = mapper1.writeValueAsString(mypojo);    String jsonStringJSON = mapper2.writeValueAsString(mypojo);    // takes java class with def or customized constructors and creates JSON    System.out.println("XML is " + "\n" + jsonStringXML + "\n");    System.out.println("Json is " + "\n" + jsonStringJSON);}   }/***** MyPojo.java *****/   import com.fasterxml.jackson.annotation.JsonIgnore;   import com.fasterxml.jackson.annotation.JsonProperty;   import com.fasterxml.jackson.annotation.JsonPropertyOrder;   import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;  @JsonPropertyOrder({ "name", "age", "id", "note" })  @JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "myPOJO")   public class MyPojo {@JsonProperty("_id")private String id;private String name;private int age;@JsonIgnoreprivate String note;public String getNote() {    return note;}public void setNote(String note) {    this.note = note;}public String getId() {    return id;}public void setId(String id) {    this.id = id;}public String getName() {    return name;}public void setName(String name) {    this.name = name;}public int getAge() {    return age;}public void setAge(int age) {    this.age = age;} }

RESULT

XML
        <myPOJO xmlns="urn:stackify:jacksonxml">        <name xmlns="">Dhani</name>         <age xmlns="">5</age>         <_id xmlns="">18082013</_id>         </myPOJO>
JSON
     {      "name" : "Dhani",       "age" : 5,       "_id" : "18082013"     }