From 31885c7d952eb43ae46c6a3deff7f7612d88e410 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 2 Jul 2021 20:03:29 -0700 Subject: [PATCH] Minor improvement wrt #90: avoid exception, serialize as empty Object --- .../datatype/guava/GuavaSerializers.java | 6 ++ .../datatype/guava/ser/CacheSerializer.java | 55 +++++++++++++++++++ .../datatype/guava/CacheTypesTest.java | 20 +++++++ release-notes/VERSION-2.x | 1 + 4 files changed, 82 insertions(+) create mode 100644 guava/src/main/java/com/fasterxml/jackson/datatype/guava/ser/CacheSerializer.java create mode 100644 guava/src/test/java/com/fasterxml/jackson/datatype/guava/CacheTypesTest.java diff --git a/guava/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java b/guava/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java index 1d2e8be7..f69db2ef 100644 --- a/guava/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java +++ b/guava/src/main/java/com/fasterxml/jackson/datatype/guava/GuavaSerializers.java @@ -15,6 +15,7 @@ import com.fasterxml.jackson.databind.util.StdConverter; import com.fasterxml.jackson.datatype.guava.ser.RangeSetSerializer; import com.google.common.base.Optional; +import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilderSpec; import com.google.common.collect.FluentIterable; @@ -25,6 +26,7 @@ import com.google.common.hash.HashCode; import com.google.common.net.HostAndPort; import com.google.common.net.InternetDomainName; +import com.fasterxml.jackson.datatype.guava.ser.CacheSerializer; import com.fasterxml.jackson.datatype.guava.ser.GuavaOptionalSerializer; import com.fasterxml.jackson.datatype.guava.ser.MultimapSerializer; import com.fasterxml.jackson.datatype.guava.ser.RangeSerializer; @@ -91,6 +93,10 @@ public JsonSerializer findSerializer(SerializationConfig config, JavaType typ JavaType iterableType = _findDeclared(type, Iterable.class); return new StdDelegatingSerializer(FluentConverter.instance, iterableType, null); } + // [datatypes-collections#90]: add minimal "as-empty" serializer for Caches + if (Cache.class.isAssignableFrom(raw)) { + return new CacheSerializer(); + } return super.findSerializer(config, type, beanDesc); } diff --git a/guava/src/main/java/com/fasterxml/jackson/datatype/guava/ser/CacheSerializer.java b/guava/src/main/java/com/fasterxml/jackson/datatype/guava/ser/CacheSerializer.java new file mode 100644 index 00000000..536caa58 --- /dev/null +++ b/guava/src/main/java/com/fasterxml/jackson/datatype/guava/ser/CacheSerializer.java @@ -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> +{ + 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 + { + } +} diff --git a/guava/src/test/java/com/fasterxml/jackson/datatype/guava/CacheTypesTest.java b/guava/src/test/java/com/fasterxml/jackson/datatype/guava/CacheTypesTest.java new file mode 100644 index 00000000..0fb5b91f --- /dev/null +++ b/guava/src/test/java/com/fasterxml/jackson/datatype/guava/CacheTypesTest.java @@ -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 cache = CacheBuilder.newBuilder().build(); + cache.put("key", "value"); + + assertEquals("{}", MAPPER.writeValueAsString(cache)); + } +} diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 3667f117..78a57012 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -19,6 +19,7 @@ Active Maintainers: 2.13.0 (not yet released) #85: (eclipse-collections) Update eclipse-collections to version 10 and implement Triple deserialization. Still compatible with older EC versions +- (guava) Serialize `Cache` instances as empty Objects (see #90) 2.12.3 (12-Apr-2021)