Reputation: 1949
I need deserialize a string(json) to a arraylist inside a model. I'm using the Jackson-Annotation library to do this. Anyone can help me?
I've tried this, but doesn't work:
@JsonDeserialize(as = Model.class)
private ArrayList<Model> model;
or:
@JsonDeserialize(as = ArrayModel.class) //ArrayModel extends arrayList<Model>
private ArrayList<Model> model;
Sample:
public class Model extends BaseModel {
@JsonProperty("id")
private int id;
@JsonDeserialize(as = ModelTwo.class)
private ArrayList<ModelTwo> modelTwo;
public ArrayList<ModelTwo> getModelTwo() {
return modelTwo;
}
public void setModelTwo(ArrayList<ModelTwo> modelTwo) {
this.modelTwo = modelTwo;
}
}
Upvotes: 1
Views: 808
Reputation: 1949
I've solved this!
You need say the type of Object and the type of content. After this, you need create a new Json with properties on params.
On first model:
@JsonProperty("property")
@JsonDeserialize(as=ArrayList.class, contentAs=ModelTwo.class)
private List<ModelTwo> modelsTwo;
On second model:
@JsonCreator
public ModelTwo(
@JsonProperty("id") int id,
@JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
Upvotes: 1