Reputation: 2829
Say we have a JSON structure that looks like this:
{
"field1": "val1",
"field2": "v2",
"f3": "v3",
"f4": "v4",
"arrayOfStuff": [
{
"f5": "v5",
....
"f10": "v10"
}
],
"attributes": [
{"att1": "att1"},
{"att2": "attr2"},
{"att3": "att3"}
],
"options": [
"ignoreMismatchFile"
]
}
And our matching java class looks like:
public class Message {
@IsUniqueId
private String field1; //
private String field2;
private String field3;
private String field4;
private List<AnotherObject> f5;
@JsonProperty("attributes")
private LinkedHashMap<String, String> attributes;
private List<String> options;
....
}
The parsing code looks like:
protected Message loadSavedMessageAsMessageObject(String path) throws IOException {
File file = ResourceUtils.getFile(path);
if (file.exists()) {
ObjectMapper mapper = this.getObjectMapper();
return mapper.readValue(file, Message.class);
}
return null;
}
We tried different ways of accomplishing this, originally we tried to have attributes as private List<MessageAttribute> attributes;
but that has not worked either (we switched to the Map based on Another answer - doesn't work)
Our goal is to keep Attributes a dynamic, not hard coded list of attributes.
This is how the MessageAttribute
class looked like:
public class MessageAttribute {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
The exception we currently get is:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_OBJECT token
at [Source: (File); line: 32, column: 3] (through reference chain: com.org.Message["attributes"])
Upvotes: 5
Views: 3324
Reputation: 40058
Corresponding Message
POJO to above JSON is in wrong format, i made couple of changes attributes
should be List of Map
and list of AnotherObject
should point to
arrayOfStuff
public class Message {
@IsUniqueId
private String field1; //
private String field2;
private String field3;
private String field4;
private List<AnotherObject> arrayOfStuff; //or you can have List<Map<String,String>> arrayOfStuff
@JsonProperty("attributes")
private List<LinkedHashMap<String, String>> attributes; // this is list of map objects
private List<String> options;
....
}
Upvotes: 2