无法将null Jackson节点反序列化为集合

时间:2017-08-04 14:06:35

标签: java json jackson objectmapper

deserializing无值时,我正在collectionJsonNode对象进行测试。我希望对象等于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返回null?

2 个答案:

答案 0 :(得分:2)

由于您已经拥有JsonNode,因此可以使用ObjectMapper#convertValue

@Test
public void converts_null_to_null() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree("{\"foo\":null}");
    JsonNode foo = jsonNode.get("foo");

    Set<String> result = mapper.convertValue(foo, new TypeReference<Set<String>>() {});

    assertNull(result);
}

请注意,如果您传入普通convertValue()Map 按预期工作。在您的情况下,您需要删除defaultIfNull并自行检查null

if (node.get("parentUrlArticleTags") !== null) {
    parentUrlArticleTags = parseArticleTags(node.get("parentUrlArticleTags"));
}

答案 1 :(得分:0)

就我而言,我遇到了一个与众不同的问题

Cannot deserialize instance of `java.util.HashSet` out of START_OBJECT token
 at [Source: (String)"{"bbc":"firstbckt","ssg":{"751fad0":"751fad0","be0eb99":"be0eb99"}}"

这是我的POJO

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("serial")
public class BulkUploadRsp implements Serializable {
    private String bbc;
    private Map<String, String> ssgs = Maps.newHashMap();
    //..
    public void setSsgs(Set<String> ssgs) {
        this.ssgs = ssgs.stream().collect(Collectors.toMap(ssg -> ssg, ssg -> ssg));
    }
}

问题是:我有一个以set*开头的util方法。将其重命名为setSsgs(Set<String> ssgs)有助于解决该问题