-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suppport simple deserialization of Cache
#116
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80aca9f
Implement simple CacheDeserializer
JooHyukKim bc45b77
Update GuavaDeserializers.java
JooHyukKim c060ecf
Update GuavaDeserializers.java
JooHyukKim 4595f22
Remove unnecessary comments
JooHyukKim a30cd4f
Fix failing tests.
JooHyukKim 72e030a
Update SimpleCacheDeserializer.java
JooHyukKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||||
Comment on lines
+137
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Exception this part where we do not assume the value is array like Line 178 in 66ee959
|
||||
} | ||||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Referenced most from
jackson-datatypes-collections/guava/src/main/java/com/fasterxml/jackson/datatype/guava/deser/multimap/GuavaMultimapDeserializer.java
Line 155 in 66ee959