I am using JSON Jackson 2.0 to write a data Model to file and then load it back into the application. I can successfully write the object into a string but I encounter the following error message when trying to marshal it back into a object:
No suitable constructor found for type [simple type, class ImportAttributeModel]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: user; line: 1, column: 111] (through reference chain: ImportTemplateModel[“modelList”])
This is a code snippet of my classes:
public class ImportTemplateModel
{
private Map<Integer, AttributeModel> m_modelList;
private GraphElementType m_type;
public ImportTemplateModel()
{
}
// getters & setters
}
public class AttributeModel
{
private String m_label;
private String m_key;
private ElementDefinition m_definition;
public AttributeModel(String label, String key, ElementDefinition def)
{
m_label = label;
m_key = key;
m_definition = def;
}
// getters & setters
}
Here is the calls I make to write & read the model:
// Write model
ObjectMapper mapper = new ObjectMapper();
ImportTemplateModel itm = new ImportTemplateModel()
// set model atttributes
...
//
try {
mapper.writeValue(new File(filepath), itm);
}
catch (IOException e) {
}
....
// Read Model
ObjectMapper mapper = new ObjectMapper();
try {
mapper.readValue(new File(filepath), ImportTemplateModel.class);
}
catch (IOException e) {
}
I am not sure what I am doing wrong at the moment… Is it because AttributeModel is not a POJO? or is it I can’t use Maps for this implementation? If so, how do I get around this?
It needs to have a no-argument constructor so Jackson can instantiate it.