Json 如何在jackson中保留具有空值的映射

Json 如何在jackson中保留具有空值的映射,json,jackson,Json,Jackson,问题: HashMap testMap = new HashMap(); testMap.put("Key1", "Value1"); testMap.put("Key2", null); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enableDefaultTyping(); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FIN

问题:

HashMap testMap = new HashMap();
testMap.put("Key1", "Value1");
testMap.put("Key2", null);  

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enableDefaultTyping(); 
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.WRAPPER_OBJECT);          
objectMapper.setVisibility(PropertyAccessor.SETTER,JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.GETTER,JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD,JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.IS_GETTER,JsonAutoDetect.Visibility.NONE);          
//objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);

String jsonString = objectMapper.writeValueAsString(testMap);
System.out.println(jsonString);
a) 当我使用上面的语句并执行时,它抛出下面的异常

objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
b) 当我使用上面的语句并执行时,它运行良好,但结果如下所示

java.lang.UnsupportedOperationException: Type id handling not implemented for type java.lang.Object
    at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:142)
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeTypedFields(MapSerializer.java:798)

objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
但是从json中删除了Key2。那么,如何保留空值呢

{"java.util.HashMap":{"Key1":"Value1"}}
c) 当我使用上述语句并使用jackson-databind.jar(版本2.7)执行时 它还可以工作并保留空值。 但是JacksonDatabindVersion2.7支持JDK7以后的版本,那么我如何在JDK6版本中使用同样的东西呢


让我知道克服它的任何其他选择。

我在jackson-databind.jar(版本2.7.2)中验证了jdk 6,并且工作正常

您是否尝试过编写自定义序列化程序?
objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);