Debian Codegraph @

jr-annotation-support/src/test/java/com/fasterxml/jackson/jr/annotationsupport/BasicRenameTest.java

raw
1 package com.fasterxml.jackson.jr.annotationsupport;
2
3 import com.fasterxml.jackson.annotation.JsonCreator;
4 import com.fasterxml.jackson.annotation.JsonProperty;
5
6 import com.fasterxml.jackson.annotation.JsonValue;
7 import com.fasterxml.jackson.jr.ob.JSON;
8 import com.fasterxml.jackson.jr.ob.JSONObjectException;
9
10 public class BasicRenameTest extends ASTestBase
11 {
12 static class NameSimple {
13 @JsonProperty("firstName")
14 public String _first;
15
16 @JsonProperty // just explicit marker, no rename
17 public String _last;
18
19 protected NameSimple() { }
20 public NameSimple(String f, String l) {
21 _first = f;
22 _last = l;
23 }
24 }
25
26 private interface EnumIdentifier {
27 @JsonValue
28 String getId();
29 }
30
31 /**
32 * enum that uses {@link JsonProperty} to alias values
33 */
34 protected enum ABCRename { @JsonProperty("A1") A, @JsonProperty("B1") B, C; }
35
36 /**
37 * enum that uses a {@link JsonValue} method to alias its values and a {@link JsonCreator} to
38 * deserialize
39 */
40 protected enum ABCJsonValueJsonCreator
41 {
42 A("A1"),
43 B("B1"),
44 C("C");
45
46 private String label;
47
48 ABCJsonValueJsonCreator(String label) {
49 this.label = label;
50 }
51
52 @Override
53 public String toString() {
54 return "ABCJsonValueJsonCreator{" +
55 "label='" + label + '\'' +
56 '}';
57 }
58
59 @JsonValue
60 public String serialize() {
61 return label;
62 }
63
64 @JsonCreator
65 public static ABCJsonValueJsonCreator fromLabel(String label) {
66 for (ABCJsonValueJsonCreator value : ABCJsonValueJsonCreator.values()) {
67 if (value.label.equals(label)) {
68 return value;
69 }
70 }
71 throw new IllegalArgumentException("Unexpected label '" + label + "'");
72 }
73 }
74
75 /**
76 * Enum values in this class are anonymous inner classes
77 */
78 protected enum SubclassingEnum implements EnumIdentifier {
79 ENUM_A {
80 @Override
81 public String getId() {
82 return "A";
83 }
84 },
85 ENUM_B {
86 @Override
87 public String getId() {
88 return "B";
89 }
90 },
91 ENUM_NO_JSON_VALUE {
92 // don't use this to make the serialized version
93 // hiding the superclass's @JsonValue
94 @JsonValue(false)
95 @Override
96 public String getId() {
97 return "NONE";
98 }
99 };
100
101
102 @JsonCreator
103 public static SubclassingEnum from(String id) {
104 if ("A".equals(id)) {
105 return ENUM_A;
106 }
107 if ("B".equals(id)) {
108 return ENUM_B;
109 }
110 return null;
111 }
112 }
113
114
115
116 /*
117 /**********************************************************************
118 /* Test methods
119 /**********************************************************************
120 */
121
122 // for stricter validation, fail on unknown properties
123 private final JSON JSON_WITH_ANNO = jsonWithAnnotationSupport()
124 .with(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY);
125
126 public void testBasicRenameOnSerialize() throws Exception
127 {
128 final NameSimple input = new NameSimple("Bob", "Burger");
129 // default, no ignorals:
130 assertEquals(a2q("{'_first':'Bob','_last':'Burger'}"), JSON.std.asString(input));
131
132 // but if we rename "_last"
133 assertEquals(a2q("{'_last':'Burger','firstName':'Bob'}"), JSON_WITH_ANNO.asString(input));
134
135 // and ensure no leakage to default one:
136 assertEquals(a2q("{'_first':'Bob','_last':'Burger'}"), JSON.std.asString(input));
137 }
138
139 public void testBasicRenameOnDeserialize() throws Exception
140 {
141 final String json = a2q("{'firstName':'Bob','_last':'Burger'}");
142 final JSON j = JSON.std
143 .with(JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY);
144
145 try {
146 j.beanFrom(NameSimple.class, json);
147 fail("Should not pass");
148 } catch (JSONObjectException e) {
149 verifyException(e, "Unrecognized JSON property \"firstName\"");
150 }
151 NameSimple result = JSON_WITH_ANNO.beanFrom(NameSimple.class, json);
152 assertEquals("Bob", result._first);
153 assertEquals("Burger", result._last);
154 }
155
156 public void testEnumRenameOnSerialize() throws Exception
157 {
158 ABCRename inputA = ABCRename.A;
159 // default
160 assertEquals(a2q("\"A\""), JSON.std.asString(inputA));
161 // with annotations
162 assertEquals(a2q("\"A1\""), JSON_WITH_ANNO.asString(inputA));
163
164 ABCRename inputB = ABCRename.B;
165 // default
166 assertEquals(a2q("\"B\""), JSON.std.asString(inputB));
167 // with annotations
168 assertEquals(a2q("\"B1\""), JSON_WITH_ANNO.asString(inputB));
169
170 ABCRename inputC = ABCRename.C;
171 // default
172 assertEquals(a2q("\"C\""), JSON.std.asString(inputC));
173 // with annotations
174 assertEquals(a2q("\"C\""), JSON_WITH_ANNO.asString(inputC));
175 }
176
177 public void testEnumRenameOnDeserialize() throws Exception
178 {
179 String jsonA = a2q("\"A1\"");
180 ABCRename resultA = JSON_WITH_ANNO.beanFrom(ABCRename.class, jsonA);
181 assertEquals(ABCRename.A, resultA);
182
183 String jsonB = a2q("\"B1\"");
184 ABCRename resultB = JSON_WITH_ANNO.beanFrom(ABCRename.class, jsonB);
185 assertEquals(ABCRename.B, resultB);
186
187 String jsonC = a2q("\"C\"");
188 ABCRename resultC = JSON_WITH_ANNO.beanFrom(ABCRename.class, jsonC);
189 assertEquals(ABCRename.C, resultC);
190 }
191
192 public void testJsonValueCreatorEnumRenameOnSerialize() throws Exception
193 {
194 ABCJsonValueJsonCreator inputA = ABCJsonValueJsonCreator.A;
195 // default
196 assertEquals("\"ABCJsonValueJsonCreator{label='A1'}\"", JSON.std.asString(inputA));
197 // with annotations
198 assertEquals(a2q("\"A1\""), JSON_WITH_ANNO.asString(inputA));
199
200 ABCJsonValueJsonCreator inputB = ABCJsonValueJsonCreator.B;
201 // default
202 assertEquals("\"ABCJsonValueJsonCreator{label='B1'}\"", JSON.std.asString(inputB));
203 // with annotations
204 assertEquals(a2q("\"B1\""), JSON_WITH_ANNO.asString(inputB));
205
206 ABCJsonValueJsonCreator inputC = ABCJsonValueJsonCreator.C;
207 // default
208 assertEquals("\"ABCJsonValueJsonCreator{label='C'}\"", JSON.std.asString(inputC));
209 // with annotations
210 assertEquals(a2q("\"C\""), JSON_WITH_ANNO.asString(inputC));
211 }
212
213 public void testJsonValueCreatorEnumRenameOnDeserialize() throws Exception
214 {
215 String jsonA = a2q("\"A1\"");
216 ABCJsonValueJsonCreator resultA = JSON_WITH_ANNO.beanFrom(ABCJsonValueJsonCreator.class, jsonA);
217 assertEquals(ABCJsonValueJsonCreator.A, resultA);
218
219 String jsonB = a2q("\"B1\"");
220 ABCJsonValueJsonCreator resultB = JSON_WITH_ANNO.beanFrom(ABCJsonValueJsonCreator.class, jsonB);
221 assertEquals(ABCJsonValueJsonCreator.B, resultB);
222
223 String jsonC = a2q("\"C\"");
224 ABCJsonValueJsonCreator resultC = JSON_WITH_ANNO.beanFrom(ABCJsonValueJsonCreator.class, jsonC);
225 assertEquals(ABCJsonValueJsonCreator.C, resultC);
226 }
227
228 public void testJsonValueCreatorHierarchicalEnumRenameOnSerialize() throws Exception
229 {
230 SubclassingEnum inputA = SubclassingEnum.ENUM_A;
231 // default
232 assertEquals("\"ENUM_A\"", JSON.std.asString(inputA));
233 // with annotations
234 assertEquals(a2q("\"A\""), JSON_WITH_ANNO.asString(inputA));
235
236 SubclassingEnum inputB = SubclassingEnum.ENUM_B;
237 // default
238 assertEquals("\"ENUM_B\"", JSON.std.asString(inputB));
239 // with annotations
240 assertEquals(a2q("\"B\""), JSON_WITH_ANNO.asString(inputB));
241 }
242
243 public void testJsonValueCreatorHierarchicalEnumRenameOnDeserialize() throws Exception
244 {
245 String jsonA = a2q("\"A\"");
246 SubclassingEnum resultA = JSON_WITH_ANNO.beanFrom(SubclassingEnum.class, jsonA);
247 assertEquals(SubclassingEnum.ENUM_A, resultA);
248
249 String jsonB = a2q("\"B\"");
250 SubclassingEnum resultB = JSON_WITH_ANNO.beanFrom(SubclassingEnum.class, jsonB);
251 assertEquals(SubclassingEnum.ENUM_B, resultB);
252 }
253
254 public void testJsonValueHidingSubclass() throws Exception
255 {
256 SubclassingEnum input = SubclassingEnum.ENUM_NO_JSON_VALUE;
257 // default
258 assertEquals("\"ENUM_NO_JSON_VALUE\"", JSON.std.asString(input));
259 // with annotations
260 assertEquals(a2q("\"ENUM_NO_JSON_VALUE\""), JSON_WITH_ANNO.asString(input));
261 }
262 }