Home > OS >  How to deserialize json empty array
How to deserialize json empty array

Time:12-08

I have a JSON looks like the following:

{
  "name": "John",
  "age": 20,
  "skills": [
  ]
}

the skills if it's not empty looks like the following:

{
   "skills": [
       "skill_1": {
   }, 
   "skill_2": {
   }]
}

and I need to deserialize this JSON to POJO:

public class Profile {
    public String name;
    public int age;
    @JsonDeserialize(using = SkillsMapDeserializer.class)
    public Map<String, Skill> skills;
}
public class Skill {
    public String skillName;
    public int age;
}

and my SkillsMapDeserializer looks like the following:

public class SkillsMapDeserializer extends JsonDeserializer<Map<String, Skill>> {
    @Override
    public Map<String, Skill> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        final Map<String, Skill> map = jsonParser.readValueAs(new TypeReference<Map<String, Skill>>() {
        });
        if (map == null) {
            return new HashMap<>();
        }
        return map;
    }
}

if the skills aren't empty all works fine, but if the skills are an empty array I get an exception that looks like the following:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap<java.lang.Object,java.lang.Object>` out of START_ARRAY token

How can I fix this issue?

CodePudding user response:

From your json data, it seem skills is an array of object.

"skills": [],
 "skills": [
   "skill_1": {}, 
   "skill_2": {}
 ]

But your java define it as Map

public Map<String, Skill> skills;

That's why you got an exception when trying convert array to map directly. If you can't change the POJOs Profile, you should have an mediate step to convert list to Map.

public class SkillsMapDeserializer extends JsonDeserializer<Map<String, Skill>> {
    @Override
    public Map<String, Skill> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        final List<Map<String,Skill>> skills = jsonParser.readValueAs(new TypeReference<List<Map<String,Skill>>>>() {
        });
        return functionConvertListToMapWithParam(skills);
    }
}

CodePudding user response:

skills is not a map. it should be list of objects. try to modify your POJO like below:-

public class Profile {

    @JsonProperty("name")
    private String name;
    @JsonProperty("age")
    private Integer age;
    @JsonProperty("skills")
    private List < Object > skills = null;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("age")
    public Integer getAge() {
        return age;
    }

    @JsonProperty("age")
    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonProperty("skills")
    public List < Object > getSkills() {
        return skills;
    }

    @JsonProperty("skills")
    public void setSkills(List < Object > skills) {
        this.skills = skills;
    }

}
  • Related