英文:
Read empty string from JSON to empty List<...>
问题
I have a List<String> field in my POJO. List so:
public class Student {
private List<String> courseNames;
...
}
When the in-coming JSON (from 3rd-party) contains an array (like the following), it is not an issue.
{
"courseNames": ["abc", "xyz"]
}
But the 3rd party app sometimes sends an empty array. Like so:
{
"courseNames": ""
}
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<List<String>> {
@Override
public List<String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
if(jsonParser.getCurrentToken() != JsonToken.START_ARRAY) {
return new ArrayList<>();
}
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<String> courseNames;
...
}
When the in-coming JSON (from 3rd-party) contains an array (like the following), it is not an issue.
{
"courseNames": ["abc", "xyz"]
}
But the 3rd party app sometimes sends an empty array. Like so:
{
"courseNames": ""
}
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<List<String>> {
@Override
public List<String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
if(jsonParser.getCurrentToken() != JsonToken.START_ARRAY) {
return new ArrayList<>();
}
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<String> courseNames;
}
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
//a non empty courseNames array
String courseNames = "{\"courseNames\": [\"abc\", \"xyz\"]}";
Student student = mapper.readValue(courseNames, Student.class);
//ok, it prints {"courseNames":["abc","xyz"]}
System.out.println(mapper.writeValueAsString(student));
//an empty courseNames array
courseNames = "{\"courseNames\": []}";
student = mapper.readValue(courseNames, Student.class);
//ok, it prints the empty courseNames array
System.out.println(mapper.writeValueAsString(student));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论