-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Guava] Suppport simple deserialization of
Cache
(#116)
- Loading branch information
1 parent
2a8bd8c
commit a1bbc87
Showing
4 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
guava/src/main/java/com/fasterxml/jackson/datatype/guava/SimpleCacheDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.fasterxml.jackson.datatype.guava; | ||
|
||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.KeyDeserializer; | ||
import com.fasterxml.jackson.databind.deser.NullValueProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.type.MapLikeType; | ||
import com.fasterxml.jackson.datatype.guava.deser.cache.GuavaCacheDeserializer; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
/** | ||
* {@link GuavaCacheDeserializer} class implementation for deserializing Guava {@link Cache} instances. | ||
* | ||
* @since 2.16 | ||
*/ | ||
public class SimpleCacheDeserializer | ||
extends GuavaCacheDeserializer<Cache<Object,Object>> | ||
{ | ||
|
||
/* | ||
/********************************************************** | ||
/* Life-cycle | ||
/********************************************************** | ||
*/ | ||
|
||
public SimpleCacheDeserializer(MapLikeType type, KeyDeserializer keyDeserializer, | ||
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) | ||
{ | ||
super(type, keyDeserializer, elementTypeDeserializer, elementDeserializer); | ||
} | ||
|
||
public SimpleCacheDeserializer(MapLikeType type, KeyDeserializer keyDeserializer, | ||
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer, | ||
NullValueProvider nvp) | ||
{ | ||
super(type, keyDeserializer, elementTypeDeserializer, elementDeserializer, nvp); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Abstract method overrides | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
protected Cache<Object, Object> createCache() { | ||
return CacheBuilder.newBuilder().build(); | ||
} | ||
|
||
@Override | ||
protected JsonDeserializer<?> _createContextual(MapLikeType t, | ||
KeyDeserializer kd, TypeDeserializer vtd, JsonDeserializer<?> vd, NullValueProvider np) | ||
{ | ||
return new SimpleCacheDeserializer(t, kd, vtd, vd, np); | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
...rc/main/java/com/fasterxml/jackson/datatype/guava/deser/cache/GuavaCacheDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.fasterxml.jackson.datatype.guava.deser.cache; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | ||
import com.fasterxml.jackson.databind.deser.NullValueProvider; | ||
import com.fasterxml.jackson.databind.deser.impl.NullsConstantProvider; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.type.LogicalType; | ||
import com.fasterxml.jackson.databind.type.MapLikeType; | ||
import com.google.common.cache.Cache; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public abstract class GuavaCacheDeserializer<T extends Cache<Object, Object>> | ||
extends StdDeserializer<T> implements ContextualDeserializer | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final MapLikeType type; | ||
private final KeyDeserializer keyDeserializer; | ||
private final TypeDeserializer elementTypeDeserializer; | ||
private final JsonDeserializer<?> elementDeserializer; | ||
|
||
/* | ||
* @since 2.16 : in 3.x demote to `ContainerDeserializerBase` | ||
*/ | ||
private final NullValueProvider nullProvider; | ||
private final boolean skipNullValues; | ||
|
||
/* | ||
/********************************************************** | ||
/* Life-cycle | ||
/********************************************************** | ||
*/ | ||
|
||
public GuavaCacheDeserializer(MapLikeType type, KeyDeserializer keyDeserializer, | ||
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) { | ||
this(type, keyDeserializer, elementTypeDeserializer, elementDeserializer, null); | ||
} | ||
|
||
public GuavaCacheDeserializer(MapLikeType type, KeyDeserializer keyDeserializer, | ||
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer, | ||
NullValueProvider nvp) | ||
{ | ||
super(type); | ||
this.type = type; | ||
this.keyDeserializer = keyDeserializer; | ||
this.elementTypeDeserializer = elementTypeDeserializer; | ||
this.elementDeserializer = elementDeserializer; | ||
this.nullProvider = nvp; | ||
skipNullValues = (nvp == null) ? false : NullsConstantProvider.isSkipper(nvp); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Post-processing (contextualization) | ||
/********************************************************** | ||
*/ | ||
|
||
@Override | ||
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, | ||
BeanProperty property) throws JsonMappingException | ||
{ | ||
KeyDeserializer kd = keyDeserializer; | ||
if (kd == null) { | ||
kd = ctxt.findKeyDeserializer(type.getKeyType(), property); | ||
} | ||
JsonDeserializer<?> valueDeser = elementDeserializer; | ||
final JavaType vt = type.getContentType(); | ||
if (valueDeser == null) { | ||
valueDeser = ctxt.findContextualValueDeserializer(vt, property); | ||
} else { // if directly assigned, probably not yet contextual, so: | ||
valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt); | ||
} | ||
// Type deserializer is slightly different; must be passed, but needs to become contextual: | ||
TypeDeserializer vtd = elementTypeDeserializer; | ||
if (vtd != null) { | ||
vtd = vtd.forProperty(property); | ||
} | ||
return _createContextual(type, kd, vtd, valueDeser, | ||
findContentNullProvider(ctxt, property, valueDeser)); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Abstract methods for subclasses | ||
/********************************************************************** | ||
*/ | ||
|
||
protected abstract T createCache(); | ||
|
||
protected abstract JsonDeserializer<?> _createContextual(MapLikeType t, KeyDeserializer kd, | ||
TypeDeserializer vtd, JsonDeserializer<?> vd, NullValueProvider np); | ||
|
||
/* | ||
/********************************************************************** | ||
/* Implementations | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
public LogicalType logicalType() { | ||
return LogicalType.Map; | ||
} | ||
|
||
@Override | ||
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
return deserializeContents(p, ctxt); | ||
} | ||
|
||
private T deserializeContents(JsonParser p, DeserializationContext ctxt) | ||
throws IOException | ||
{ | ||
T cache = createCache(); | ||
|
||
JsonToken currToken = p.currentToken(); | ||
if (currToken != JsonToken.FIELD_NAME) { | ||
// 01-Mar-2023, tatu: [datatypes-collections#104] Handle empty Maps too | ||
if (currToken != JsonToken.END_OBJECT) { | ||
expect(p, JsonToken.START_OBJECT); | ||
currToken = p.nextToken(); | ||
} | ||
} | ||
|
||
for (; currToken == JsonToken.FIELD_NAME; currToken = p.nextToken()) { | ||
final Object key; | ||
if (keyDeserializer != null) { | ||
key = keyDeserializer.deserializeKey(p.currentName(), ctxt); | ||
} else { | ||
key = p.currentName(); | ||
} | ||
|
||
p.nextToken(); | ||
|
||
final Object value; | ||
if (p.currentToken() == JsonToken.VALUE_NULL) { | ||
if (skipNullValues) { | ||
continue; | ||
} | ||
value = nullProvider.getNullValue(ctxt); | ||
} else if (elementTypeDeserializer != null) { | ||
value = elementDeserializer.deserializeWithType(p, ctxt, elementTypeDeserializer); | ||
} else { | ||
value = elementDeserializer.deserialize(p, ctxt); | ||
} | ||
cache.put(key, value); | ||
} | ||
return cache; | ||
} | ||
|
||
private void expect(JsonParser p, JsonToken token) throws IOException { | ||
if (p.getCurrentToken() != token) { | ||
throw new JsonMappingException(p, "Expecting " + token + " to start `Cache` value, found " + p.currentToken(), | ||
p.getCurrentLocation()); | ||
} | ||
} | ||
} |
Oops, something went wrong.