From 9ca720d9e00fb3a4c48da057eea7895c612a5f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= Date: Wed, 27 Feb 2019 11:02:41 +0100 Subject: [PATCH] substitute ObjectMapperModifier by SciJavaJSONSerializer --- .../server/json/JsonSerializerAdapter.java | 29 +++++++++++++ .../server/json/SciJavaJSONSerializer.java | 32 +++++++++++++++ .../modifiers/ObjectMapperModifier.java | 41 ------------------- .../server/modifiers/SerializerModifier.java | 30 -------------- .../server/services/DefaultJsonService.java | 22 +++++----- 5 files changed, 72 insertions(+), 82 deletions(-) create mode 100644 src/main/java/net/imagej/server/json/JsonSerializerAdapter.java create mode 100644 src/main/java/net/imagej/server/json/SciJavaJSONSerializer.java delete mode 100644 src/main/java/net/imagej/server/modifiers/ObjectMapperModifier.java delete mode 100644 src/main/java/net/imagej/server/modifiers/SerializerModifier.java diff --git a/src/main/java/net/imagej/server/json/JsonSerializerAdapter.java b/src/main/java/net/imagej/server/json/JsonSerializerAdapter.java new file mode 100644 index 00000000..069635a5 --- /dev/null +++ b/src/main/java/net/imagej/server/json/JsonSerializerAdapter.java @@ -0,0 +1,29 @@ +package net.imagej.server.json; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +import java.io.IOException; + +class JsonSerializerAdapter extends StdSerializer { + + private SciJavaJSONSerializer serializedDelegate; + + public JsonSerializerAdapter( + SciJavaJSONSerializer serializedDelegate) + { + super(serializedDelegate.handleType()); + this.serializedDelegate = serializedDelegate; + } + + + @Override + public void serialize(T value, JsonGenerator gen, + SerializerProvider provider) throws IOException + { + serializedDelegate.serialize(value, gen, provider); + } + + +} \ No newline at end of file diff --git a/src/main/java/net/imagej/server/json/SciJavaJSONSerializer.java b/src/main/java/net/imagej/server/json/SciJavaJSONSerializer.java new file mode 100644 index 00000000..d20f3b54 --- /dev/null +++ b/src/main/java/net/imagej/server/json/SciJavaJSONSerializer.java @@ -0,0 +1,32 @@ + +package net.imagej.server.json; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; + +import java.io.IOException; + +import org.scijava.plugin.SciJavaPlugin; + +public interface SciJavaJSONSerializer extends SciJavaPlugin + +{ + + void serialize(T value, JsonGenerator gen, + SerializerProvider serializers) + throws IOException; + + Class handleType(); + + default boolean isSupportedBy(Class desiredClass) { + return handleType().isAssignableFrom(desiredClass); + } + + default public void register(ObjectMapper mapper) { + SimpleModule mod = new SimpleModule(); + mod.addSerializer(new JsonSerializerAdapter<>(this)); + mapper.registerModule(mod); + } +} diff --git a/src/main/java/net/imagej/server/modifiers/ObjectMapperModifier.java b/src/main/java/net/imagej/server/modifiers/ObjectMapperModifier.java deleted file mode 100644 index 7388f9a2..00000000 --- a/src/main/java/net/imagej/server/modifiers/ObjectMapperModifier.java +++ /dev/null @@ -1,41 +0,0 @@ - -package net.imagej.server.modifiers; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; -import java.util.function.Consumer; - -import org.scijava.plugin.SciJavaPlugin; - -public abstract class ObjectMapperModifier implements SciJavaPlugin, - Consumer -{ - - private Class supportedClass; - private Set> excludedClasses; - - public ObjectMapperModifier(Class supportedClass, - Collection> excludedClasses) - { - super(); - this.supportedClass = supportedClass; - this.excludedClasses = new HashSet<>(excludedClasses); - } - - protected Class getSupportedClass() { - return supportedClass; - } - - protected Set> getExcludedClasses() { - return excludedClasses; - } - - public boolean isSupportedBy(Class desiredClass) { - return getSupportedClass().isAssignableFrom(desiredClass) && - getExcludedClasses().stream().noneMatch(v -> v.isAssignableFrom( - desiredClass)); - } -} diff --git a/src/main/java/net/imagej/server/modifiers/SerializerModifier.java b/src/main/java/net/imagej/server/modifiers/SerializerModifier.java deleted file mode 100644 index c2cc4f4f..00000000 --- a/src/main/java/net/imagej/server/modifiers/SerializerModifier.java +++ /dev/null @@ -1,30 +0,0 @@ - -package net.imagej.server.modifiers; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; - -import java.util.Collection; - -public class SerializerModifier extends ObjectMapperModifier { - - private StdSerializer serializer; - - public SerializerModifier(Class supportedClass, - Collection> excludedClasses, StdSerializer serializer) - { - super(supportedClass, excludedClasses); - this.serializer = serializer; - - } - - @SuppressWarnings("unchecked") - @Override - public void accept(ObjectMapper mapper) { - SimpleModule mod = new SimpleModule(); - mod.addSerializer((Class) getSupportedClass(), serializer); - mapper.registerModule(mod); - } - -} diff --git a/src/main/java/net/imagej/server/services/DefaultJsonService.java b/src/main/java/net/imagej/server/services/DefaultJsonService.java index 8ec91ab9..738d2a71 100644 --- a/src/main/java/net/imagej/server/services/DefaultJsonService.java +++ b/src/main/java/net/imagej/server/services/DefaultJsonService.java @@ -47,10 +47,10 @@ import java.io.IOException; import java.util.Arrays; -import java.util.Collection; +import java.util.List; +import net.imagej.server.json.SciJavaJSONSerializer; import net.imagej.server.mixins.Mixins; -import net.imagej.server.modifiers.ObjectMapperModifier; import net.imglib2.EuclideanSpace; import org.scijava.Context; @@ -78,7 +78,7 @@ public class DefaultJsonService implements JsonService { */ private final UntypedObjectDeserializer idToObjDeserializer; - private final Collection objectMapperModifiers; + private final List jsonSerializers; /** * Constructs and initializes a JsonService with an {@link ObjectService}. @@ -135,7 +135,8 @@ public JsonSerializer modifySerializer(SerializationConfig config, // If the serialized class is supported thanks to a modification to // ObjectMapper, let's do it that way - if (objectMapperModifiers.stream().anyMatch(e -> e.isSupportedBy( + if (jsonSerializers.stream().map(obj -> (SciJavaJSONSerializer) obj) + .anyMatch(e -> e.isSupportedBy( desiredClass))) return serializer; // If the serialized class is unknown (i.e. serialized using the general @@ -152,10 +153,10 @@ public JsonSerializer modifySerializer(SerializationConfig config, objToIdMapper = new ObjectMapper(); objToIdMapper.registerModule(objToIdModule); - objectMapperModifiers = ctx.getService(PluginService.class) - .createInstancesOfType((ObjectMapperModifier.class)); + jsonSerializers = ctx.getService(PluginService.class).createInstancesOfType( + SciJavaJSONSerializer.class); - applyModifiers(); + registerSerializers(); // register Jackson MixIns to obtain better json output format for some // specific types @@ -179,9 +180,8 @@ public boolean notSerialized(final Class target) { .isAssignableFrom(target)); } - private void applyModifiers() { - for (ObjectMapperModifier modifier : objectMapperModifiers) { - modifier.accept(objToIdMapper); - } + private void registerSerializers() { + jsonSerializers.stream().map(obj -> (SciJavaJSONSerializer) obj).forEach( + serializer -> serializer.register(objToIdMapper)); } }