1
1
package com .fasterxml .jackson .datatype .guava .deser .cache ;
2
2
3
+ import java .io .IOException ;
4
+
5
+ import com .google .common .cache .Cache ;
6
+
3
7
import com .fasterxml .jackson .core .JsonParser ;
4
8
import com .fasterxml .jackson .core .JsonToken ;
9
+
5
10
import com .fasterxml .jackson .databind .*;
6
11
import com .fasterxml .jackson .databind .deser .ContextualDeserializer ;
7
12
import com .fasterxml .jackson .databind .deser .NullValueProvider ;
10
15
import com .fasterxml .jackson .databind .jsontype .TypeDeserializer ;
11
16
import com .fasterxml .jackson .databind .type .LogicalType ;
12
17
import com .fasterxml .jackson .databind .type .MapLikeType ;
13
- import com .google .common .cache .Cache ;
14
-
15
- import java .io .IOException ;
18
+ import com .fasterxml .jackson .databind .util .ClassUtil ;
16
19
17
20
public abstract class GuavaCacheDeserializer <T extends Cache <Object , Object >>
18
21
extends StdDeserializer <T > implements ContextualDeserializer
@@ -146,6 +149,11 @@ private T deserializeContents(JsonParser p, DeserializationContext ctxt)
146
149
} else {
147
150
value = elementDeserializer .deserialize (p , ctxt );
148
151
}
152
+ if (value == null ) {
153
+ _tryToAddNull (p , ctxt , cache , key );
154
+ continue ;
155
+ }
156
+
149
157
cache .put (key , value );
150
158
}
151
159
return cache ;
@@ -157,4 +165,26 @@ private void expect(JsonParser p, JsonToken token) throws IOException {
157
165
p .currentLocation ());
158
166
}
159
167
}
168
+
169
+ /**
170
+ * Some/many Guava containers do not allow addition of {@code null} values,
171
+ * so isolate handling here.
172
+ *
173
+ * @since 2.17
174
+ */
175
+ protected void _tryToAddNull (JsonParser p , DeserializationContext ctxt ,
176
+ T cache , Object key )
177
+ throws IOException
178
+ {
179
+ // Ideally we'd have better idea of where nulls are accepted, but first
180
+ // let's just produce something better than NPE:
181
+ try {
182
+ cache .put (key , null );
183
+ } catch (NullPointerException e ) {
184
+ ctxt .handleUnexpectedToken (_valueType , JsonToken .VALUE_NULL , p ,
185
+ "Guava `Cache` of type %s does not accept `null` values" ,
186
+ ClassUtil .classNameOf (cache ));
187
+ }
188
+ }
189
+
160
190
}
0 commit comments