I'm trying to deserialize a JSON string into a ConcurrentHashMap object and I'm getting errors because my JSON contains properties with null values, but ConcurrentHashMap does not accept null values. Here is the fragment of code:
我正在尝试将JSON字符串反序列化为ConcurrentHashMap对象,并且我收到错误,因为我的JSON包含具有空值的属性,但ConcurrentHashMap不接受空值。这是代码片段:
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonString, ConcurrentHashMap.class);
Is there a way to ignore properties with null values during deserialization? I know that we can ignore these properties during serialization:
有没有办法在反序列化期间忽略具有空值的属性?我知道在序列化过程中我们可以忽略这些属性:
mapper.setSerializationInclusion(JsonInclude.NON_NULL);
But what about deserialization process?
但是反序列化过程呢?
2 个解决方案
#1
1
The following trick has worked for me:
以下技巧对我有用:
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"key1\": 1, \"key2\": null, \"key3\": 3}";
ConcurrentHashMap<String, Object> map = mapper.readValue(jsonString, new ConcurrentHashMap<String, Object>() {
@Override
public Object put(String key, Object value) {
return value != null ? super.put(key, value) : null;
}
}.getClass());
System.out.println(map); // {key1=1, key3=3}
The idea is to simply override ConcurrentHashMap.put()
method so that it ignores null
values that are to be added to the map.
我们的想法是简单地覆盖ConcurrentHashMap.put()方法,以便忽略要添加到地图的空值。
Instead of an anonymous inner class, you could create your own class that extends from ConcurrentHashMap
:
您可以创建自己的从ConcurrentHashMap扩展的类,而不是匿名内部类:
public class NullValuesIgnorerConcurrentHashMap<K, V>
extends ConcurrentHashMap<K, V> {
@Override
public V put(K key, V value) {
return value != null ? super.put(key, value) : null;
}
}
Then you would use this class to deserialize to a ConcurrentHashMap
:
然后,您将使用此类反序列化为ConcurrentHashMap:
ConcurrentHashMap<String, Object> map =
mapper.readValue(jsonString, NullValuesIgnorerConcurrentHashMap.class);
System.out.println(map); // {key1=1, key3=3}
With this approach, the returned map would never throw NullPointerException
on put()
when given a null
value.
使用这种方法,返回的映射在给定空值时永远不会在put()上抛出NullPointerException。
#2
3
There may be a better way but a workaround would be:
可能有更好的方法,但解决方法是:
Map<String, Object> map = mapper.readValue(jsonString, HashMap.class);
map.values().removeIf(o -> o == null);
return new ConcurrentHashMap<> (map);
#1
1
The following trick has worked for me:
以下技巧对我有用:
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"key1\": 1, \"key2\": null, \"key3\": 3}";
ConcurrentHashMap<String, Object> map = mapper.readValue(jsonString, new ConcurrentHashMap<String, Object>() {
@Override
public Object put(String key, Object value) {
return value != null ? super.put(key, value) : null;
}
}.getClass());
System.out.println(map); // {key1=1, key3=3}
The idea is to simply override ConcurrentHashMap.put()
method so that it ignores null
values that are to be added to the map.
我们的想法是简单地覆盖ConcurrentHashMap.put()方法,以便忽略要添加到地图的空值。
Instead of an anonymous inner class, you could create your own class that extends from ConcurrentHashMap
:
您可以创建自己的从ConcurrentHashMap扩展的类,而不是匿名内部类:
public class NullValuesIgnorerConcurrentHashMap<K, V>
extends ConcurrentHashMap<K, V> {
@Override
public V put(K key, V value) {
return value != null ? super.put(key, value) : null;
}
}
Then you would use this class to deserialize to a ConcurrentHashMap
:
然后,您将使用此类反序列化为ConcurrentHashMap:
ConcurrentHashMap<String, Object> map =
mapper.readValue(jsonString, NullValuesIgnorerConcurrentHashMap.class);
System.out.println(map); // {key1=1, key3=3}
With this approach, the returned map would never throw NullPointerException
on put()
when given a null
value.
使用这种方法,返回的映射在给定空值时永远不会在put()上抛出NullPointerException。
#2
3
There may be a better way but a workaround would be:
可能有更好的方法,但解决方法是:
Map<String, Object> map = mapper.readValue(jsonString, HashMap.class);
map.values().removeIf(o -> o == null);
return new ConcurrentHashMap<> (map);