5

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"])
2
  • have you considered the idea of writing a Custom Deserializer Commented Feb 1, 2019 at 19:46
  • Didnt get that far, and luckily no longer required :) Commented Feb 1, 2019 at 19:59

1 Answer 1

2

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;
  ....
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! With this answer, figured out we had the JSON wrong, so instead of double wrapping changed the attributes to be a single object with many dynamic fields, works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.