I'm not sure if this is at all possible as it contradicts the semantics of @XmlPath itself, but if yes, please do help me with this.
I'm getting this json by calling some external API from my API implementation
{
"title": "Forrest Gump",
"writingCredits": {
"novel": "Winston Groom",
"screenplay": "Eric Roth"
},
"directedBy": "Robert Zemeckis",
"casts": ["Tom Hanks", "Rebecca Williams", "Sally Field"],
"releaseDate": "1994-06-07T00:00:00.000Z",
"producedBy": {
"producers": ["Wendy Finerman", "Steve Starkey"],
"co-producers": ["Charles Newrith"]
}
}
and mapping this to following POJO that'll be returned as resource from my own API
public class Film
{
private String title;
@XmlPath("writingCredits/screenplay/text()")
private String writer;
@XmlPath("directedBy/text()")
private String director;
@XmlPath("casts[0]/text()")
private String actor;
@XmlJavaTypeAdapter(DateUnmarshaller.class)
@XmlPath("releaseDate/text()")
private int releaseYear;
@XmlPath("producedBy/producers[0]/text()")
private String producer;
private String category;
//Commented getters and setters to save space
}
Here I'm able to map the necessary info to my POJO using MOXy but facing problem while marshalling the same. While marshalling it retains the original nested structure of the received json though my POJO structure is not nested, I mean it is not referring to any other POJO. Can I get something like this when I marshall my POJO to json:
{
"title": "Forrest Gump",
"writer": "Eric Roth",
"director": "Robert Zemeckis",
"actor": "Tom Hanks",
"releaseYear": 1994,
"producer": "Wendy Finerman"
}
but instead it showed up like this:
{
"title": "Forrest Gump",
"writingCredits": {
"screenplay": "Eric Roth"
},
"directedBy": "Robert Zemeckis",
"casts": "Tom Hanks",
"releaseDate": 1994,
"producedBy": {
"producers": "Wendy Finerman"
}
}
So, is there any way to deal with this?
And one more thing, for the category attribute I wanna decide its value if a particular json entry is present in received json, e.g. if the received json contains a json entry like
"animationInfo": {
"studio" : "pixar",
"some extra info" : [],
"and some more" : {}
}
then category should be set to "animation". Do I need to write one more XmlAdapter for it?
Thank you..!