I'm testing deserializing
to a collection
object when my JsonNode
no value. I want the object to be equal to null
.
This is what I'm trying:
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>>() {});
}
But I get a MismatchedInputException
, stating that there's no content to map. How do I get the ObjectMapper
to return a null?