-
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.
Minor improvement wrt #90: avoid exception, serialize as empty Object
- Loading branch information
1 parent
a40a4ae
commit 31885c7
Showing
4 changed files
with
82 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
55 changes: 55 additions & 0 deletions
55
guava/src/main/java/com/fasterxml/jackson/datatype/guava/ser/CacheSerializer.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,55 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.type.WritableTypeId; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
import com.google.common.cache.Cache; | ||
|
||
public class CacheSerializer extends StdSerializer<Cache<?, ?>> | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public CacheSerializer() { | ||
super(Cache.class, false); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty(SerializerProvider prov, Cache<?, ?> value) { | ||
// Since we serialize all as empty, let's claim we are always empty | ||
return true; | ||
} | ||
|
||
@Override | ||
public void serialize(Cache<?, ?> value, JsonGenerator gen, SerializerProvider provider) | ||
throws IOException | ||
{ | ||
gen.writeStartObject(value); | ||
_writeContents(value, gen, provider); | ||
gen.writeEndObject(); | ||
} | ||
|
||
@Override | ||
public void serializeWithType(Cache<?, ?> value, JsonGenerator gen, SerializerProvider ctxt, | ||
TypeSerializer typeSer) | ||
throws IOException | ||
{ | ||
gen.assignCurrentValue(value); | ||
WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, | ||
typeSer.typeId(value, JsonToken.START_OBJECT)); | ||
_writeContents(value, gen, ctxt); | ||
typeSer.writeTypeSuffix(gen, typeIdDef); | ||
} | ||
|
||
// Just a stub in case we have time to implement proper (if optional) serialization | ||
protected void _writeContents(Cache<?, ?> value, JsonGenerator g, SerializerProvider ctxt) | ||
throws JacksonException | ||
{ | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
guava/src/test/java/com/fasterxml/jackson/datatype/guava/CacheTypesTest.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,20 @@ | ||
package com.fasterxml.jackson.datatype.guava; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
public class CacheTypesTest extends ModuleTestBase | ||
{ | ||
private final ObjectMapper MAPPER = mapperWithModule(); | ||
|
||
// [datatypes-collections#90]: only ensure we can serialize caches as | ||
// empty, to begin with | ||
public void testSerializabilityOfCacheAsEmpty() throws Exception | ||
{ | ||
Cache<String, String> cache = CacheBuilder.newBuilder().build(); | ||
cache.put("key", "value"); | ||
|
||
assertEquals("{}", MAPPER.writeValueAsString(cache)); | ||
} | ||
} |
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