Jackson deserialize “” as an empty list

前端 未结 1 1585
醉酒成梦
醉酒成梦 2020-12-10 18:02

I have a JSON string that marks empty lists as \"\" instead of []. So for example, if I have an object with no children, I\'ll receive a string lik

相关标签:
1条回答
  • 2020-12-10 18:59

    Couple of options; first, you want to enable `ACCEPT_EMPTY_STRING_AS_NULL_OBJECT':

    mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    

    so that empty String becomes null. And if you want it to get converted to actual empty List, override setter:

    public void setChildren(List<Child> c) {
        if (c == null) {
           children = Collections.emptyList();
        } else {
           chidlren = c;
        }
    }
    
    0 讨论(0)
提交回复
热议问题