Skip to content

Commit 6814284

Browse files
committed
Complete merging #2241
1 parent 9aad873 commit 6814284

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project: jackson-databind
2828
#2223: Add `missingNode()` method in `JsonNodeFactory`
2929
#2227: Minor cleanup of exception message for `Enum` binding failure
3030
(reported by RightHandedMonkey@github)
31+
#2241: Add `JsonPropertyNamingStrategy.LOWER_DOT_CASE` for dot-delimited names
32+
(contributed by [email protected])
3133

3234
2.9.9 (not yet released)
3335

src/main/java/com/fasterxml/jackson/databind/PropertyNamingStrategy.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,47 @@ public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParam
203203
}
204204

205205
public abstract String translate(String propertyName);
206-
}
207-
206+
207+
/**
208+
* Helper method to share implementation between snake and dotted case.
209+
*/
210+
protected static String translateLowerCaseWithSeparator(final String input, final char separator)
211+
{
212+
if (input == null) {
213+
return input; // garbage in, garbage out
214+
}
215+
final int length = input.length();
216+
if (length == 0) {
217+
return input;
218+
}
208219

220+
final StringBuilder result = new StringBuilder(length + (length >> 1));
221+
int upperCount = 0;
222+
for (int i = 0; i < length; ++i) {
223+
char ch = input.charAt(i);
224+
char lc = Character.toLowerCase(ch);
225+
226+
if (lc == ch) { // lower-case letter means we can get new word
227+
// but need to check for multi-letter upper-case (acronym), where assumption
228+
// is that the last upper-case char is start of a new word
229+
if (upperCount > 1) {
230+
// so insert hyphen before the last character now
231+
result.insert(result.length() - 1, separator);
232+
}
233+
upperCount = 0;
234+
} else {
235+
// Otherwise starts new word, unless beginning of string
236+
if ((upperCount == 0) && (i > 0)) {
237+
result.append(separator);
238+
}
239+
++upperCount;
240+
}
241+
result.append(lc);
242+
}
243+
return result.toString();
244+
}
245+
}
246+
209247
/*
210248
/**********************************************************
211249
/* Standard implementations
@@ -369,40 +407,8 @@ public String translate(String input) {
369407
public static class KebabCaseStrategy extends PropertyNamingStrategyBase
370408
{
371409
@Override
372-
public String translate(String input)
373-
{
374-
if (input == null) return input; // garbage in, garbage out
375-
int length = input.length();
376-
if (length == 0) {
377-
return input;
378-
}
379-
380-
StringBuilder result = new StringBuilder(length + (length >> 1));
381-
382-
int upperCount = 0;
383-
384-
for (int i = 0; i < length; ++i) {
385-
char ch = input.charAt(i);
386-
char lc = Character.toLowerCase(ch);
387-
388-
if (lc == ch) { // lower-case letter means we can get new word
389-
// but need to check for multi-letter upper-case (acronym), where assumption
390-
// is that the last upper-case char is start of a new word
391-
if (upperCount > 1) {
392-
// so insert hyphen before the last character now
393-
result.insert(result.length() - 1, '-');
394-
}
395-
upperCount = 0;
396-
} else {
397-
// Otherwise starts new word, unless beginning of string
398-
if ((upperCount == 0) && (i > 0)) {
399-
result.append('-');
400-
}
401-
++upperCount;
402-
}
403-
result.append(lc);
404-
}
405-
return result.toString();
410+
public String translate(String input) {
411+
return translateLowerCaseWithSeparator(input, '-');
406412
}
407413
}
408414

@@ -413,16 +419,10 @@ public String translate(String input)
413419
* @since 2.10
414420
*/
415421
public static class LowerDotCaseStrategy extends PropertyNamingStrategyBase {
416-
/*
417422
@Override
418423
public String translate(String input){
419424
return translateLowerCaseWithSeparator(input, '.');
420425
}
421-
*/
422-
@Override
423-
public String translate(String input) {
424-
return input.toLowerCase();
425-
}
426426
}
427427

428428
/*

src/test/java/com/fasterxml/jackson/databind/introspect/TestNamingStrategyStd.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,12 @@ public void testSimpleKebabCase() throws Exception
336336
FirstNameBean.class);
337337
assertEquals("Billy", result.firstName);
338338
}
339-
340339
/*
341340
/**********************************************************
342341
/* Test methods for LOWER_DOT_CASE
343342
/**********************************************************
344343
*/
345344

346-
/*
347345
public void testLowerCaseWithDotsStrategyStandAlone()
348346
{
349347
assertEquals("some.value",
@@ -371,7 +369,6 @@ public void testSimpleLowerCaseWithDots() throws Exception
371369
FirstNameBean.class);
372370
assertEquals("Billy", result.firstName);
373371
}
374-
*/
375372

376373
/*
377374
/**********************************************************

0 commit comments

Comments
 (0)