|
| 1 | +package tools.jackson.databind.ser; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 8 | +import com.fasterxml.jackson.annotation.JsonSerializeAs; |
| 9 | + |
| 10 | +import tools.jackson.databind.*; |
| 11 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * Unit tests for new {@link JsonSerializeAs} annotation. |
| 17 | + */ |
| 18 | +public class JsonSerializeAsTest extends DatabindTestUtil |
| 19 | +{ |
| 20 | + /* |
| 21 | + /********************************************************************** |
| 22 | + /* Annotated helper classes for @JsonSerializeAs#value on class |
| 23 | + /********************************************************************** |
| 24 | + */ |
| 25 | + |
| 26 | + public interface Fooable { |
| 27 | + public int getFoo(); |
| 28 | + } |
| 29 | + |
| 30 | + // force use of interface |
| 31 | + @JsonSerializeAs(Fooable.class) |
| 32 | + public static class FooImpl implements Fooable { |
| 33 | + @Override |
| 34 | + public int getFoo() { return 42; } |
| 35 | + public int getBar() { return 15; } |
| 36 | + } |
| 37 | + |
| 38 | + static class FooImplNoAnno implements Fooable { |
| 39 | + @Override |
| 40 | + public int getFoo() { return 42; } |
| 41 | + public int getBar() { return 15; } |
| 42 | + } |
| 43 | + |
| 44 | + public class Fooables { |
| 45 | + public FooImpl[] getFoos() { |
| 46 | + return new FooImpl[] { new FooImpl() }; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /* |
| 51 | + /********************************************************************** |
| 52 | + /* Annotated helper classes for @JsonSerializeAs#value on property |
| 53 | + /********************************************************************** |
| 54 | + */ |
| 55 | + |
| 56 | + public class FooableWrapper { |
| 57 | + public FooImpl getFoo() { |
| 58 | + return new FooImpl(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static class FooableWithFieldWrapper { |
| 63 | + @JsonSerializeAs(Fooable.class) |
| 64 | + public Fooable getFoo() { |
| 65 | + return new FooImplNoAnno(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + /********************************************************************** |
| 71 | + /* Annotated helper classes for @JsonSerializeAs#content |
| 72 | + /********************************************************************** |
| 73 | + */ |
| 74 | + |
| 75 | + interface Bean5476Base { |
| 76 | + public int getA(); |
| 77 | + } |
| 78 | + |
| 79 | + @JsonPropertyOrder({"a","b"}) |
| 80 | + static abstract class Bean5476Abstract implements Bean5476Base { |
| 81 | + @Override |
| 82 | + public int getA() { return 1; } |
| 83 | + |
| 84 | + public int getB() { return 2; } |
| 85 | + } |
| 86 | + |
| 87 | + static class Bean5476Impl extends Bean5476Abstract { |
| 88 | + public int getC() { return 3; } |
| 89 | + } |
| 90 | + |
| 91 | + static class Bean5476Wrapper { |
| 92 | + @JsonSerializeAs(content=Bean5476Abstract.class) |
| 93 | + public List<Bean5476Base> values; |
| 94 | + public Bean5476Wrapper(int count) { |
| 95 | + values = new ArrayList<Bean5476Base>(); |
| 96 | + for (int i = 0; i < count; ++i) { |
| 97 | + values.add(new Bean5476Impl()); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + static class Bean5476Holder { |
| 103 | + @JsonSerializeAs(Bean5476Abstract.class) |
| 104 | + public Bean5476Base value = new Bean5476Impl(); |
| 105 | + } |
| 106 | + |
| 107 | + /* |
| 108 | + /********************************************************************** |
| 109 | + /* Annotated helper classes for @JsonSerializeAs#key |
| 110 | + /********************************************************************** |
| 111 | + */ |
| 112 | + |
| 113 | + interface MapKeyBase { |
| 114 | + String getId(); |
| 115 | + } |
| 116 | + |
| 117 | + @JsonPropertyOrder({"id"}) |
| 118 | + static abstract class MapKeyAbstract implements MapKeyBase { |
| 119 | + @Override |
| 120 | + public String getId() { return "key"; } |
| 121 | + } |
| 122 | + |
| 123 | + static class MapKeyImpl extends MapKeyAbstract { |
| 124 | + public String getExtra() { return "extra"; } |
| 125 | + } |
| 126 | + |
| 127 | + static class MapKeyWrapper { |
| 128 | + @JsonSerializeAs(key=MapKeyAbstract.class) |
| 129 | + public Map<MapKeyBase, String> values; |
| 130 | + |
| 131 | + public MapKeyWrapper() { |
| 132 | + values = new LinkedHashMap<>(); |
| 133 | + values.put(new MapKeyImpl(), "value1"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /* |
| 138 | + /********************************************************************** |
| 139 | + /* Test methods |
| 140 | + /********************************************************************** |
| 141 | + */ |
| 142 | + |
| 143 | + private final ObjectWriter WRITER = objectWriter(); |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testSerializeAsInClass() throws Exception { |
| 147 | + assertEquals("{\"foo\":42}", WRITER.writeValueAsString(new FooImpl())); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testSerializeAsForArrayProp() throws Exception { |
| 152 | + assertEquals("{\"foos\":[{\"foo\":42}]}", |
| 153 | + WRITER.writeValueAsString(new Fooables())); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testSerializeAsForSimpleProp() throws Exception { |
| 158 | + assertEquals("{\"foo\":{\"foo\":42}}", |
| 159 | + WRITER.writeValueAsString(new FooableWrapper())); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testSerializeWithFieldAnno() throws Exception { |
| 164 | + assertEquals("{\"foo\":{\"foo\":42}}", |
| 165 | + WRITER.writeValueAsString(new FooableWithFieldWrapper())); |
| 166 | + } |
| 167 | + |
| 168 | + // Test for content parameter |
| 169 | + @Test |
| 170 | + public void testSpecializedContentAs() throws Exception { |
| 171 | + assertEquals(a2q("{'values':[{'a':1,'b':2}]}"), |
| 172 | + WRITER.writeValueAsString(new Bean5476Wrapper(1))); |
| 173 | + } |
| 174 | + |
| 175 | + // Test for value parameter |
| 176 | + @Test |
| 177 | + public void testSpecializedAsIntermediate() throws Exception { |
| 178 | + assertEquals(a2q("{'value':{'a':1,'b':2}}"), |
| 179 | + WRITER.writeValueAsString(new Bean5476Holder())); |
| 180 | + } |
| 181 | + |
| 182 | + // Test for key parameter |
| 183 | + @Test |
| 184 | + public void testSpecializedKeyAs() throws Exception { |
| 185 | + String json = WRITER.writeValueAsString(new MapKeyWrapper()); |
| 186 | + // Map key serialization depends on how MapKeyAbstract is serialized |
| 187 | + // Since it has only getId(), we expect the key to be serialized as just that property |
| 188 | + assertTrue(json.contains("\"values\""), "Should contain 'values' field"); |
| 189 | + } |
| 190 | +} |
0 commit comments