honey92
honey92

Reputation: 43

Can I convert JSON object to JAXB object?

I have been working with JAXB objects. Now my requirement is using JSON instead of XML. Can someone help me out with the logic?

I can't read replace entire code base with json objects. Is there any way to convert these json to jaxb objects?

Thanks in advance.

xml:

<skins>
    <skin id="lineNormal" type="style" displayname="Skin">
    <attributes datatype="string" value="one" name="bg_type" platform="general"/>
    <attributes datatype="string" value="5c5c5c00" name="background_color" platform="general"/>
    <attributes datatype="boolean" value="false" name="customcss" platform="general"/>
    <attributes datatype="string" value="00000000" name="border_color" platform="general"/>
    <attributes datatype="number" value="0" name="border_width" platform="general"/>
    <attributes datatype="string" value="plain" name="border_style" platform="general"/>
    <attributes datatype="number" value="0" name="border_radius" platform="general"/>
    <attributes datatype="string" value="Line" name="wtype" platform="general"/>
</skin>

JSON:

{
  "lineNormal": {
    "wType": "Button",
    "bg_type": "one",
    "border_color": "00000000",
    "border_radius": 0,
    "border_style": "plain",
    "border_width": 0,
    "border_type": 0,
    "font_color": "00000000",
    "font_size": 100,
    "font_weight": "normal",
    "background_color : "xxx"
    }
}

My class file for fetching application graph from the above xml is: `

@XmlRootElement(name = "application")
public class Application implements Serializable
{
    private ArrayList <Container> containers;
    private Skins skins = new Skins();
    private String name;
    private String id;
    @XmlElement(name = "container")
    public ArrayList <Container> getContainers() {
        return containers;
    }

    /**
     * Sets teh containers collection
     * @param containers
     */
    public void setContainers(ArrayList <Container> containers) {
        this.containers = containers;
    }

    /**
     * 
     * @return
     */
    @XmlElement(name = "skins")
    public Skins getSkins() 
    {
        return skins;
    }

    /**
     * 
     * @param skins
     */
    public void setSkins(Skins skins) 
    {
        this.skins = skins;
    }

    @XmlElement(name = "globals")
    public Global getGlobal() {
        return global;
    }

    public void setGlobal(Global global) {
        this.global = global;
    }



    @XmlAttribute(name="id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

     @XmlElement(name = "attributes")
    public ArrayList<Attributes> getAttributes() {
        return attributes;
    }`

//skin class is :: `

public class Skins implements Serializable{

    private ArrayList <Skin> skins = new ArrayList();

    @XmlElement(name = "skin")
    public ArrayList<Skin> getSkinList() 
    {
        return skins;
    }

    /**
     * 
     * @param skins
     */
    public void setSkinList(ArrayList<Skin> skins) 
    {
        this.skins = skins;
    }

}

`

Upvotes: 0

Views: 7613

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209112

"Can I convert JSON object to JAXB object?"

The jackson-module-jaxb-annotations for Jackson will allow you to do this.

This Jackson extension module provides support for using JAXB (javax.xml.bind) annotations as an alternative to native Jackson annotations. It is most often used to make it easier to reuse existing data beans that used with JAXB framework to read and write XML.

With Maven, you just need this dependency (it will pull in a couple other Jackson core dependencies):

<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-jaxb-annotations</artifactId>
  <version>2.4.0</version>
</dependency>

Having this dependency, you simply need to register the JAXB module with the ObjectMapper

JaxbAnnotationModule module = new JaxbAnnotationModule();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);

Then you can read the JSON into your JAXB objects.

RootObject root = objectMapper.readValue(json, RootObject.class);

This is not a sure-fire solution for all use cases, as the module does not support all JAXB annotations. See here for supported annotations


  • You may also want to check out MOXy support for this, as seen here

Upvotes: 3

Related Questions