1. Specify fields that do not need serialization through annotations
Fields that do not need serialization can be excluded by annotating @jsonField (serialize = false).
public class DontSerializeAnnotation {
@JSONField(serialize = false)
private Long id;
private String name;
public Long getId(a) {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name; }}/** * specify fields that do not need serialization via annotations:@JSONField(serialize = false)
*/
@Test
public void testDontSerializeAnnotation(a) {
DontSerializeAnnotation guestUser = new DontSerializeAnnotation();
guestUser.setId(2L);
guestUser.setName("guest");
System.out.println(JSON.toJSONString(guestUser));
}
Copy the code
Execution Result:
{"name":"guest"}
Copy the code
2. Variables declared transient are not serialized
If the serialized field is decorated with transient, it will not be serialized.
public class TransientObject {
private Long id;
private transient String name;
public Long getId(a) {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name; }}/** * variables declared transient are not serialized */
@Test
public void testTransientObject(a) {
TransientObject guestUser = new TransientObject();
guestUser.setId(2L);
guestUser.setName("guest");
System.out.println(JSON.toJSONString(guestUser));
}
Copy the code
Execution Result:
{"id":2}
Copy the code
3. Use property filters to exclude fields that do not need serialization
Using the SimplePropertyPreFilter, you can set 1 to more fields to filter out at once.
3.1 Filtering a Field in an Object
public class User {
private Long id;
private String name;
public Long getId(a) {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name; }}/** * Use property filters to exclude fields that do not require serialization */
@Test
public void testExcludeSingleFilter(a) {
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("name"); // Exclude the field named name
System.out.println(JSON.toJSONString(guestUser, filter));
}
Copy the code
Execution Result:
{"id":2}
Copy the code
3.2 Filtering fields of the same name in nested classes
The User in Group, refer to the previous section.
public class Group {
private Long id;
private String name;
private List<User> users = new ArrayList<User>();
public Long getId(a) {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<User> getUsers(a) {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void addUser(User user) { users.add(user); }}/** * Use attribute filters to exclude fields that do not need to be serialized. * For nested classes, fields with the same name in different classes are filtered out. * /
@Test
public void testExcludeEmbedFilter(a) {
Group group = new Group();
group.setId(0L);
group.setName("admin");
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
group.addUser(guestUser);
group.addUser(rootUser);
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("name");
System.out.println(JSON.toJSONString(group, filter));
}
Copy the code
Execution Result:
{"id":0,"users":[{"id":2},{"id":3}]}
Copy the code
3.3 Use multiple property filters to exclude fields that do not require serialization
If you only want to filter fields in a particular class, you can specify the class object and fields of the corresponding class when creating the filter.
In the following example, create a filter for Group and User to filter out the different fields.
/** * use multiple attribute filters to exclude fields that do not require serialization. */
@Test
public void testExcludeMultiFilter(a) {
Group group = new Group();
group.setId(0L);
group.setName("admin");
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
group.addUser(guestUser);
group.addUser(rootUser);
// Group filter to filter out the ID field
SimplePropertyPreFilter filter1 = new SimplePropertyPreFilter(Group.class);
filter1.getExcludes().add("id");
// Filter out the name field
SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter(User.class);
filter2.getExcludes().add("name");
System.out.println(JSON.toJSONString(group, new SimplePropertyPreFilter[] {filter1, filter2}));
}
Copy the code
Execution Result:
{"name":"admin","users":[{"id":2},{"id":3}]}
Copy the code
4. Use a property filter to include the fields you want to serialize.
In some cases, we only need to serialize individual fields, or when there are more fields to exclude than to serialize, we can use include rather than exclude filters.
/** * use property filters to contain fields that need to be serialized. */
@Test
public void testIncludeMultiFilter(a) {
Group group = new Group();
group.setId(0L);
group.setName("admin");
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
group.addUser(guestUser);
group.addUser(rootUser);
// Group serializes only id and users
SimplePropertyPreFilter filter1 = new SimplePropertyPreFilter(Group.class, "id"."users");
// User serializes only name
SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter(User.class);
filter2.getIncludes().add("name");
System.out.println(JSON.toJSONString(group, new SimplePropertyPreFilter[] {filter1, filter2}));
}
Copy the code
Execution Result:
{"id":0,"users":[{"name":"guest"},{"name":"root"}]}
Copy the code
5. Customize attribute filtering rules
In some special scenarios, the above simple exclusion or inclusion methods cannot meet actual application requirements.
In this case, you can consider using a custom attribute filter to achieve filtering. This implementation is very flexible and can be implemented according to actual requirements.
Some sort of rule judgment can be performed on classes, field names, and field values to determine whether serialization is required.
To implement the apply method in the PropertyFilter interface, return true to indicate that the field is included, and return false to indicate that the field is filtered out.
/ * * *@author wenshao[[email protected]]
*/
public interface PropertyFilter extends SerializeFilter {
/ * * *@paramobject the owner of the property. The object to which the field belongs *@paramname the name of the property. The field name *@paramvalue the value of the property. The field values *@return true if the property will be included, false if to be filtered out
*/
boolean apply(Object object, String name, Object value);
}
Copy the code
/** * Custom attribute filtering rules, the most flexible way to achieve field and value filtering. * /
@Test
public void testPropertyFilter(a) {
Group group = new Group();
group.setId(0L);
group.setName("admin");
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
group.addUser(guestUser);
group.addUser(rootUser);
PropertyFilter filter = new PropertyFilter() {
@Override
public boolean apply(Object object, String name, Object value) {
//System.out.println(object + " " + name + " " + value);
if (name.equals("name")) {
return false; // Exclude the field named name
}
if (value.toString().equals("2")) {
return false; // Exclude fields with value 2
}
return true; // Contains the field or value}}; System.out.println(JSON.toJSONString(group, filter)); }Copy the code
Execution Result:
{"id":0,"users":[{},{"id":3}]}
Copy the code
— Reprint this site article please indicate the author and source of binarylife.icu, please do not use for any commercial purposes
My blog: Binarylife.icu /
Public number: Binary road