@JsonTypeInfo(use = CLASS, include = PROPERTY, property = "@class")
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public interface MyInterface<T> {
boolean myMethod(T value);
}
public static class MyClass {
@JsonProperty("value")
private final Option<MyInterface<Integer>> value;
@JsonCreator
public MyClass(@JsonProperty("value") final Option<MyInterface<Integer>> value) {
this.value = value;
}
}
public static class ImplementedClass implements MyInterface<Integer> {
@Override
public boolean myMethod(final Integer value) {
return false;
}
}
@Test
public void optionBindingDoesNotWork() {
Json.mapper.registerModule(new VavrModule());
Json.prettyMapper.registerModule(new VavrModule());
final MyClass opt = new MyClass(Option.some(new ImplementedClass()));
final String jsonString = Json.encodePrettily(opt);
final MyClass result = Json.decodeValue(jsonString, new TypeReference<MyClass>() {
});
}
Error is: io.vertx.core.json.EncodeException: Failed to encode as JSON: Cannot create TypeBindings for class ImplementedClass with 1 type parameter: class expects 0 (through reference chain: MyClass["value"])
However, making an interface-alias does work:
public interface MyExtendedInterface extends MyInterface<Integer> {
}
public static class OtherImplementedClass implements MyExtendedInterface {
@Override
public boolean myMethod(final Integer value) {
return false;
}
}
public static class MyOtherClass {
@JsonProperty("value")
private final Option<MyExtendedInterface> value;
@JsonCreator
public MyOtherClass(@JsonProperty("value")final Option<MyExtendedInterface> value) {
this.value = value;
}
}
@Test
public void optionBindingDoesWork() {
Json.mapper.registerModule(new VavrModule());
Json.prettyMapper.registerModule(new VavrModule());
final MyOtherClass opt = new MyOtherClass(Option.some(new OtherImplementedClass()));
final String jsonString = Json.encodePrettily(opt);
final MyOtherClass result = Json.decodeValue(jsonString, new TypeReference<MyOtherClass>() {
});
}
io.vertx.core.json.EncodeException: Failed to encode as JSON: Cannot create TypeBindings for class NonWorkingOptionSerialization$ImplementedClass with 1 type parameter: class expects 0 (through reference chain: NonWorkingOptionSerialization$MyClass["value"])
at io.vertx.core.json.Json.encodePrettily(Json.java:103)
at NonWorkingOptionSerialization.optionBindingDoesNotWork(NonWorkingOptionSerialization.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Following sample-code throws an Exception:
Error is:
io.vertx.core.json.EncodeException: Failed to encode as JSON: Cannot create TypeBindings for class ImplementedClass with 1 type parameter: class expects 0 (through reference chain: MyClass["value"])
However, making an interface-alias does work:
The exception is thrown at: https://github.com/vavr-io/vavr-jackson/blob/2afe67256bc18ee77bfff48f514885734d1b8a64/src/main/java/io/vavr/jackson/datatype/serialize/HListSerializer.java#L54. Most probably because it tries to apply the generic type to
MyInterface<Integer>
Exact Stacktrace:
Doing the same on a List does work.
versions: vavr-jackson:
0.9.2
java:1.8
vert.x:3.5.1