Is there a library to convert Java POJOs to and fr

2019-01-21 17:05发布

I have an object graph that I would like to convert to and from JSON and XML, for the purposes of creating a REST-style API. It strikes me that someone must have done this already, but a quick search using Google and Stack Overflow reveals nothing.

Does anyone know of a suitable (Apache or equivalent license preferred) library to do this?

标签: java xml json pojo
10条回答
爷的心禁止访问
2楼-- · 2019-01-21 17:29

Json-lib is licensed under the Apache 2.0 license.

It can also transform JSON objects to XML, but you'd need to convert your POJOs to JSON through it first.

查看更多
何必那么认真
3楼-- · 2019-01-21 17:34

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

EclipseLink JAXB (MOXy) supports mapping a single object model to both XML and JSON with the same metadata:

License Information


DOMAIN MODEL

Below is the domain model we will use for this example. For this example I'm just using the standard JAXB (JSR-222) annotations which have are available in the JDK/JRE since Java SE 6.

Customer

package forum658936;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    String firstName;

    @XmlElement(nillable=true)
    String lastName;

    @XmlElement(name="phone-number")
    List<PhoneNumber> phoneNumbers;

}

PhoneNumber

package forum658936;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {

    @XmlAttribute
    int id;

    @XmlValue
    String number;

}

jaxb.properties

To specify 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

XML

input.xml

This is the XML that our demo code will read in and convert to domain objects.

<?xml version="1.0" encoding="UTF-8"?>
<customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <firstName>Jane</firstName>
    <lastName xsi:nil="true"/>
    <phone-number id="123">555-1234</phone-number>
</customer>

Things to note about the XML:


JSON

Output

Below is the JSON that was output by running the demo code.

{
   "firstName" : "Jane",
   "lastName" : null,
   "phone-number" : [ {
      "id" : 123,
      "value" : "555-1234"
   } ]
}

Things to note about the JSON:

  • The null value is used to represent that the lastName is null. There is no presence of the xsi:nil attribute.
  • The collect of phone numbers is of size 1 and is correctly bound by square brackets. Many libraries incorrectly treat collections of size 1 as JSON objects.
  • The property of type int was correctly marshalled without quotes.
  • In the XML representation id was an attribute, but in the JSON representation there is not need for it to be specially represented.

DEMO CODE

In the demo code below we will convert an XML document to objects, and then convert those same instances to JSON.

Demo

MOXy doesn't just interpret JAXB annotations it is a JAXB implementation so the standard JAXB runtime APIs are used. JSON binding is enabled by specifying MOXy specify properties on the Marshaller.

package forum658936;

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum658936/input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.marshal(customer, System.out);
    }

}
查看更多
Juvenile、少年°
4楼-- · 2019-01-21 17:39

I think you may be looking for something similar to what is here: JSON.org Java section

查看更多
对你真心纯属浪费
5楼-- · 2019-01-21 17:40

There are almost literally hundreds. My favorites are GSON for POJO <-> JSON and castor-xml for POJO <-> XML.

As a bonus both are licensed under Apache License 2.0 style licenses.

查看更多
登录 后发表回答