import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Ignore; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.databind.ObjectMapper; /** * #137 There are serious issues with some jackson versions. We need to check * what we are up to to ensure our code works properly. For the moment the rest * of our code avoid @JsonValue because of this, which causes the json * serialiation to be a bit awkward * */ @Ignore public class JacksonTest { final ObjectMapper jackson = new ObjectMapper(); final N n = new N(); @Test public void testN() throws IOException { doTest(new N()); } @Test public void testYN() throws IOException { doTest(new Y(n)); } @Test public void testYYN() throws IOException { doTest(new Y(new Y(n))); } @Test public void testZYN() throws IOException { doTest(new Z(new Y(n))); } @Test public void testZN() throws IOException { doTest(new Z(n)); } @Test public void testWN() throws IOException { doTest(new W(n)); } private void doTest(X val) throws IOException { System.out.println("testing " + val); String str = jackson.writeValueAsString(val); System.out.println("To json: " + str); X obj = jackson.readValue(str, X.class); System.out.println("reparsed: " + obj); assertEquals(val.toString(), obj.toString()); } } @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) @JsonSubTypes({ @JsonSubTypes.Type(value = Y.class), @JsonSubTypes.Type(value = Z.class), @JsonSubTypes.Type(value = W.class), @JsonSubTypes.Type(value = N.class) }) interface X { } class N implements X { @Override public String toString() { return "N"; } } class Y implements X { // NOT @JsonValue - works private final X value; @JsonCreator public Y(@JsonProperty("value") X val) { this.value = val; } @Override public String toString() { return "Y(" + value + ")"; } } class Z implements X { @JsonValue private final X value; @JsonCreator public Z(@JsonProperty("value") X value) { this.value = value; } @Override public String toString() { return "Z(" + value + ")"; } } @JsonAutoDetect(fieldVisibility = Visibility.ANY) class W implements X { private final X value; @JsonCreator public W(@JsonProperty("value") X value) { this.value = value; } @JsonValue public X getValue() { return value; } @Override public String toString() { return "W(" + value + ")"; } }