XML wrapped list to JSON array via Jettison and JAXB

可紊 提交于 2019-12-21 12:33:14

问题


I'm using JAXB to marshal an annotated object to XML in the form:

  <channels>
     <channel>Test A</channel>
     <channel>Test B</channel>
  </channels>

I want to marshal this to JSON instead using JAXB (ala http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html) but it marshals to something like the following:

  "channels" : {
    "channel" : [ "Test A", "Test B" ]
  },

Really I want it to marshal into the following form:

  "channels" : {
    {"Test A"}, {"Test B"}
  },

How can I do this? Is it the right thing to do?


回答1:


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

Below is how you could support this use case using the JSON-binding in EclipseLink JAXB (MOXy).

Java Model (Root)

Below is the Java model that I will use for this example.

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

@XmlRootElement
public class Root {

    private List<String> channels = new ArrayList<String>();

    @XmlElementWrapper
    @XmlElement(name="channel")
    public List<String> getChannels() {
        return channels;
    }

}

Specify MOXy as the JAXB Provider (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: ):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo Code

In the demo code below we will output the same instance to both XML and JSON.

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(Root.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Root root = new Root();
        root.getChannels().add("Test A");
        root.getChannels().add("Test B");

        // Output XML
        marshaller.marshal(root, System.out);

        // Output JSON
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
        marshaller.marshal(root, System.out);
    }

}

Output

Below is the output from running the demo code:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <channels>
      <channel>Test A</channel>
      <channel>Test B</channel>
   </channels>
</root>
{
   "channels" : [ "Test A", "Test B" ]
}

For More Information

  • http://blog.bdoughan.com/2013/03/binding-to-json-xml-handling-collections.html



回答2:


The simplest way is probably to convert your JAXB model to an adapted JSON model.

You would then do:

  1. instantiate the JAXB model
  2. convert it to the JSON model
  3. marshal the JSON model

Depending on how you instantiate your JAXB model, you may want to directly instantiate the JSON model instead.



来源:https://stackoverflow.com/questions/16612626/xml-wrapped-list-to-json-array-via-jettison-and-jaxb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!