[429] | 1 |
|
---|
| 2 | import static org.junit.Assert.assertEquals;
|
---|
| 3 |
|
---|
[430] | 4 | import java.io.IOException;
|
---|
| 5 |
|
---|
[429] | 6 | import org.junit.Test;
|
---|
| 7 |
|
---|
| 8 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
| 9 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
| 10 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 11 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 12 | import com.fasterxml.jackson.annotation.JsonSubTypes;
|
---|
| 13 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
---|
| 14 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
| 15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * There are serious issues with some jackson versions. We need to check what we
|
---|
| 19 | * are up to to ensure our code works properly.
|
---|
| 20 | *
|
---|
| 21 | */
|
---|
| 22 | public class JacksonTest {
|
---|
| 23 | final ObjectMapper jackson = new ObjectMapper();
|
---|
| 24 | final N n = new N();
|
---|
| 25 |
|
---|
| 26 | @Test
|
---|
[430] | 27 | public void testN() throws IOException {
|
---|
[429] | 28 | doTest(new N());
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | @Test
|
---|
[430] | 32 | public void testYN() throws IOException {
|
---|
[429] | 33 | doTest(new Y(n));
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | @Test
|
---|
[430] | 37 | public void testYYN() throws IOException {
|
---|
[429] | 38 | doTest(new Y(new Y(n)));
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Test
|
---|
[430] | 42 | public void testZYN() throws IOException {
|
---|
[429] | 43 | doTest(new Z(new Y(n)));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | @Test
|
---|
[430] | 47 | public void testZN() throws IOException {
|
---|
[429] | 48 | doTest(new Z(n));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[430] | 51 | @Test
|
---|
| 52 | public void testWN() throws IOException {
|
---|
| 53 | doTest(new W(n));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | private void doTest(X val) throws IOException {
|
---|
[429] | 57 | System.out.println("testing " + val);
|
---|
| 58 | String str = jackson.writeValueAsString(val);
|
---|
| 59 | System.out.println("To json: " + str);
|
---|
| 60 | X obj = jackson.readValue(str, X.class);
|
---|
| 61 | System.out.println("reparsed: " + obj);
|
---|
| 62 |
|
---|
| 63 | assertEquals(val.toString(), obj.toString());
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
|
---|
| 69 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
| 70 | @JsonSubTypes({ @JsonSubTypes.Type(value = Y.class),
|
---|
| 71 | @JsonSubTypes.Type(value = Z.class),
|
---|
[430] | 72 | @JsonSubTypes.Type(value = W.class),
|
---|
[429] | 73 | @JsonSubTypes.Type(value = N.class) })
|
---|
| 74 | interface X {
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | class N implements X {
|
---|
| 78 |
|
---|
| 79 | @Override
|
---|
| 80 | public String toString() {
|
---|
| 81 | return "N";
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | class Y implements X {
|
---|
| 87 |
|
---|
| 88 | // NOT @JsonValue - works
|
---|
| 89 | private final X value;
|
---|
| 90 |
|
---|
| 91 | @JsonCreator
|
---|
| 92 | public Y(@JsonProperty("value") X val) {
|
---|
| 93 | this.value = val;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Override
|
---|
| 97 | public String toString() {
|
---|
| 98 | return "Y(" + value + ")";
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | class Z implements X {
|
---|
| 104 | @JsonValue
|
---|
| 105 | private final X value;
|
---|
| 106 |
|
---|
| 107 | @JsonCreator
|
---|
[430] | 108 | public Z(@JsonProperty("value") X value) {
|
---|
[429] | 109 | this.value = value;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | @Override
|
---|
| 113 | public String toString() {
|
---|
| 114 | return "Z(" + value + ")";
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[430] | 117 | }
|
---|
| 118 |
|
---|
| 119 | @JsonAutoDetect(fieldVisibility = Visibility.ANY)
|
---|
| 120 | class W implements X {
|
---|
| 121 | private final X value;
|
---|
| 122 |
|
---|
| 123 | @JsonCreator
|
---|
| 124 | public W(@JsonProperty("value") X value) {
|
---|
| 125 | this.value = value;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | @JsonValue
|
---|
| 129 | public X getValue() {
|
---|
| 130 | return value;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | @Override
|
---|
| 134 | public String toString() {
|
---|
| 135 | return "W(" + value + ")";
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | }
|
---|