I have below json:
"[{\"movieName\":\"A\",\"Leadactor\":\"\",\"leadActress\":\"\",\"movieTitle\":\"\",\"hero\":\"\",\"heroine\":\"\",\"source\":\"IMDB\"}," +
"{\"movieName\":\"\",\"Leadactor\":\"\",\"leadActress\":\"\",\"movieTitle\":\"B\",\"hero\":\"B1\",\"heroine\":\"B2\",\"source\":\"Netflix\"}," +
"{\"movieName\":\"C\",\"Leadactor\":\"C1\",\"leadActress\":\"C2\",\"movieTitle\":\"\",\"hero\":\"\",\"heroine\":\"\",\"source\":\"IMDB\"}," +
"{\"movieName\":\"D\",\"Leadactor\":\"D1\",\"leadActress\":\"D2\",\"movieTitle\":\"\",\"hero\":\"\",\"heroine\":\"\",\"source\":\"IMDB\"}," +
"{\"movieName\":\"\",\"Leadactor\":\"\",\"leadActress\":\"\",\"movieTitle\":\"E\",\"hero\":\"E1\",\"heroine\":\"E2\",\"source\":\"Netflix\"}]";
I am using jackson parser to map it to a class:
I want movieName and movieTitle to map into Name property in the java class. So i wrote the below class:
public static class MovieData {
@JsonProperty("Name")
private String name;
@JsonSetter({"movieName"})
private void setMovieName(final String name) {
if((name != null) && (! name.equals(""))) {
setNameInternal(name);
}
}
@JsonSetter("movieTitle")
private void setMovieTitle(final String name) {
if((name != null) && (! name.equals(""))) {
setNameInternal(name);
}
}
private void setNameInternal(final String name) {
this.name = name;
}
}
In my real json there are so many fields like movieName, movieTitle which i want to normalize into a common name.
Is there any simple syntax like the below which can reduce code duplication:
public static class MovieData {
@JsonProperty("Name")
private String name;
@JsonSetter(value = { "movieName", "movieTitle" })
private void setName(final String name) {
if((name != null) && (! name.equals(""))) {
this.name=name;
}
}
}
The above code gave me error on jsonSetter:
Type mismatch: cannot convert from String[] to String.
EDIT
If Jackson doesn't support it, can GSON Support this operation.
Thanks
Moviewith an attributename, which contains the value of either"movieName"or"movieTitle"? And if, let's say,"movieName"has a value,"movieTitle"will be always emtpty?