A
PropertyNamingStrategy
that translates typical camel case Java
property names to lower case JSON element names, separated by
underscores. This implementation is somewhat lenient, in that it
provides some additional translations beyond strictly translating from
camel case only. In particular, the following translations are applied
by this PropertyNamingStrategy.
- Every upper case letter in the Java property name is translated
into two characters, an underscore and the lower case equivalent of the
target character, with three exceptions.
- For contiguous sequences of upper case letters, characters after
the first character are replaced only by their lower case equivalent,
and are not preceded by an underscore.
- This provides for reasonable translations of upper case acronyms,
e.g., "theWWW" is translated to "the_www".
- An upper case character in the first position of the Java property
name is not preceded by an underscore character, and is translated only
to its lower case equivalent.
- For example, "Results" is translated to "results",
and not to "_results".
- An upper case character in the Java property name that is already
preceded by an underscore character is translated only to its lower case
equivalent, and is not preceded by an additional underscore.
- For example, "user_Name" is translated to
"user_name", and not to "user__name" (with two
underscore characters).
- If the Java property name starts with an underscore, then that
underscore is not included in the translated name, unless the Java
property name is just one character in length, i.e., it is the
underscore character. This applies only to the first character of the
Java property name.
These rules result in the following additional example translations from
Java property names to JSON element names.
- "userName" is translated to "user_name"
- "UserName" is translated to "user_name"
- "USER_NAME" is translated to "user_name"
- "user_name" is translated to "user_name" (unchanged)
- "user" is translated to "user" (unchanged)
- "User" is translated to "user"
- "USER" is translated to "user"
- "_user" is translated to "user"
- "_User" is translated to "user"
- "__user" is translated to "_user"
(the first of two underscores was removed)
- "user__name" is translated to "user__name"
(unchanged, with two underscores)