직렬화 및 직렬화 해제 중 JSON 속성의 다른 이름
Jackson 라이브러리에서 직렬화/비직렬화 중에 클래스 내에서 하나의 필드를 다른 이름으로 만들 수 있습니까?
예를 들어, 저는 "Coordiantes"라는 수업이 있습니다.
class Coordinates{
int red;
}
JSON으로부터의 역직렬화의 경우는, 다음과 같은 형식을 사용합니다.
{
"red":12
}
그러나 개체를 직렬화하면 다음과 같은 결과가 나타납니다.
{
"r":12
}
기능을 ★★★★★★★★★★을@JsonProperty
getter "setter" "setter" "setter" "setter"(다음에) :
class Coordiantes{
int red;
@JsonProperty("r")
public byte getRed() {
return red;
}
@JsonProperty("red")
public void setRed(byte red) {
this.red = red;
}
}
하지만 예외는 있습니다.
org.codehouse.disc.map.exc 를 지정합니다.인식할 수 없는 속성 예외:인식할 수 없는 필드 "빨간색"
방금 테스트한 결과, 다음과 같이 동작합니다.
public class Coordinates {
byte red;
@JsonProperty("r")
public byte getR() {
return red;
}
@JsonProperty("red")
public void setRed(byte red) {
this.red = red;
}
}
메서드 이름은 달라야 하므로 잭슨은 이를 하나의 필드가 아닌 다른 필드로 해석합니다.
테스트 코드는 다음과 같습니다.
Coordinates c = new Coordinates();
c.setRed((byte) 5);
ObjectMapper mapper = new ObjectMapper();
System.out.println("Serialization: " + mapper.writeValueAsString(c));
Coordinates r = mapper.readValue("{\"red\":25}",Coordinates.class);
System.out.println("Deserialization: " + r.getR());
결과:
Serialization: {"r":5}
Deserialization: 25
하시면 됩니다.@jsonAlias
2.9되었습니다.
예:
public class Info {
@JsonAlias({ "red" })
public String r;
}
은 「」를 사용합니다.r
「」, 「」, 「」를합니다.red
디시리얼라이제이션중에 에일리어스로서 사용됩니다. 해도 아직 할 수 있다.r
탈직렬화 될 수도 있어요
@JsonSetter와 @JsonGetter를 조합하여 속성의 역직렬화와 시리얼화를 각각 제어할 수 있습니다.이를 통해 실제 필드 이름에 대응하는 표준화된 getter 및 setter 메서드 이름을 유지할 수도 있습니다.
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonGetter;
class Coordinates {
private int red;
//# Used during serialization
@JsonGetter("r")
public int getRed() {
return red;
}
//# Used during deserialization
@JsonSetter("red")
public void setRed(int red) {
this.red = red;
}
}
2개의 다른 getter/setters 쌍을 1개의 변수로 바인드합니다.
class Coordinates{
int red;
@JsonProperty("red")
public byte getRed() {
return red;
}
public void setRed(byte red) {
this.red = red;
}
@JsonProperty("r")
public byte getR() {
return red;
}
public void setR(byte red) {
this.red = red;
}
}
getter / setter 쌍쌍ter 、 getter / setter 。.@JsonProperty
이를 위한 유닛 테스트를 다음에 나타냅니다.
public class JsonPropertyTest {
private static class TestJackson {
private String color;
@JsonProperty(value = "device_color", access = JsonProperty.Access.READ_ONLY)
public String getColor() {
return color;
};
@JsonProperty(value = "color", access = JsonProperty.Access.WRITE_ONLY)
public void setColor(String color) {
this.color = color;
}
}
@Test
public void shouldParseWithAccessModeSpecified() throws Exception {
String colorJson = "{\"color\":\"red\"}";
ObjectMapper mapper = new ObjectMapper();
TestJackson colotObject = mapper.readValue(colorJson, TestJackson.class);
String ser = mapper.writeValueAsString(colotObject);
System.out.println("Serialized colotObject: " + ser);
}
}
저는 다음과 같은 출력을 받았습니다.
Serialized colotObject: {"device_color":"red"}
다음 배리언트를 사용할 수 있습니다.
import lombok.Getter;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
//...
@JsonProperty(value = "rr") // for deserialization
@Getter(onMethod_ = {@JsonGetter(value = "r")}) // for serialization
private String rrrr;
Lombok getter와 함께
이것은, 솔루션으로서 기대했던 것은 아니었습니다(합법적인 사용 예이긴 하지만).기존 버그 클라이언트(이미 출시된 모바일 앱)에서 대체 이름을 사용할 수 있도록 해야 했습니다.
해결책은 다음과 같은 별도의 세터 방식을 제공하는 것입니다.
@JsonSetter( "r" )
public void alternateSetRed( byte red ) {
this.red = red;
}
" "로 주석 "@JsonAlias
2.9+에 대해 언급하지 2.9에서 되었습니다.@JsonProperty
둘 이상의 에일리어스(json 속성의 다른 이름)로 역직렬화할 항목에서 올바르게 작동합니다.
하였습니다.com.fasterxml.jackson.annotation.JsonAlias
의 을 확보하기 com.fasterxml.jackson.databind.ObjectMapper
제 사용 사례로요.
예:
@Data
@Builder
public class Chair {
@JsonAlias({"woodenChair", "steelChair"})
private String entityType;
}
@Test
public void test1() {
String str1 = "{\"woodenChair\":\"chair made of wood\"}";
System.out.println( mapper.readValue(str1, Chair.class));
String str2 = "{\"steelChair\":\"chair made of steel\"}";
System.out.println( mapper.readValue(str2, Chair.class));
}
잘 작동합니다.
, 라이브러리와 , , Gson을 사용해 .그래서 Gson을 사용하고 있다면,@SerializedName("name")
대신@JsonProperty("name")
이것이 도움이 되기를 바란다
기능으로서 이 기능을 포함시켰을 것입니다.왜냐하면 이제 다른 설정을 하기 때문입니다.@JsonProperty
getter와 setter의 결과는 예상대로입니다(같은 필드에 대해 시리얼화 및 역직렬화 시 다른 속성 이름).잭슨 버전 2.6.7
제 경우 브라질 포르투갈어로 된 입력을 읽고 영어로 된 출력을 생성해야 했습니다.
그래서 저에게 효과가 있는 해결방법이@JsonAlias
대신@JsonProperty
:
// pseudo-java
@Value
public class User {
String username;
public User(
@JsonAlias("nome_usuario") String username) {
// ...
}
}
이를 위해 serialize 클래스를 작성할 수 있습니다.
public class Symbol
{
private String symbol;
private String name;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class SymbolJsonSerializer extends JsonSerializer<Symbol> {
@Override
public void serialize(Symbol symbol, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("symbol", symbol.getSymbol());
//Changed name to full_name as the field name of Json string
jgen.writeStringField("full_name", symbol.getName());
jgen.writeEndObject();
}
}
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Symbol.class, new SymbolJsonSerializer());
mapper.registerModule(module);
//only convert non-null field, option...
mapper.setSerializationInclusion(Include.NON_NULL);
String jsonString = mapper.writeValueAsString(symbolList);
언급URL : https://stackoverflow.com/questions/8560348/different-names-of-json-property-during-serialization-and-deserialization
'programing' 카테고리의 다른 글
여러 탭을 열 때 Nuxt 브라우저가 충돌합니다. (0) | 2022.07.05 |
---|---|
Java 형식의 yyy-MM-dd 형식의 달력 날짜 (0) | 2022.07.05 |
TypeError: 정의되지 않은 저장소 디스패치 vuex의 속성 'token'을 읽을 수 없습니다. (0) | 2022.07.05 |
쿠키가 서버에서 캐시하는 vuej 및 vuex에 http only 플래그를 사용하여 쿠키를 설정하는 방법(Laravel) (0) | 2022.07.05 |
Vuex Store 및 계산된 속성을 사용하여 컴포넌트에 전달된 객체 배열을 루프하려면 어떻게 해야 합니까? (0) | 2022.07.05 |