java无法将null Jackson节点反序列化到集合
当我的JsonNode
没有值时,我正在测试deserializing
到collection
对象。我希望对象等于null
这就是我要尝试的:
public class ImmutableDiscoveredUrlDeserializer extends JsonDeserializer<ImmutableDiscoveredUrl> {
String parentUrl;
Double parentUrlSentiment;
Set<String> childUrls;
Boolean isParentVendorUrl;
Map<TagClassification, Set<String>> parentUrlArticleTags;
/*
* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
public ImmutableDiscoveredUrl deserialize(JsonParser p, DeserializationContext ctx)
throws IOException {
JsonNode node = p.readValueAsTree();
parentUrl = defaultIfNull(node.get("parentUrl").asText(), null);
childUrls = defaultIfNull(parseChildUrls(node), emptySet());
isParentVendorUrl = defaultIfNull(Boolean.valueOf(node.get("isParentVendorUrl").asText()), null);
parentUrlArticleTags = defaultIfNull(parseArticleTags(node.get("parentUrlArticleTags")), emptyMap());
return ImmutableDiscoveredUrl.discoveredUrl().parentUrl(parentUrl)
.parentUrlSentiment(parentUrlSentiment).childUrls(childUrls)
.isParentVendorUrl(isParentVendorUrl).parentUrlArticleTags(parentUrlArticleTags);
}
private Set<String> parseChildUrls(JsonNode node) throws IOException {
ObjectMapper tagsMapper = new ObjectMapper();
return tagsMapper.convertValue(node, new TypeReference<Set<String>>() {});
}
private Map<TagClassification, Set<String>> parseArticleTags(JsonNode node) throws IOException {
ObjectMapper tagsMapper = new ObjectMapper();
return tagsMapper.convertValue(node, new TypeReference<Set<String>>() {});
}
但我得到一个MismatchedInputException
,声明没有可映射的内容。如何让ObjectMapper
返回空值
# 1 楼答案
在我的情况下,我有一个小不同的问题,因为
这是我的POJO
问题是:我有一个以
set*
开头的util方法。将其重命名为setSsgs(Set<String> ssgs)
有助于解决该问题