-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ Jackson | |
:maxdepth: 3 | ||
:numbered: 0 | ||
|
||
jacksonObjectMapper | ||
jacksonObjectMapper | ||
jacksonJsonFilter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Jackson @JsonFilter | ||
|
||
## Using @JsonFilter | ||
**Step-1**: Create a class annotated with `@JsonFilter` and assign a filter name. | ||
|
||
**Step-2**: Create the instance of `SimpleFilterProvider` and add our filter `studentFilter` using `addFilter()` method. | ||
``` | ||
SimpleFilterProvider filterProvider = new SimpleFilterProvider(); | ||
filterProvider.addFilter("studentFilter", | ||
SimpleBeanPropertyFilter.serializeAllExcept("stdName", "stdCity")); | ||
``` | ||
|
||
**Step-3**: Set the instance of `SimpleFilterProvider` to `ObjectMapper` using `setFilterProvider()` method. | ||
``` | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.setFilterProvider(filterProvider); | ||
``` | ||
|
||
**Step-4**: Now serialize the instance of `Student` class. | ||
``` | ||
Student student = new Student("Mohit", 30, "ABCD", "Varanasi"); | ||
String jsonData = mapper.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(student); | ||
System.out.println(jsonData); | ||
``` | ||
`stdName` and `stdCity` properties will not be serialized. Find the output. | ||
``` | ||
{ | ||
"stdAge" : 30, | ||
"stdCollege" : "ABCD" | ||
} | ||
``` | ||
|
||
## SimpleBeanPropertyFilter | ||
`SimpleFilterProvider` is the implementation of `PropertyFilter` that only uses property name to determine if it should be serialized or filtered out. Find some methods of `SimpleFilterProvider`. | ||
- `serializeAllExcept()`: Serializes all properties except the properties configured to this method. | ||
- `filterOutAllExcept()`: Serializes properties only configured to this method. | ||
- `serializeAll()`: Serializes all properties and filters out nothing. | ||
|
||
## @JsonFilter at Property Level | ||
We can use `@JsonFilter` annotation on fields, methods and constructor parameters since Jackson 2.3. Here we will create two filters using `@JsonFilter` at property level and filter the properties using `serializeAllExcept()` and `filterOutAllExcept()` methods. | ||
|
||
## References | ||
- [Jackson @JsonFilter Example](https://www.concretepage.com/jackson-api/jackson-jsonfilter-example) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
utils/src/main/java/t5750/utils/jackson/entity/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package t5750.utils.jackson.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class Address { | ||
@JsonProperty("city") | ||
private String city; | ||
@JsonProperty("state") | ||
private String state; | ||
@JsonProperty("country") | ||
private String country; | ||
|
||
public Address() { | ||
} | ||
|
||
public Address(String city, String state, String country) { | ||
this.city = city; | ||
this.state = state; | ||
this.country = country; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
utils/src/main/java/t5750/utils/jackson/entity/Company.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package t5750.utils.jackson.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFilter; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class Company { | ||
@JsonProperty("compName") | ||
private String name; | ||
@JsonFilter("ceoFilter") | ||
@JsonProperty("compCeo") | ||
private Employee ceo; | ||
@JsonFilter("addressFilter") | ||
@JsonProperty("compAddress") | ||
private Address address; | ||
|
||
public Company() { | ||
} | ||
|
||
public Company(String name, Employee ceo, Address address) { | ||
this.name = name; | ||
this.ceo = ceo; | ||
this.address = address; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
utils/src/main/java/t5750/utils/jackson/entity/Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package t5750.utils.jackson.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class Employee { | ||
@JsonProperty("empName") | ||
private String name; | ||
@JsonProperty("empAge") | ||
private Integer age; | ||
@JsonProperty("empProfile") | ||
private String profile; | ||
|
||
public Employee() { | ||
} | ||
|
||
public Employee(String name, Integer age, String profile) { | ||
this.name = name; | ||
this.age = age; | ||
this.profile = profile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package t5750.utils.util; | ||
|
||
import java.util.Set; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; | ||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; | ||
|
||
/** | ||
*/ | ||
public class JacksonUtil { | ||
public static ObjectMapper objectMapper(String filterId, | ||
SimpleBeanPropertyFilter filter) { | ||
final SimpleFilterProvider filterProvider = new SimpleFilterProvider(); | ||
filterProvider.addFilter(filterId, filter); | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.setFilterProvider(filterProvider); | ||
return objectMapper; | ||
} | ||
|
||
public static ObjectMapper objectMapperSerializeAllExcept(String filterId, | ||
String... propertyArray) { | ||
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter | ||
.serializeAllExcept(propertyArray); | ||
return objectMapper(filterId, filter); | ||
} | ||
|
||
public static ObjectMapper objectMapperFilterOutAllExcept(String filterId, | ||
Set<String> properties) { | ||
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter | ||
.filterOutAllExcept(properties); | ||
return objectMapper(filterId, filter); | ||
} | ||
|
||
public static ObjectMapper objectMapperFilterOutAllExcept(String filterId, | ||
String... propertyArray) { | ||
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter | ||
.filterOutAllExcept(propertyArray); | ||
return objectMapper(filterId, filter); | ||
} | ||
} |
67 changes: 56 additions & 11 deletions
67
utils/src/test/java/t5750/utils/jackson/JacksonJsonFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,70 @@ | ||
package t5750.utils.jackson; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
import t5750.utils.jackson.entity.Address; | ||
import t5750.utils.jackson.entity.Company; | ||
import t5750.utils.jackson.entity.Employee; | ||
import t5750.utils.jackson.entity.Student; | ||
import t5750.utils.util.Global; | ||
import t5750.utils.util.JacksonUtil; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; | ||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; | ||
|
||
/** | ||
* | ||
*/ | ||
public class JacksonJsonFilterTest { | ||
private static final Student STUDENT = new Student(Global.T5750, 30, "ABCD", | ||
"Varanasi"); | ||
|
||
@Test | ||
public void testJsonFilter() throws Exception { | ||
final SimpleFilterProvider filterProvider = new SimpleFilterProvider(); | ||
filterProvider.addFilter("studentFilter", SimpleBeanPropertyFilter | ||
.serializeAllExcept("stdName", "stdCity")); | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.setFilterProvider(filterProvider); | ||
final Student student = new Student("Mohit", 30, "ABCD", "Varanasi"); | ||
public void testSerializeAllExcept() throws JsonProcessingException { | ||
final ObjectMapper objectMapper = JacksonUtil | ||
.objectMapperSerializeAllExcept("studentFilter", "stdName", | ||
"stdCity"); | ||
final String jsonData = objectMapper.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(STUDENT); | ||
System.out.println(jsonData); | ||
} | ||
|
||
@Test | ||
public void testFilterOutAllExcept() throws JsonProcessingException { | ||
final ObjectMapper objectMapper = JacksonUtil | ||
.objectMapperFilterOutAllExcept("studentFilter", "stdName", | ||
"stdCity"); | ||
final String jsonData = objectMapper.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(STUDENT); | ||
System.out.println(jsonData); | ||
} | ||
|
||
@Test | ||
public void testFilterOutAllExceptWithSet() throws JsonProcessingException { | ||
Set<String> props = new HashSet<>(); | ||
props.add("stdName"); | ||
props.add("stdCity"); | ||
final ObjectMapper objectMapper = JacksonUtil | ||
.objectMapperFilterOutAllExcept("studentFilter", props); | ||
final String jsonData = objectMapper.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(student); | ||
.writeValueAsString(STUDENT); | ||
System.out.println(jsonData); | ||
} | ||
|
||
@Test | ||
public void testCompanyToJSON() throws JsonProcessingException { | ||
final SimpleFilterProvider filterProvider = new SimpleFilterProvider(); | ||
filterProvider.addFilter("ceoFilter", | ||
SimpleBeanPropertyFilter.serializeAllExcept("empProfile")); | ||
filterProvider.addFilter("addressFilter", SimpleBeanPropertyFilter | ||
.filterOutAllExcept("state", "country")); | ||
final ObjectMapper mapper = new ObjectMapper(); | ||
mapper.setFilterProvider(filterProvider); | ||
final Employee emp = new Employee(Global.T5750, 45, "Manager"); | ||
final Address address = new Address("Noida", "UP", "China"); | ||
final Company company = new Company("XYZ", emp, address); | ||
final String jsonData = mapper.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(company); | ||
System.out.println(jsonData); | ||
} | ||
} |