[英]JSON string serialization ( to camel case ) and deserialization (from snake case )using Jackson
我試圖將json字符串反序列化為POJO,然后使用Jackson將其序列化回json字符串,但是在此過程中,我希望生成的json字符串已更改鍵值。
例如輸入json字符串:
{"some_key":"value"}
這是我的POJO的樣子
public class Sample {
@JsonProperty("some_key")
private String someKey;
public String getSomeKey(){
return someKey ;
};
}
當我再次序列化它時,我希望json字符串是這樣的
{"someKey":"value"} .
有什么辦法可以實現?
我可以根據輸入的json字符串重命名setter函數來進行反序列化。
class Test{
private String someKey;
// for deserializing from field "some_key"
public void setSome_key( String someKey) {
this.someKey = someKey;
}
public String getSomeKey(){
return someKey;
}
}
您應該能夠通過定義反序列化的創建者來實現這一點,然后讓Jackson進行序列化的默認行為。
public class Sample {
private final String someKey;
@JsonCreator
public Sample(@JsonProperty("some_key") String someKey) {
this.someKey = someKey;
}
// Should serialize as "someKey" by default
public String getSomeKey(){
return someKey;
}
}
您可能需要在ObjectMapper
上禁用MapperFeature.AUTO_DETECT_CREATORS
才能起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.