Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The priority of json_name should be higher than PropertyNamingStrategy #127

Open
extraSix opened this issue Jul 24, 2024 · 0 comments
Open

Comments

@extraSix
Copy link

extraSix commented Jul 24, 2024

jackson-datatype-protobuf: 0.9.15
jackson-databind: 2.13.2.2
protobuf-java: 3.19.2

message Test {
    string deviceId = 1 [json_name = "deviceId"];
}

For example, even if I set [json_name = "deviceId"], the JSON still displays "deviceid",since json_name == fieldName json_name is not effective, the JSON still displays "deviceid" due to the SnakeToCamelNamingStrategy. I looked into the relevant issue where ‘json_name == fieldName json_name is not effective’ change started from #103

I think json_name is a special strategy and its priority should be higher than the general strategy - PropertyNamingStrategy. To draw a comparison: in Jackson, the priority of @JsonProperty is higher than PropertyNamingStrategy.

I know that setting PropertyNamingStrategy.LOWER_CAMEL_CASE can solve this issue. However, due to some historical reasons, some fields in protobuf classes are in LOWER_CAMEL_CASE format while others are in LOWER_UNDERSCORE format. Therefore, PropertyNamingStrategy.LOWER_CAMEL_CASE is not suitable.

BTW, I can temporarily resolve this by customizing a PropertyNamingStrategy.

    private static class TryLowerUnderscoreToLowerCamelCaseStrategy extends NamingBase {

        @Override
        public String translate(String fieldName) {
            // deviceId -> deviceId
            if (containsUpperCase(fieldName) && !fieldName.contains("_")) {
                return fieldName;
            }
            /*
            device_id -> deviceId
            deviceId -> deviceid
             */
            return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, fieldName);
        }

        private static boolean containsUpperCase(String str) {
            for (char c : str.toCharArray()) {
                if (Character.isUpperCase(c)) {
                    return true;
                }
            }
            return false;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant