-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from kozusznik/preparingForMergeRequest
Enable interval serialization
- Loading branch information
Showing
8 changed files
with
196 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/net/imagej/server/json/JsonSerializerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/main/java/net/imagej/server/json/SciJavaJsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/java/net/imagej/server/SciJavaJsonSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
|
||
package net.imagej.server; | ||
|
||
import static io.dropwizard.testing.FixtureHelpers.fixture; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import io.dropwizard.jackson.Jackson; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import net.imagej.server.json.SciJavaJsonSerializer; | ||
import net.imagej.server.services.DefaultJsonService; | ||
import net.imagej.server.services.DefaultObjectService; | ||
import net.imglib2.FinalInterval; | ||
import net.imglib2.Interval; | ||
|
||
import org.junit.Test; | ||
import org.scijava.Context; | ||
import org.scijava.plugin.Plugin; | ||
|
||
public class SciJavaJsonSerializerTest { | ||
|
||
@Test | ||
public void serializeIntervalUsingSciJavaJsonSerializer() throws Exception { | ||
|
||
final ObjectMapper mapper = Jackson.newObjectMapper(); | ||
final DefaultObjectService objectService = new DefaultObjectService(); | ||
final DefaultJsonService jsonService = new DefaultJsonService(new Context(), | ||
objectService); | ||
jsonService.addDeserializerTo(mapper); | ||
|
||
final Interval testInterval = new FinalInterval(new long[] { 0, 1 }, | ||
new long[] { 8, 9 }); | ||
final Map<String, Object> outputs = new HashMap<>(); | ||
outputs.put("testInterval", testInterval); | ||
|
||
final String parsed = jsonService.parseObject(outputs); | ||
|
||
final String expected = mapper.writeValueAsString(mapper.readValue(fixture( | ||
"fixtures/outputs/finalIntervalType.json"), Map.class)); | ||
|
||
assertEquals(expected, parsed); | ||
|
||
} | ||
|
||
@Plugin(type = SciJavaJsonSerializer.class) | ||
public static class ExampleIntervalJsonSerializer implements | ||
SciJavaJsonSerializer<Interval> | ||
{ | ||
|
||
@Override | ||
public boolean isSupportedBy(Class<?> desiredClass) { | ||
return desiredClass.equals(FinalInterval.class); | ||
} | ||
|
||
@Override | ||
public void serialize(Interval interval, JsonGenerator gen, | ||
SerializerProvider serializers) throws IOException | ||
{ | ||
|
||
gen.writeStartObject(); | ||
gen.writeArrayFieldStart("min"); | ||
for (int i = 0; i < interval.numDimensions(); i++) { | ||
gen.writeNumber(interval.min(i)); | ||
} | ||
gen.writeEndArray(); | ||
gen.writeArrayFieldStart("max"); | ||
for (int i = 0; i < interval.numDimensions(); i++) { | ||
gen.writeNumber(interval.max(i)); | ||
} | ||
gen.writeEndArray(); | ||
gen.writeEndObject(); | ||
|
||
} | ||
|
||
@Override | ||
public Class<Interval> handleType() { | ||
return Interval.class; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"testInterval": { | ||
"min": [0, 1], | ||
"max": [8, 9] | ||
} | ||
} |