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 8216fe6 commit a72efac
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
import com.fasterxml.jackson.databind.util.StdConverter;
import com.fasterxml.jackson.datatype.guava.ser.RangeSetSerializer;

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;
import com.fasterxml.jackson.datatype.guava.ser.TableSerializer;

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 Down Expand Up @@ -96,6 +97,12 @@ public ValueSerializer<?> findSerializer(SerializationConfig config, JavaType ty
JavaType iterableType = _findDeclared(type, Iterable.class);
return new StdDelegatingSerializer(FluentConverter.instance, iterableType, null, null);
}
// [datatypes-collections#90]: add bogus "serialize as empty" serializer to avoid
// error on "no properties". If proper serialization (and deserialization) needed,
// would need to resolve type parameters here
if (Cache.class.isAssignableFrom(raw)) {
return new CacheSerializer();
}
return ImmutablePrimitiveTypes.isAssignableFromImmutableArray(raw)
.transform(ImmutablePrimitiveTypes.ImmutablePrimitiveArrays::newSerializer)
.orNull();
Expand Down
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
{
}
}
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 a72efac

Please sign in to comment.