Jackson (二)--ObjectMapper.setSerializationInclusion 序列化设置

2 篇文章 0 订阅
1 篇文章 0 订阅

ObjectMapper将实例对象序列化为json字符串时,可以通过设置序列化参数,进行一些特殊处理;

//        // 序列化时  ,属性值为null的忽略

  1.         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);         
  2.         objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); 
  3.         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); 
  4.         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 
  5.         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);

        //通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化 

  •         //JsonInclude.Include.ALWAYS 默认 
  •         //JsonInclude.Include.NON_DEFAULT 属性为默认值不序列化 
  •         //JsonInclude.Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化 
  •         //JsonInclude.Include.NON_NULL 属性为NULL 不序列化 

或者在实体类中使用标签进行处理如下:

  •  @JsonIgnoreProperties({"sex","addTime"})  //序列化时忽略的属性名称集合  ,加在类上,给出的属性都不序列化
  • @JsonProperty("stu_id")    //序列化时,如果要修改某些属性的名字,可以使用, 序列化 将id改为 stu_id
  • @JsonIgnore        //序列化时忽略字段,用在 属性上 、方法上 边。
package cn.json.bean;
 
import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
 
 
@JsonIgnoreProperties({"sex","addTime"})  //序列化时忽略的属性名称集合  ,加在类上,给出的属性都不序列化
public class student implements Serializable{
	/**
	 * @Fields serialVersionUID : 
	 */
	private static final long serialVersionUID = -6510230638877789163L;
	@JsonProperty("stu_id")	//序列化时,如果要修改某些属性的名字,可以使用, 序列化 将id改为 stu_id
	private Integer id ;
	@JsonIgnore		//序列化时忽略字段,用在 属性上 、方法上 边。
	private String name ;
	private Integer age ;
	private String sex;
	private Date addTime ;
	public Date getAddTime() {
		return addTime;
	}
	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", addTime=" + addTime + "]";
	}
	
}

 Demo程序中可以通过如下设置:

package com.json.JsonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;

import cn.json.bean.student;

public class Demo2 
{

	private static final ObjectMapper objectMapper = new ObjectMapper();

	public static void main( String[] args ) throws JsonProcessingException, ClassNotFoundException
    {

//		// 序列化时  ,属性值为null的忽略
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);    	 
		objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); 
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); 
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
		//通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化 
		//JsonInclude.Include.ALWAYS 默认 
		//JsonInclude.Include.NON_DEFAULT 属性为默认值不序列化 
		//JsonInclude.Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化 
		//JsonInclude.Include.NON_NULL 属性为NULL 不序列化 
		student stu1 = new student();
		stu1.setId(1);
		stu1.setName("aaav");
		stu1.setSex("男");
		stu1.setAddTime(new Date());
		stu1.setAge(11);
		student stu2 = new student();
		stu2.setId(2);
		stu2.setName("bbbb");
		stu2.setSex("男");
		stu2.setAddTime(new Date());
		stu2.setAge(12);
		String data = objectMapper.writeValueAsString(stu1);
		System.out.println("序列化结果:"+data);

	
    }
}

 

  •     @JsonIgnoreProperties({"sex","addTime"}) sex addTime 字段序列化是被忽略掉了(放在class上边)
  •     @JsonIgnore        //序列化时忽略字段
  •     private String name ;
  •     @JsonProperty("stu_id")    //序列化时,如果要修改某些属性的名字
  •     private Integer id ;   id改成了stu_id

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值