Skip to content

Commit 1d4b2da

Browse files
BAEL-9174 | Dynamic Ignore with Jackson (#18332)
* BAEL-9174 | Dynamic Ignore with Jackson * BAEL-9174 | fixes * BAEL-9174 | removing finals * BAEL-9174 | removing before each * BAEL-9174 | fixes * BAEL-9174 | when _ then * BAEL-9174 | user - with
1 parent 36e1c75 commit 1d4b2da

File tree

6 files changed

+278
-0
lines changed

6 files changed

+278
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.jackson.dynamicignore;
2+
3+
import com.fasterxml.jackson.annotation.JsonFilter;
4+
5+
@JsonFilter("publicFilter")
6+
public class UserWithFilter {
7+
8+
private Long id;
9+
10+
private String name;
11+
12+
public UserWithFilter(Long id, String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
public UserWithFilter() {
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public Long getId() {
25+
return id;
26+
}
27+
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.jackson.dynamicignore;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
5+
public class UserWithMixin {
6+
7+
private Long id;
8+
9+
private String name;
10+
11+
public UserWithMixin(Long id, String name) {
12+
this.id = id;
13+
this.name = name;
14+
}
15+
16+
public UserWithMixin() {
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
/**
28+
* Mixin interface that is used to hide sensitive information
29+
*/
30+
public interface PublicMixin {
31+
32+
@JsonIgnore
33+
Long getId();
34+
}
35+
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.jackson.dynamicignore;
2+
3+
import com.fasterxml.jackson.annotation.JsonView;
4+
5+
public class UserWithView {
6+
7+
@JsonView(InternalView.class)
8+
private Long id;
9+
10+
@JsonView(PublicView.class)
11+
private String name;
12+
13+
public UserWithView(Long id, String name) {
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
public UserWithView() {
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
/**
30+
* View that is used to hide sensitive information
31+
*/
32+
public interface PublicView {
33+
34+
}
35+
36+
/**
37+
* View that is used to show all information
38+
*/
39+
public interface InternalView extends PublicView {
40+
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.jackson.dynamicignore;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
6+
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class DynamicIgnoreJsonFilterUnitTest {
14+
15+
@Test
16+
void whenWritingWithoutFilter_thenIdIsPresent() throws JsonProcessingException {
17+
UserWithFilter user = new UserWithFilter(1000L, "John");
18+
19+
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
20+
filterProvider.addFilter("publicFilter", SimpleBeanPropertyFilter.serializeAll());
21+
22+
ObjectMapper objectMapper = new ObjectMapper().setFilterProvider(filterProvider);
23+
String result = objectMapper.writeValueAsString(user);
24+
25+
assertThat(result).contains("John");
26+
assertThat(result).contains("1000");
27+
}
28+
29+
@Test
30+
void whenWritingWithFilter_thenIdIsIgnored() throws JsonProcessingException {
31+
UserWithFilter user = new UserWithFilter(1000L, "John");
32+
33+
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
34+
filterProvider.addFilter("publicFilter", SimpleBeanPropertyFilter.serializeAllExcept("id"));
35+
36+
ObjectMapper objectMapper = new ObjectMapper().setFilterProvider(filterProvider);
37+
String result = objectMapper.writeValueAsString(user);
38+
39+
assertThat(result).contains("John");
40+
assertThat(result).doesNotContain("1000");
41+
}
42+
43+
@Test
44+
void whenReadingWithoutFilter_thenIdIsPresent() throws JsonProcessingException {
45+
String json = "{\"id\":1000,\"name\":\"John\"}";
46+
47+
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
48+
filterProvider.addFilter("publicFilter", SimpleBeanPropertyFilter.serializeAll());
49+
50+
ObjectMapper objectMapper = new ObjectMapper().setFilterProvider(filterProvider);
51+
UserWithFilter result = objectMapper.readValue(json, UserWithFilter.class);
52+
53+
assertEquals(1000L, result.getId());
54+
assertEquals("John", result.getName());
55+
}
56+
57+
@Test
58+
void whenReadingWithFilter_thenIdIsStillPresent() throws JsonProcessingException {
59+
String json = "{\"id\":1000,\"name\":\"John\"}";
60+
61+
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
62+
filterProvider.addFilter("publicFilter", SimpleBeanPropertyFilter.serializeAllExcept("id"));
63+
64+
ObjectMapper objectMapper = new ObjectMapper().setFilterProvider(filterProvider);
65+
UserWithFilter result = objectMapper.readValue(json, UserWithFilter.class);
66+
67+
assertEquals(1000L, result.getId());
68+
assertEquals("John", result.getName());
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.jackson.dynamicignore;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertNull;
11+
12+
class DynamicIgnoreJsonMixinUnitTest {
13+
14+
@Test
15+
void whenWritingWithoutMixin_thenIdIsPresent() throws JsonProcessingException {
16+
UserWithMixin user = new UserWithMixin(1000L, "John");
17+
String result = new ObjectMapper().writeValueAsString(user);
18+
assertThat(result).contains("John");
19+
assertThat(result).contains("1000");
20+
}
21+
22+
@Test
23+
void whenWritingWithPublicMixin_thenIdIsIgnored() throws JsonProcessingException {
24+
UserWithMixin user = new UserWithMixin(1000L, "John");
25+
ObjectMapper objectMapper = new ObjectMapper().addMixIn(UserWithMixin.class, UserWithMixin.PublicMixin.class);
26+
String result = objectMapper.writeValueAsString(user);
27+
assertThat(result).contains("John");
28+
assertThat(result).doesNotContain("1000");
29+
}
30+
31+
@Test
32+
void whenReadingWithoutMixin_thenIdIsPresent() throws JsonProcessingException {
33+
String json = "{\"id\":1000,\"name\":\"John\"}";
34+
UserWithMixin user = new ObjectMapper().readValue(json, UserWithMixin.class);
35+
assertEquals(1000L, user.getId());
36+
assertEquals("John", user.getName());
37+
}
38+
39+
@Test
40+
void whenReadingWithPublicMixin_thenIdIsIgnored() throws JsonProcessingException {
41+
String json = "{\"id\":1000,\"name\":\"John\"}";
42+
ObjectMapper objectMapper = new ObjectMapper().addMixIn(UserWithMixin.class, UserWithMixin.PublicMixin.class);
43+
UserWithMixin user = objectMapper.readValue(json, UserWithMixin.class);
44+
assertNull(user.getId());
45+
assertEquals("John", user.getName());
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.jackson.dynamicignore;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.ObjectReader;
6+
import com.fasterxml.jackson.databind.ObjectWriter;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNull;
13+
14+
class DynamicIgnoreJsonViewUnitTest {
15+
16+
@Test
17+
void whenWritingWithPublicView_thenIdIsIgnored() throws JsonProcessingException {
18+
UserWithView user = new UserWithView(1000L, "John");
19+
ObjectWriter objectWriter = new ObjectMapper().writerWithView(UserWithView.PublicView.class);
20+
String result = objectWriter.writeValueAsString(user);
21+
assertThat(result).contains("John");
22+
assertThat(result).doesNotContain("1000");
23+
}
24+
25+
@Test
26+
void whenWritingWithInternalView_thenIdIsPresent() throws JsonProcessingException {
27+
UserWithView user = new UserWithView(1000L, "John");
28+
ObjectWriter objectWriter = new ObjectMapper().writerWithView(UserWithView.InternalView.class);
29+
String result = objectWriter.writeValueAsString(user);
30+
assertThat(result).contains("John");
31+
assertThat(result).contains("1000");
32+
}
33+
34+
@Test
35+
void whenReadingWithPublicView_thenIdIsIgnored() throws JsonProcessingException {
36+
String json = "{\"id\":1000,\"name\":\"John\"}";
37+
ObjectReader objectReader = new ObjectMapper().readerWithView(UserWithView.PublicView.class)
38+
.forType(UserWithView.class);
39+
UserWithView user = objectReader.readValue(json);
40+
assertEquals("John", user.getName());
41+
assertNull(user.getId());
42+
}
43+
44+
@Test
45+
void whenReadingWithInternalView_thenIdIsPresent() throws JsonProcessingException {
46+
String json = "{\"id\":1000,\"name\":\"John\"}";
47+
ObjectReader objectReader = new ObjectMapper().readerWithView(UserWithView.InternalView.class)
48+
.forType(UserWithView.class);
49+
UserWithView user = objectReader.readValue(json);
50+
assertEquals("John", user.getName());
51+
assertEquals(1000L, user.getId());
52+
}
53+
54+
}

0 commit comments

Comments
 (0)