0

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;
    }

}

`

honey92
  • 43
  • 2
  • 9

1 Answers1

3

"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
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • but the problem is i dont have specific id name and all.I just have skin1,and its data,skin2 and its data and so on.skin1 skin2..are different different ids.this implementation is not found anywhere.could you please help me to implement this – honey92 Oct 08 '14 at 04:49
  • We'll need to at least the see the JAXB classes you are working with, or an xsd from with we can generate the JAXB classes – Paul Samsotha Oct 08 '14 at 05:25
  • here is one of my class file. @XmlRootElement(name = "application") public class Application implements Serializable { private ArrayList containers; private Skins skins = new Skins(); @XmlElement(name = "container") public ArrayList getContainers() { return containers; }//such setters and getters }and skins is another class for retrieving all skins,and den skin class for each single skin – honey92 Oct 08 '14 at 05:31
  • Can you please edit your original post with it, instead of as a comment – Paul Samsotha Oct 08 '14 at 05:32
  • You may need to reformat how the json is sent. Is that allowed for this requirement? – Paul Samsotha Oct 08 '14 at 05:57
  • ill be provided with that simple json.can i take it and reformat? i mean reading the object and then reformatting – honey92 Oct 08 '14 at 06:00
  • Here's the problem. When you do this `"lineNormal": {..`, you're saying that the is an attribute `Skin lineNormal;` in the class. But that attribute will be changing and will not be constant. I'll keep hacking at it, as I have time. – Paul Samsotha Oct 08 '14 at 06:25
  • Also your xml attribute names have no relevance to the attribute names in the json – Paul Samsotha Oct 08 '14 at 06:28
  • My issue is resolved @peeskillet.I recreated my object with the read json values. and yes those are of different names actually – honey92 Oct 09 '14 at 00:39