Jackson ObjectMapper
- Java에서 Json를 다룰때 주로 사용하는 Jackson ObjectMapper에 대해 정리를 해보자.
Gradle
ObjectMapper를 이용한 Json Write, Read
Json Write
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "bmw");
String carAsString = objectMapper.writeValueAsString(car);
Json Read
Annotation
@JsonProperty
@JsonSerialize
@JsonDeserialize
@JsonIgnore
public class BeanWithIgnore {
@JsonIgnore
public int id;
public String name;
}
@JsonIgnoreProperties({ "id" })
@JsonIgnoreProperties({ "id" })
public class BeanWithIgnore {
public int id;
public String name;
}
@JsonAnyGetter
public class ExtendableBean {
public String name;
private Map<String, String> properties;
@JsonAnyGetter
public Map<String, String> getProperties() {
return properties;
}
}
{
"name":"My bean",
"attr2":"val2",
"attr1":"val1"
}
@JsonAnySetter
public class ExtendableBean {
public String name;
private Map<String, String> properties;
@JsonAnySetter
public void add(String key, String value) {
properties.put(key, value);
}
}
public class ExtendableBean {
public String name;
private Map<String, String> properties;
@JsonAnySetter
public void add(String key, String value) {
properties.put(key, value);
}
}
'Programing > Java' 카테고리의 다른 글
URI (0) | 2018.11.22 |
---|---|
Queue 자료 구조 (0) | 2018.11.12 |
Java Jdbc를 이용한 데이터베이스 접근하기 (0) | 2018.10.23 |
Guava Eventbus @AllowConcurrentEvents (0) | 2018.10.10 |
CompletableFuture (0) | 2018.09.15 |