java 使用Jackson将带有下划线的JSON反序列化为Java中的驼峰案例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38809959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:47:31  来源:igfitidea点击:

Deserialising JSON with underscores to camel case in Java using Hymanson?

javajsonHymanson

提问by Ram Patra

To serialize java objects with camel case attributes to json with underscore we use PropertyNamingStrategyas SNAKE_CASE.

要将具有驼峰式大小写属性的 java 对象序列化为带有下划线的 json,我们使用PropertyNamingStrategyas SNAKE_CASE

So, is there something to do the opposite as well. That is, deserialise json with underscore to Java objects with camel case.

那么,是否也有相反的方法。也就是说,将带有下划线的 json 反序列化为带有驼峰式大小写的 Java 对象。

For example, JSON:

例如,JSON:

{
    "user_id": "213sdadl"
    "channel_id": "asd213l"
}

should be deserialised to this Java object:

应该反序列化为这个 Java 对象:

public class User {
    String userId; // should have value "213sdadl"
    String channelId; // should have value "asd213l"
}

I know one way of doing thiswhich is via @JsonPropertyannotation which works at field level. I am interested in knowing any global setting for this.

我知道这样做的一种方法是通过@JsonProperty在字段级别工作的注释。我有兴趣了解任何全局设置。

采纳答案by Sohlowmawn

Well, you can have an implementation for PropertyNamingStrategy that looks something like this:

好吧,你可以有一个 PropertyNamingStrategy 的实现,看起来像这样:

import org.codehaus.Hymanson.map.MapperConfig;
import org.codehaus.Hymanson.map.PropertyNamingStrategy;
import org.codehaus.Hymanson.map.introspect.AnnotatedField;
import org.codehaus.Hymanson.map.introspect.AnnotatedMethod;

public class CustomNameStrategy extends PropertyNamingStrategy {
    @Override
    public String nameForField(MapperConfig config, AnnotatedField field, String defaultName) {
        return convert(defaultName);
    }

    @Override
    public String nameForGetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName) {
        return convert(defaultName);
    }

    @Override
    public String nameForSetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName) {
        return convert(defaultName);
    }

    public String convert(String defaultName) {
        char[] arr = defaultName.toCharArray();
        StringBuilder nameToReturn = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == '_') {
                nameToReturn.append(Character.toUpperCase(arr[i + 1]));
                i++;
            } else {
                nameToReturn.append(arr[i]);
            }
        }
        return nameToReturn.toString();
    }
}

then in your implementation or the class that does the desrialization you can have:

然后在您的实现或执行反序列化的类中,您可以拥有:

ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new CustomNameStrategy());
YourClass yourClass = mapper.readValue(yourJsonString, YourClass.class);