Skip to content

Commit

Permalink
Minor improvement wrt #90: avoid exception, serialize as empty Object
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 3, 2021
1 parent a40a4ae commit 31885c7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
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
{
}
}
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));
}
}
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 31885c7

Please sign in to comment.