當我序列化/反序列化任何對象時,所有字段名都轉換爲小寫。 是否有任何配置可以讓Jackson將字段名稱保持原樣?序列化和反序列化?Jackson ObjectMapper大/小寫問題
(我知道@JsonProperty,但這並不似乎是正確的,因爲我需要的僅僅是傑克遜尊重已經存在的東西)
我的測試代碼:
import java.io.Serializable;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class Test {
static class Example implements Serializable {
private String Test;
private String ABC;
private String XyZ;
public String getTest() { return Test; }
public void setTest(String test) { Test = test; }
public String getABC() { return ABC; }
public void setABC(String abc) { ABC = abc; }
public String getXyZ() { return XyZ; }
public void setXyZ(String xyz) { XyZ = xyz; }
}
static class MyPropertyNamingStrategy 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);
}
private String convert(String input) {
return input;
}
}
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(new MyPropertyNamingStrategy())
.enable(SerializationFeature.INDENT_OUTPUT)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
//From OBJECT to JSON
Example ex = new Example();
ex.setTest("1");
ex.setABC("2");
ex.setXyZ("3");
System.out.println(objectMapper.writeValueAsString(ex));
//FROM JSON to OBJECT
String jsonString = "{ \"Test\":\"0\", \"ABC\":\"1\", \"XyZ\":\"2\" }";
Example fEx = objectMapper.readValue(jsonString, Example.class);
}
}
謝謝到@ BlueLettuce16,我已經設法構建了一個'改進'版本的PropertyNamingStrategy
。那就是:
import java.lang.reflect.Modifier;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class CustomPropertyNamingStrategy extends PropertyNamingStrategy {
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return convertForField(defaultName);
}
@Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convertForMethod(method, defaultName);
}
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convertForMethod(method, defaultName);
}
private String convertForField(String defaultName) {
return defaultName;
}
private String convertForMethod(AnnotatedMethod method, String defaultName) {
if (isGetter(method)) {
return method.getName().substring(3);
}
if (isSetter(method)) {
return method.getName().substring(3);
}
return defaultName;
}
private boolean isGetter(AnnotatedMethod method) {
if (Modifier.isPublic(method.getModifiers()) && method.getGenericParameterTypes().length == 0) {
if (method.getName().matches("^get[A-Z].*") && !method.getGenericReturnType().equals(void.class))
return true;
if (method.getName().matches("^is[A-Z].*") && method.getGenericReturnType().equals(boolean.class))
return true;
}
return false;
}
private boolean isSetter(AnnotatedMethod method) {
return Modifier.isPublic(method.getModifiers()) && method.getGenericReturnType().equals(void.class) && method.getGenericParameterTypes().length == 1
&& method.getName().matches("^set[A-Z].*");
}
}
實現自定義['PropertyNamingStrategy'](https://github.com/FasterXML/jackson-databind/ blob/master/src/main/java/com/fasterxml/jackson/databind/PropertyNamingStrategy.java),也許 – 2014-11-04 21:30:39
你是如何使用@JsonProperty的?你傳遞給它的字符串字段名稱? – 2014-11-04 21:45:03
我沒有看到實現PropertyNamingStrategy以正確匹配字段名稱的方法。如果一個字段被命名爲「ABC」,另一個命名爲XYz,則在序列化/反序列化時,它們將不匹配JSON。 – MBarni 2014-11-05 10:50:20