|
1 | 1 | package com.fasterxml.jackson.datatype.guava.deser;
|
2 | 2 |
|
3 |
| -import com.fasterxml.jackson.databind.JsonMappingException; |
4 |
| -import com.fasterxml.jackson.databind.type.TypeFactory; |
5 | 3 | import java.io.IOException;
|
6 | 4 | import java.util.Collection;
|
7 | 5 | import java.util.List;
|
8 | 6 |
|
| 7 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 8 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 9 | + |
9 | 10 | import com.fasterxml.jackson.core.JsonParser;
|
| 11 | +import com.fasterxml.jackson.core.JsonToken; |
10 | 12 | import com.fasterxml.jackson.databind.BeanProperty;
|
11 | 13 | import com.fasterxml.jackson.databind.DeserializationContext;
|
12 | 14 | import com.fasterxml.jackson.databind.JavaType;
|
13 | 15 | import com.fasterxml.jackson.databind.JsonDeserializer;
|
14 | 16 | import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
|
15 | 17 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
16 | 18 | import com.fasterxml.jackson.databind.type.LogicalType;
|
| 19 | + |
17 | 20 | import com.google.common.collect.ImmutableRangeSet;
|
18 | 21 | import com.google.common.collect.Range;
|
19 | 22 | import com.google.common.collect.RangeSet;
|
@@ -95,10 +98,35 @@ public RangeSet<Comparable<?>> deserialize(JsonParser p, DeserializationContext
|
95 | 98 | final Collection<?> ranges = (Collection<?>) _deserializer.deserialize(p, ctxt);
|
96 | 99 | ImmutableRangeSet.Builder<Comparable<?>> builder = ImmutableRangeSet.builder();
|
97 | 100 | for (Object ob : ranges) {
|
| 101 | + if (ob == null) { |
| 102 | + _tryToAddNull(p, ctxt, builder); |
| 103 | + continue; |
| 104 | + } |
98 | 105 | @SuppressWarnings("unchecked")
|
99 | 106 | Range<Comparable<?>> range = (Range<Comparable<?>>) ob;
|
100 | 107 | builder.add(range);
|
101 | 108 | }
|
102 | 109 | return builder.build();
|
103 | 110 | }
|
| 111 | + |
| 112 | + /** |
| 113 | + * Some/many Guava containers do not allow addition of {@code null} values, |
| 114 | + * so isolate handling here. |
| 115 | + * |
| 116 | + * @since 2.17 |
| 117 | + */ |
| 118 | + protected void _tryToAddNull(JsonParser p, DeserializationContext ctxt, |
| 119 | + ImmutableRangeSet.Builder<Comparable<?>> builder) |
| 120 | + throws IOException |
| 121 | + { |
| 122 | + // Ideally we'd have better idea of where nulls are accepted, but first |
| 123 | + // let's just produce something better than NPE: |
| 124 | + try { |
| 125 | + builder.add(null); |
| 126 | + } catch (NullPointerException e) { |
| 127 | + ctxt.handleUnexpectedToken(_valueType, JsonToken.VALUE_NULL, p, |
| 128 | + "Guava `RangeSet` does not accept `null` values"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
104 | 132 | }
|
0 commit comments