读取 JSON 中的空字符串到空的 List<...>。

huangapple go评论70阅读模式
英文:

Read empty string from JSON to empty List<...>

问题

I have a List<String> field in my POJO. List so:

public class Student {
    private List&lt;String&gt; courseNames;
    ...
}

When the in-coming JSON (from 3rd-party) contains an array (like the following), it is not an issue.

{
    &quot;courseNames&quot;: [&quot;abc&quot;, &quot;xyz&quot;]
}

But the 3rd party app sometimes sends an empty array. Like so:

{
    &quot;courseNames&quot;: &quot;&quot;
}

In this case, I like to have my courseNames field populated as an empty list.

Looking at this post, I figured implementing a custom JsonDeserializer is one way to achieve this.

So I partially implemented it:

public class StringToEmptyListJsonDeserializer extends JsonDeserializer&lt;List&lt;String&gt;&gt; {

    @Override
    public List&lt;String&gt; deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
        if(jsonParser.getCurrentToken() != JsonToken.START_ARRAY) {
            return new ArrayList&lt;&gt;();
        }
        return // What to do here?
    }
}

Are there any suggestions on how to delegate to the default List<...> JsonDeserializer? Or are there better solutions?

(Translation in English):
You can delegate to the default List<...> JsonDeserializer like this:

return deserializationContext.readValue(jsonParser, List.class);

This will use Jackson's default deserialization for a List.

英文:

I have a List<String> field in my POJO. List so:

public class Student {
    private List&lt;String&gt; courseNames;
    ...
}

When the in-coming JSON (from 3rd-party) contains an array (like the following), it is not an issue.

{
    &quot;courseNames&quot;: [&quot;abc&quot;, &quot;xyz&quot;]
}

But the 3rd party app sometimes sends an empty array. Like so:

{
    &quot;courseNames&quot;: &quot;&quot;
}

In this case, I like to have my courseNames field populated as an empty list.

Looking at this post, I figured implementing a custom JsonDeserializer is one way to achieve this.

So I partially implemented it:

public class StringToEmptyListJsonDeserializer extends JsonDeserializer&lt;List&lt;String&gt;&gt; {

    @Override
    public List&lt;String&gt; deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
        if(jsonParser.getCurrentToken() != JsonToken.START_ARRAY) {
            return new ArrayList&lt;&gt;();
        }
        return // What to do here?
    }
}

Are there any suggestions on how to delegate to the default List<...> JsonDeserializer? Or are there better solutions?

答案1

得分: 1

您可以启用DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT功能,以允许将JSON中的空字符串值("")绑定为POJO和其他结构化值(Maps、Collections)的null。因此,该值将转换为非空列表或空列表,如下所示:

@Data
public class Student {
    private List<String> courseNames;
}

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// 非空的courseNames数组
String courseNames = "{\"courseNames\": [\"abc\", \"xyz\"]}";
Student student = mapper.readValue(courseNames, Student.class);
// 打印:{"courseNames":["abc","xyz"]}
System.out.println(mapper.writeValueAsString(student));
// 空的courseNames数组
courseNames = "{\"courseNames\": []}";
student = mapper.readValue(courseNames, Student.class);
// 打印空的courseNames数组
System.out.println(mapper.writeValueAsString(student));

这段代码演示了如何使用Jackson库的功能来处理空字符串值的JSON反序列化。

英文:

You can enable the DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT feature to allow JSON empty String value ("") to be bound as null for POJOs and other structured values (Maps, Collections). So the value will be converted either to a non empty list or an empty list like below:

@Data
public class Student {
    private List&lt;String&gt; courseNames;
}

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
//a non empty courseNames array 
String courseNames = &quot;{\&quot;courseNames\&quot;: [\&quot;abc\&quot;, \&quot;xyz\&quot;]}&quot;;
Student student = mapper.readValue(courseNames, Student.class);
//ok, it prints {&quot;courseNames&quot;:[&quot;abc&quot;,&quot;xyz&quot;]}
System.out.println(mapper.writeValueAsString(student));
//an empty courseNames array
courseNames = &quot;{\&quot;courseNames\&quot;: []}&quot;;
student = mapper.readValue(courseNames, Student.class);
//ok, it prints the empty courseNames array
System.out.println(mapper.writeValueAsString(student));

huangapple
  • 本文由 发表于 2023年3月21日 03:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定