Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Nested classes in JSon and removing unnecessary levels
Nested classes in JSon and removing unnecessary levels [message #1041778] Mon, 15 April 2013 15:10
Stuart Rivero is currently offline Stuart Rivero
Messages: 1
Registered: April 2013
Junior Member
I'm using the "eclipselink.media-type"="application/json" on the JaxBMarshaller to generate JSon from classes with XML annotations.

I have a wrapper class called Feed which holds varying types of FeedContent. In the case I'm looking at, the type is a Categories class that inherits from FeedContent, which has an array of Category objects.

The JSon I am getting is:

{
    "categories": {
        "category": [
            {
                "id": "arts"
            },
            {
                "id": "food"
            },
         ]
    }
}


whereas I want the following (no category element):

{
     "categories": [
            {
                "id": "arts"
            },
            {
                "id": "food"
            },
         ]
}



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "feedContent"
})
@XmlRootElement(name = "feed")
public class Feed {

    ...
    @XmlElements({
            ...
            @XmlElement(name = "categories", type = Categories.class)
           ...
    })
    private FeedContent feedContent;
    ...
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Categories extends FeedContent {

    @XmlElement(name = "category")
    private List<Category> categories = new ArrayList<Category>();

    public void addCategory(Category category) {
        this.categories.add(category);
    }

}

@XmlAccessorType(XmlAccessType.FIELD)
public class Category {
    
    private String id;

    public Category() {
    }

    public String getId() {
        return id;
    }
}


Is there a way to achieve the JSon I require? The only way I'm managed this so far is to have a Categories field in Feed but this makes the design less flexible.

I tested it In XML and the feed is being generated how I would expect:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<feed>
    <categories>
        <category>
            <id>arts</id>
        </category>
        <category>
            <id>food</id>
        </category>
 </categories>
</feed> 

Previous Topic:Beginner's Problem / strange behavior
Next Topic:[EclipseLink.Examples.Employee] How to configure java:global/employeeDS on GF4
Goto Forum:
  


Current Time: Wed Sep 24 02:45:03 GMT 2014

Powered by FUDForum. Page generated in 0.01568 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software