-
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
8216fe6
commit a72efac
Showing
4 changed files
with
80 additions
and
1 deletion.
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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
package com.fasterxml.jackson.datatype.guava.ser; | ||
|
||
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<?, ?>> | ||
{ | ||
public CacheSerializer() { | ||
super(Cache.class); | ||
} | ||
|
||
@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 JacksonException | ||
{ | ||
gen.writeStartObject(value); | ||
_writeContents(value, gen, provider); | ||
gen.writeEndObject(); | ||
} | ||
|
||
@Override | ||
public void serializeWithType(Cache<?, ?> value, JsonGenerator gen, SerializerProvider ctxt, | ||
TypeSerializer typeSer) | ||
throws JacksonException | ||
{ | ||
gen.assignCurrentValue(value); | ||
WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, ctxt, | ||
typeSer.typeId(value, JsonToken.START_OBJECT)); | ||
_writeContents(value, gen, ctxt); | ||
typeSer.writeTypeSuffix(gen, ctxt, typeIdDef); | ||
} | ||
|
||
// Just a stub in case we have time to implement proper (if optional) serialization | ||
protected void _writeContents(Cache<?, ?> value, JsonGenerator g, SerializerProvider provider) | ||
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