@JsonRootName
特性
- 序列化:
- 需要设置WRAP_ROOT_VALUE属性,jackson才会支持root对象,JsonRootName注解才会发挥作用
- 反序列化:
- 默认不支持对象名称,需要设置UNWRAP_ROOT_VALUE开启
- 标注有注解的,则对象名称必须要保持一致才能反序列化成功
@JsonInclude
该注解的作用仅在序列化操作时有用:用于控制方法、属性等是否应该被序列化;
可以在类,属性,方法上进行使用
属性
ALWAYS
默认策略,任何情况都执行序列化
无论是空【null】还是空字符串【””】还是空数组【[]】都会进行序列化
//@JsonInclude(JsonInclude.Include.ALWAYS{"id" : 1,"name" : "","age" : null,"isAdmin" : true,"optional" : {"present" : false},"atomic" : null,"hobby" : [ ]}
NON_NULL
所有null的值都不会进行序列化,但是空字符串会被序列化
//@JsonInclude(JsonInclude.Include.NonNull{"id" : 1,"name" : "","isAdmin" : true,"optional" : {"present" : false},"atomic" : null,"hobby" : [ ]}
NON_ABSENT
当实例化的对象有Optional或AtomicReference类型的成员变量时,
如果Optional引用的实例和AtomicReference为空,用NON_ABSENT能使该字段不做序列化;
//@JsonInclude(JsonInclude.Include.NonAbsent{"id" : 1,"name" : "","isAdmin" : true,"hobby" : [ ]}
NON_EMPTY
以下情况都不会序列化
- null
- 空字符串
- 空集合
- 空数组
- Optional类型的,其引用为空
- AtomicReference类型的,其引用为空
{"id" : 1,"isAdmin" : true,"optional" : {"present" : false}}
NON_DEFAULT
对保持默认值的字段不做序列化
//@JsonInclude(JsonInclude.Include.NON_DEFAULT{"id" : 1,"name" : "","optional" : {"present" : false},"atomic" : null,"hobby" : [ ]}
CUSTOM
此时要指定valueFilter属性,该属性对应一个类,用来自定义判断被JsonInclude修饰的字段是否序列化
//@JsonInclude(JsonInclude.Include.CUSTOM{"name" : ""}
USE_DEFAULTS
当JsonInclude在类和属性上都有时,
优先使用属性上的注解,此时如果在序列化的属性的get方法上使用了JsonInclude,并设置为USE_DEFAULTS,就会使用类注解的设置
通常该配置用于全局搭配局部进行使用
//@JsonInclude(JsonInclude.Include.USE_DEFAULTS{"id" : 1,"name" : "","age" : null,"isAdmin" : true,"optional" : {"present" : false},"atomic" : null}
测试类
实体类
//@JsonInclude(JsonInclude.Include.USE_DEFAULTS)//@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = CustomFilter.class)//@JsonInclude(JsonInclude.Include.NON_DEFAULT)//@JsonInclude(JsonInclude.Include.NON_EMPTY)//@JsonInclude(JsonInclude.Include.NON_ABSENT)//@JsonInclude(JsonInclude.Include.NON_NULL)@JsonInclude(JsonInclude.Include.ALWAYS)@Data@Accessors(chain = true)public class TestJsonInclude {private Long id;private String name;private Integer age;private Boolean isAdmin = true;private Optional<String> optional;@JsonInclude(JsonInclude.Include.NON_NULL)private AtomicReference<String> atomic;@JsonInclude(JsonInclude.Include.NON_EMPTY)private List<Map<String, Object>> hobby;@JsonInclude(JsonInclude.Include.NON_NULL)public AtomicReference<String> getAtomic() {return atomic;}}
自定义序列化类
/**
* <p>
* 自定义序列化规则
* </p>
*
* @author xupu
* @since 2022/5/26 17:19:11
*/
public class CustomFilter {
@Override
public boolean equals(Object obj) {
// null,或者不是字符串就返回true,意味着不被序列化
if(null == obj || !(obj instanceof String)) {
return true;
}
// 长度大于2就返回true,意味着不被序列化
return ((String) obj).length() > 2;
}
}
测试案例类
public class JsonIncludeDemo {
private static ObjectMapper mapper;
static {
mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
public static void main(String[] args) throws JsonProcessingException {
Class<TestJsonInclude> clazz = TestJsonInclude.class;
if(clazz.isAnnotationPresent(JsonInclude.class)) {
// 获取 "类" 上的注解
JsonInclude getAnnotation = clazz.getAnnotation(JsonInclude.class);
testInclude(String.valueOf(getAnnotation.value()));
// 默认策略,任何情况都执行序列化
// 非空
// null的不会序列化,但如果类型是AtomicReference,依然会被序列化
// null、集合数组等没有内容、空字符串等,都不会被序列化
// 如果字段是默认值,就不会被序列化
// 此时要指定valueFilter属性,该属性对应一个类,用来自定义判断被JsonInclude修饰的字段是否序列化
// 当JsonInclude在类和属性上都有时,优先使用属性上的注解,此时如果在序列化的get方法上使用了JsonInclude,并设置为USE_DEFAULTS,就会使用类注解的设置
}
}
private static void testInclude(String type) throws JsonProcessingException {
System.out.println("//@JsonInclude(JsonInclude.Include." + type);
if("NonAbsent".equals(type)) {
mapper.registerModule(new Jdk8Module());
}
TestJsonInclude test = new TestJsonInclude().setId(1L)
.setName("")
.setAge(null)
.setOptional(Optional.empty())
.setAtomic(new AtomicReference<>())
.setHobby(new ArrayList<>());
System.out.println(mapper.writeValueAsString(test));
}
}
