Skip to content

Commit

Permalink
substitute ObjectMapperModifier by SciJavaJSONSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kozusznik committed Feb 27, 2019
1 parent 6d3d3df commit 9ca720d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 82 deletions.
29 changes: 29 additions & 0 deletions src/main/java/net/imagej/server/json/JsonSerializerAdapter.java
Original file line number Diff line number Diff line change
@@ -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<T> extends StdSerializer<T> {

private SciJavaJSONSerializer<T> serializedDelegate;

public JsonSerializerAdapter(
SciJavaJSONSerializer<T> serializedDelegate)
{
super(serializedDelegate.handleType());
this.serializedDelegate = serializedDelegate;
}


@Override
public void serialize(T value, JsonGenerator gen,
SerializerProvider provider) throws IOException
{
serializedDelegate.serialize(value, gen, provider);
}


}
32 changes: 32 additions & 0 deletions src/main/java/net/imagej/server/json/SciJavaJSONSerializer.java
Original file line number Diff line number Diff line change
@@ -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<T> extends SciJavaPlugin

{

void serialize(T value, JsonGenerator gen,
SerializerProvider serializers)
throws IOException;

Class<T> 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);
}
}

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/java/net/imagej/server/modifiers/SerializerModifier.java

This file was deleted.

22 changes: 11 additions & 11 deletions src/main/java/net/imagej/server/services/DefaultJsonService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +78,7 @@ public class DefaultJsonService implements JsonService {
*/
private final UntypedObjectDeserializer idToObjDeserializer;

private final Collection<ObjectMapperModifier> objectMapperModifiers;
private final List<?> jsonSerializers;

/**
* Constructs and initializes a JsonService with an {@link ObjectService}.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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));
}
}

0 comments on commit 9ca720d

Please sign in to comment.