Jackson JSON Processor
  1. Jackson JSON Processor
  2. JACKSON-598

Add Built-In PropertyNamingStrategy for Camel Case to Lower Case with Underscores

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: ObjectMapper
    • Labels:
      None
    • Number of attachments :
      3

      Description

      For convenience, Jackson should provide PropertyNamingStrategy implementation that translates from typical camel case Java property names to lower case JSON element names separated with underscores.

      In detail, as discussed in the mailing list, this PropertyNamingStrategy should be implemented according to the following description.

      Except for contiguous sequences of uppercase letters, every uppercase character from the Java identifier is translated into two characters: the first an underscore, and the second the lowercase equivalent of the target character. For contiguous sequences of uppercase letters, only the first character is replaced with an underscore and its lowercase equivalent, with each subsequent uppercase letter replaced by just its lowercase equivalent.

      This would result in the following example translations, from Java
      identifier names to JSON element names.

      Java Identifier Name -> JSON Element Name
                   results -> "results" // no change
                  fromUser -> "from_user"
                   Results -> "_results"
                 from_user -> "from_user" // no change
                     _user -> "_user" // no change
                     _User -> "__user" // two underscores
                     myURI -> "my_uri"
                    myURIs -> "my_uris"
                 UGLY_NAME -> "_ugly_name"
                 from$user -> "from$user" // no change
                 from7user -> "from7user" // no change
                         _ -> "_" // no change

        Activity

        Hide
        Programmer Bruce added a comment - - edited

        Attached suggested implementations for this enhancement request. (Followed coding style of existing code base.)

        Show
        Programmer Bruce added a comment - - edited Attached suggested implementations for this enhancement request. (Followed coding style of existing code base.)
        Hide
        Programmer Bruce added a comment - - edited

        Attached implementation according to the following description, per conversation in the mailing list.

        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.
          1. 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".
          2. 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".
          3. 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)
        Show
        Programmer Bruce added a comment - - edited Attached implementation according to the following description, per conversation in the mailing list. 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)
        Hide
        Tatu Saloranta added a comment -

        Added this on http://wiki.fasterxml.com/JacksonRelease19 page, planned to be included in 1.9.

        One of next steps is figuring out how to plug these in; can either just add classes, document. Alternative would be to add separate methods, but there's concern of adding too many methods in ObjectMapper.

        Show
        Tatu Saloranta added a comment - Added this on http://wiki.fasterxml.com/JacksonRelease19 page, planned to be included in 1.9. One of next steps is figuring out how to plug these in; can either just add classes, document. Alternative would be to add separate methods, but there's concern of adding too many methods in ObjectMapper.
        Hide
        Tatu Saloranta added a comment -

        Was included in 1.9.

        Show
        Tatu Saloranta added a comment - Was included in 1.9.

          People

          • Assignee:
            Tatu Saloranta
            Reporter:
            Programmer Bruce
          • Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: