@@ -24,7 +24,7 @@ static class Acronyms
24
24
public String WWW ;
25
25
public String someURL ;
26
26
public String someURIs ;
27
-
27
+
28
28
public Acronyms () {this (null , null , null );}
29
29
public Acronyms (String WWW , String someURL , String someURIs )
30
30
{
@@ -33,7 +33,7 @@ public Acronyms(String WWW, String someURL, String someURIs)
33
33
this .someURIs = someURIs ;
34
34
}
35
35
}
36
-
36
+
37
37
@ JsonPropertyOrder ({"from_user" , "user" , "from$user" , "from7user" , "_x" })
38
38
static class UnchangedNames
39
39
{
@@ -43,7 +43,7 @@ static class UnchangedNames
43
43
public String from7user ;
44
44
// Used to test "_", but it's explicitly deprecated in JDK8 so...
45
45
public String _x ;
46
-
46
+
47
47
public UnchangedNames () {this (null , null , null , null , null );}
48
48
public UnchangedNames (String from_user , String _user , String from$user , String from7user , String _x )
49
49
{
@@ -84,7 +84,7 @@ static class BoringBean {
84
84
public String firstName = "Bob" ;
85
85
public String lastName = "Burger" ;
86
86
}
87
-
87
+
88
88
public static class ClassWithObjectNodeField {
89
89
public String id ;
90
90
public ObjectNode json ;
@@ -206,21 +206,80 @@ protected SnakeNameBean(@Name("id") String id,
206
206
{"xCoordinate" , "x_coordinate" },
207
207
});
208
208
209
+ final static List <Object []> UPPER_SNAKE_CASE_NAME_TRANSLATIONS = Arrays .asList (new Object [][] {
210
+ {null , null },
211
+ {"" , "" },
212
+ {"a" , "A" },
213
+ {"abc" , "ABC" },
214
+ {"1" , "1" },
215
+ {"123" , "123" },
216
+ {"1a" , "1A" },
217
+ {"a1" , "A1" },
218
+ {"$" , "$" },
219
+ {"$a" , "$A" },
220
+ {"a$" , "A$" },
221
+ {"$_a" , "$_A" },
222
+ {"a_$" , "A_$" },
223
+ {"a$a" , "A$A" },
224
+ {"$A" , "$_A" },
225
+ {"$_A" , "$_A" },
226
+ {"_" , "_" },
227
+ {"__" , "_" },
228
+ {"___" , "__" },
229
+ {"A" , "A" },
230
+ {"A1" , "A1" },
231
+ {"1A" , "1_A" },
232
+ {"_a" , "A" },
233
+ {"_A" , "A" },
234
+ {"a_a" , "A_A" },
235
+ {"a_A" , "A_A" },
236
+ {"A_A" , "A_A" },
237
+ {"A_a" , "A_A" },
238
+ {"WWW" , "WWW" },
239
+ {"someURI" , "SOME_URI" },
240
+ {"someURIs" , "SOME_URIS" },
241
+ {"Results" , "RESULTS" },
242
+ {"_Results" , "RESULTS" },
243
+ {"_results" , "RESULTS" },
244
+ {"__results" , "_RESULTS" },
245
+ {"__Results" , "_RESULTS" },
246
+ {"___results" , "__RESULTS" },
247
+ {"___Results" , "__RESULTS" },
248
+ {"userName" , "USER_NAME" },
249
+ {"user_name" , "USER_NAME" },
250
+ {"user__name" , "USER__NAME" },
251
+ {"UserName" , "USER_NAME" },
252
+ {"User_Name" , "USER_NAME" },
253
+ {"User__Name" , "USER__NAME" },
254
+ {"_user_name" , "USER_NAME" },
255
+ {"_UserName" , "USER_NAME" },
256
+ {"_User_Name" , "USER_NAME" },
257
+ {"USER_NAME" , "USER_NAME" },
258
+ {"_Bars" , "BARS" },
259
+ {"usId" , "US_ID" },
260
+ {"uId" , "U_ID" },
261
+ {"xCoordinate" , "X_COORDINATE" },
262
+ });
263
+
209
264
private final ObjectMapper VANILLA_MAPPER = newJsonMapper ();
210
265
211
- private final ObjectMapper _lcWithUndescoreMapper = JsonMapper .builder ()
266
+ private final ObjectMapper _lcWithUnderscoreMapper = JsonMapper .builder ()
212
267
.propertyNamingStrategy (PropertyNamingStrategies .SNAKE_CASE )
213
268
.build ();
214
269
270
+ private final ObjectMapper _ucWithUnderscoreMapper = JsonMapper .builder ()
271
+ .propertyNamingStrategy (PropertyNamingStrategies .UPPER_SNAKE_CASE )
272
+ .build ();
273
+
215
274
/*
216
275
/**********************************************************
217
276
/* Test methods for SNAKE_CASE
218
277
/**********************************************************
219
278
*/
220
279
221
280
/**
222
- * Unit test to verify translations of
223
- * {@link PropertyNamingStrategies#SNAKE_CASE}
281
+ * Unit test to verify translations of
282
+ * {@link PropertyNamingStrategies#SNAKE_CASE}
224
283
* outside the context of an ObjectMapper.
225
284
*/
226
285
public void testLowerCaseStrategyStandAlone ()
@@ -235,11 +294,11 @@ public void testLowerCaseStrategyStandAlone()
235
294
public void testLowerCaseTranslations () throws Exception
236
295
{
237
296
// First serialize
238
- String json = _lcWithUndescoreMapper .writeValueAsString (new PersonBean ("Joe" , "Sixpack" , 42 ));
297
+ String json = _lcWithUnderscoreMapper .writeValueAsString (new PersonBean ("Joe" , "Sixpack" , 42 ));
239
298
assertEquals ("{\" first_name\" :\" Joe\" ,\" last_name\" :\" Sixpack\" ,\" age\" :42}" , json );
240
-
299
+
241
300
// then deserialize
242
- PersonBean result = _lcWithUndescoreMapper .readValue (json , PersonBean .class );
301
+ PersonBean result = _lcWithUnderscoreMapper .readValue (json , PersonBean .class );
243
302
assertEquals ("Joe" , result .firstName );
244
303
assertEquals ("Sixpack" , result .lastName );
245
304
assertEquals (42 , result .age );
@@ -248,11 +307,11 @@ public void testLowerCaseTranslations() throws Exception
248
307
public void testLowerCaseAcronymsTranslations () throws Exception
249
308
{
250
309
// First serialize
251
- String json = _lcWithUndescoreMapper .writeValueAsString (new Acronyms ("world wide web" , "http://jackson.codehaus.org" , "/path1/,/path2/" ));
310
+ String json = _lcWithUnderscoreMapper .writeValueAsString (new Acronyms ("world wide web" , "http://jackson.codehaus.org" , "/path1/,/path2/" ));
252
311
assertEquals ("{\" www\" :\" world wide web\" ,\" some_url\" :\" http://jackson.codehaus.org\" ,\" some_uris\" :\" /path1/,/path2/\" }" , json );
253
-
312
+
254
313
// then deserialize
255
- Acronyms result = _lcWithUndescoreMapper .readValue (json , Acronyms .class );
314
+ Acronyms result = _lcWithUnderscoreMapper .readValue (json , Acronyms .class );
256
315
assertEquals ("world wide web" , result .WWW );
257
316
assertEquals ("http://jackson.codehaus.org" , result .someURL );
258
317
assertEquals ("/path1/,/path2/" , result .someURIs );
@@ -261,11 +320,11 @@ public void testLowerCaseAcronymsTranslations() throws Exception
261
320
public void testLowerCaseOtherNonStandardNamesTranslations () throws Exception
262
321
{
263
322
// First serialize
264
- String json = _lcWithUndescoreMapper .writeValueAsString (new OtherNonStandardNames ("Results" , "_User" , "___" , "$User" ));
323
+ String json = _lcWithUnderscoreMapper .writeValueAsString (new OtherNonStandardNames ("Results" , "_User" , "___" , "$User" ));
265
324
assertEquals ("{\" results\" :\" Results\" ,\" user\" :\" _User\" ,\" __\" :\" ___\" ,\" $_user\" :\" $User\" }" , json );
266
-
325
+
267
326
// then deserialize
268
- OtherNonStandardNames result = _lcWithUndescoreMapper .readValue (json , OtherNonStandardNames .class );
327
+ OtherNonStandardNames result = _lcWithUnderscoreMapper .readValue (json , OtherNonStandardNames .class );
269
328
assertEquals ("Results" , result .Results );
270
329
assertEquals ("_User" , result ._User );
271
330
assertEquals ("___" , result .___ );
@@ -275,27 +334,57 @@ public void testLowerCaseOtherNonStandardNamesTranslations() throws Exception
275
334
public void testLowerCaseUnchangedNames () throws Exception
276
335
{
277
336
// First serialize
278
- String json = _lcWithUndescoreMapper .writeValueAsString (new UnchangedNames ("from_user" , "_user" , "from$user" , "from7user" , "_x" ));
337
+ String json = _lcWithUnderscoreMapper .writeValueAsString (new UnchangedNames ("from_user" , "_user" , "from$user" , "from7user" , "_x" ));
279
338
assertEquals ("{\" from_user\" :\" from_user\" ,\" user\" :\" _user\" ,\" from$user\" :\" from$user\" ,\" from7user\" :\" from7user\" ,\" x\" :\" _x\" }" , json );
280
339
281
340
// then deserialize
282
- UnchangedNames result = _lcWithUndescoreMapper .readValue (json , UnchangedNames .class );
341
+ UnchangedNames result = _lcWithUnderscoreMapper .readValue (json , UnchangedNames .class );
283
342
assertEquals ("from_user" , result .from_user );
284
343
assertEquals ("_user" , result ._user );
285
344
assertEquals ("from$user" , result .from$user );
286
345
assertEquals ("from7user" , result .from7user );
287
346
assertEquals ("_x" , result ._x );
288
347
}
289
348
349
+ /*
350
+ /**********************************************************
351
+ /* Test methods for UPPER_SNAKE_CASE
352
+ /**********************************************************
353
+ */
354
+
355
+ public void testUpperSnakeCaseStrategyStandAlone ()
356
+ {
357
+ for (Object [] pair : UPPER_SNAKE_CASE_NAME_TRANSLATIONS ) {
358
+ String translatedJavaName = PropertyNamingStrategies .UPPER_SNAKE_CASE
359
+ .nameForField (null , null , (String ) pair [0 ]);
360
+ assertEquals ((String ) pair [1 ], translatedJavaName );
361
+ }
362
+ }
363
+
364
+ public void testUpperSnakeCaseTranslations () throws Exception
365
+ {
366
+ // First serialize
367
+ String json = _ucWithUnderscoreMapper
368
+ .writeValueAsString (new PersonBean ("Joe" , "Sixpack" , 42 ));
369
+ assertEquals ("{\" FIRST_NAME\" :\" Joe\" ,\" LAST_NAME\" :\" Sixpack\" ,\" AGE\" :42}" , json );
370
+
371
+ // then deserialize
372
+ PersonBean result = _ucWithUnderscoreMapper .readValue (json , PersonBean .class );
373
+ assertEquals ("Joe" , result .firstName );
374
+ assertEquals ("Sixpack" , result .lastName );
375
+ assertEquals (42 , result .age );
376
+ }
377
+
378
+
290
379
/*
291
380
/**********************************************************
292
381
/* Test methods for UPPER_CAMEL_CASE
293
382
/**********************************************************
294
383
*/
295
384
296
385
/**
297
- * Unit test to verify translations of
298
- * {@link PropertyNamingStrategies#UPPER_CAMEL_CASE }
386
+ * Unit test to verify translations of
387
+ * {@link PropertyNamingStrategies#UPPER_CAMEL_CASE }
299
388
* outside the context of an ObjectMapper.
300
389
*/
301
390
public void testPascalCaseStandAlone ()
0 commit comments