Java
Jackson의 모든 것 - Property 편
Jaime.Lee
2019. 11. 20. 15:12
모든 소스는 여기서 확인하실 수 있습니다.
@JsonIgnoreProperties
@JsonIgnoreProperties는 Jackson이 무시할 속성 또는 속성 리스트를 나타냅니다. 문자열 배열 형식으로 나타낼 수 있습니다.
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties("password")
public class BeanWithIgnoreProperties {
private String id;
private String name;
private String password;
}
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
public class BeanWithIgnorePropertiesTests {
@Test
public void WhenJsonIgnorePropertiesProvided_ThenSerialize_ExpectIgnored() throws JsonProcessingException {
// when
BeanWithIgnoreProperties bean = new BeanWithIgnoreProperties();
bean.setId("id");
bean.setName("name");
bean.setPassword("password");
// then
String json = new ObjectMapper().writeValueAsString(bean);
// expected
assertThat(json, not(containsString("password")));
System.out.println(json);
}
}
Testing started at 3:07 PM ...
> Task :jackson:cleanTest
> Task :jackson:compileJava
> Task :jackson:processResources NO-SOURCE
> Task :jackson:classes
> Task :jackson:compileTestJava
> Task :jackson:processTestResources NO-SOURCE
> Task :jackson:testClasses
> Task :jackson:test
{"id":"id","name":"name"}
BUILD SUCCESSFUL in 10s
4 actionable tasks: 4 executed
3:07:37 PM: Tasks execution finished ':jackson:cleanTest :jackson:test --tests "io.lcalmsky.jackson.domain.BeanWithIgnorePropertiesTests.WhenJsonIgnorePropertiesProvided_ThenSerialize_ExpectIgnored"'.
password 필드를 무시하라고 명시하였으므로 serialize 하였을 때 password 필드가 누락된 것을 확인할 수 있습니다.
@JsonIgnore
@JsonIgnoreProperties와 동일한 기능을 하지만 주로 필드변수에 직접 사용합니다.
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BeanWithIgnore {
private String id;
private String name;
@JsonIgnore
private String password;
}
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
public class BeanWithIgnoreTests {
@Test
public void WhenJsonIgnoreProvided_ThenSerialize_ExpectIgnored() throws JsonProcessingException {
// when
BeanWithIgnore bean = new BeanWithIgnore();
bean.setId("id");
bean.setName("name");
bean.setPassword("password");
// then
String json = new ObjectMapper().writeValueAsString(bean);
// expected
assertThat(json, not(containsString("password")));
System.out.println(json);
}
}
Testing started at 8:49 PM ...
Gradle Daemon started in 759 ms
> Task :jackson:cleanTest
> Task :jackson:compileJava
> Task :jackson:processResources NO-SOURCE
> Task :jackson:classes
> Task :jackson:compileTestJava
> Task :jackson:processTestResources NO-SOURCE
> Task :jackson:testClasses
> Task :jackson:test
{"id":"id","name":"name"}
BUILD SUCCESSFUL in 10s
4 actionable tasks: 4 executed
8:49:10 PM: Tasks execution finished ':jackson:cleanTest :jackson:test --tests "io.lcalmsky.jackson.domain.BeanWithIgnoreTests"'.
@JsonIgnoreType
어노테이션이있는 유형의 모든 특성이 무시되도록 표시합니다.
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BeanWithJsonIgnoreType {
private int id;
private Name name;
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreType
public static class Name {
private String firstName;
private String lastName;
}
}
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
public class BeanWithJsonIgnoreTypeTests {
@Test
public void WhenJsonIgnoreTypeProvided_ThenSerialize_ExpectedIgnored() throws JsonProcessingException {
// when
BeanWithJsonIgnoreType.Name name = new BeanWithJsonIgnoreType.Name();
name.setFirstName("Jungmin");
name.setLastName("Lee");
BeanWithJsonIgnoreType bean = new BeanWithJsonIgnoreType();
bean.setId(1);
bean.setName(name);
// then
String json = new ObjectMapper().writeValueAsString(bean);
// expected
System.out.println(json);
Assert.assertThat(json, CoreMatchers.not(CoreMatchers.containsString("Jungmin")));
Assert.assertThat(json, CoreMatchers.not(CoreMatchers.containsString("Lee")));
Assert.assertThat(json, CoreMatchers.not(CoreMatchers.containsString("name")));
}
}
Testing started at 10:46 AM ...
> Task :jackson:cleanTest
> Task :jackson:compileJava
> Task :jackson:processResources NO-SOURCE
> Task :jackson:classes
> Task :jackson:compileTestJava
> Task :jackson:processTestResources NO-SOURCE
> Task :jackson:testClasses
> Task :jackson:test
{"id":1}
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
10:46:44 AM: Tasks execution finished ':jackson:cleanTest :jackson:test --tests "io.lcalmsky.jackson.domain.BeanWithJsonIgnoreTypeTests.WhenJsonIgnoreTypeProvided_ThenSerialize_ExpectedIgnored"'.
@JsonInclude
비어있거나(empty), null이거나 기본값인 속성을 포함하거나 제외하기위해 사용합니다.
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BeanWithJsonInclude {
private Integer id;
private String name;
}
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
public class BeanWithJsonIncludeTests {
@Test
public void WhenBeanWithJsonIncludeProvided_ThenSerialize_ExpectCorrect() throws JsonProcessingException {
// when
BeanWithJsonInclude beanWithId = new BeanWithJsonInclude();
beanWithId.setId(1);
beanWithId.setName(null);
BeanWithJsonInclude beanWithName = new BeanWithJsonInclude();
beanWithName.setId(null);
beanWithName.setName("name");
// then
ObjectMapper objectMapper = new ObjectMapper();
String jsonWithId = objectMapper.writeValueAsString(beanWithId);
String jsonWithName = objectMapper.writeValueAsString(beanWithName);
// expected
System.out.println(jsonWithId);
System.out.println(jsonWithName);
assertThat(jsonWithId, not(containsString("name")));
assertThat(jsonWithName, not(containsString("id")));
}
}
Testing started at 10:58 AM ...
> Task :jackson:cleanTest
> Task :jackson:compileJava
> Task :jackson:processResources NO-SOURCE
> Task :jackson:classes
> Task :jackson:compileTestJava
> Task :jackson:processTestResources NO-SOURCE
> Task :jackson:testClasses
> Task :jackson:test
{"id":1}
{"name":"name"}
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
10:58:08 AM: Tasks execution finished ':jackson:cleanTest :jackson:test --tests "io.lcalmsky.jackson.domain.BeanWithJsonIncludeTests.WhenBeanWithJsonIncludeProvided_ThenSerialize_ExpectCorrect"'.
@JsonAutoDetect
visibility에 따라 표시 여부를 결정합니다.
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NON_PRIVATE)
public class BeanWithJsonAutoDetect {
public int id;
protected String name;
private String password;
public BeanWithJsonAutoDetect(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
package io.lcalmsky.jackson.domain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
public class BeanWithJsonAutoDetectTests {
@Test
public void WhenBeanHasJsonAutoDetectAnnotation_ThenSerialize_ExpectCorrect() throws JsonProcessingException {
// when
BeanWithJsonAutoDetect bean = new BeanWithJsonAutoDetect(1, "Jungmin", "password");
// then
String json = new ObjectMapper().writeValueAsString(bean);
// expected
System.out.println(json);
assertThat(json, not(containsString("password")));
}
}
Testing started at 11:12 AM ...
> Task :jackson:cleanTest
> Task :jackson:compileJava
> Task :jackson:processResources NO-SOURCE
> Task :jackson:classes
> Task :jackson:compileTestJava
> Task :jackson:processTestResources NO-SOURCE
> Task :jackson:testClasses
> Task :jackson:test
{"id":1,"name":"Jungmin"}
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
11:12:54 AM: Tasks execution finished ':jackson:cleanTest :jackson:test --tests "io.lcalmsky.jackson.domain.BeanWithJsonAutoDetectTests.WhenBeanHasJsonAutoDetectAnnotation_ThenSerialize_ExpectCorrect"'.