当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

自定义ObjectMapper

发表于: 2015-03-02   作者:conkeyn   来源:转载   浏览:
摘要: d   import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com

d

 

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * json与java对象之间转化器
 * 
 * @version 1.0.0
 * @since 2014年10月21日 下午3:11:39
 */
public final class JsonObjectMapper extends ObjectMapper
{
	/** */
	private static final long serialVersionUID = 4563671462132723274L;

	public JsonObjectMapper()
	{
		super();
		//从JSON到java object
		//没有匹配的属性名称时不作失败处理
		this.configure(MapperFeature.AUTO_DETECT_FIELDS, true);

		//反序列化
		//禁止遇到空原始类型时抛出异常,用默认值代替。
		this.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
		this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
		//禁止遇到未知(新)属性时报错,支持兼容扩展
		this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		this.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
		this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
		this.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
		this.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
		this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
		this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);

		//序列化
		//禁止序列化空值
		this.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
		this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		this.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
		this.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true);
		this.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
		this.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true);
		this.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
		//不包含空值属性
		this.setSerializationInclusion(Include.NON_EMPTY);
		//this.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
		//是否缩放排列输出,默认false,有些场合为了便于排版阅读则需要对输出做缩放排列
		this.configure(SerializationFeature.INDENT_OUTPUT, true);

		//开启序列化和反序列化时为对象附加@class属性
		this.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, As.PROPERTY);
	}

}

 

/**
 * 测试objectMapper特性
 * 
 * @version 1.0.0
 * @since 2015年3月2日 下午2:02:05
 * @param <T> 泛型参数
 */
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
//只包含非空属性
@JsonInclude(Include.NON_NULL)
@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY)
public class Msg<T>
{
	
	@JsonProperty(required = false, value = "code")
	private String code;

	/** 消息内容 */
	@JsonProperty(required = false, value = "msg")
	private String msg;
	
	@JsonProperty(required = false, value = "name")
	String name;
	
	@JsonProperty(required = false, value = "payload")
	@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY)
	T payload;

	public T getPayload()
	{
		return payload;
	}

	public void setPayload(T payload)
	{
		this.payload = payload;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getCode()
	{
		return code;
	}

	public void setCode(String code)
	{
		this.code = code;
	}

	public String getMsg()
	{
		return msg;
	}

	public void setMsg(String msg)
	{
		this.msg = msg;
	}
}

 

/**
 * 测试objectMapper特性
 * 
 * @version 1.0.0
 * @since 2015年3月2日 下午2:02:05
 * @param <T> d
 */
@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY)
public class TypeMap<T>
{
	@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY)
	//指示Map的value是使用@JsonTypeInfo进行解析
	Map<String, T> map = new HashMap<String, T>();

	public Map<String, T> getMap()
	{
		return map;
	}

	public void setMap(Map<String, T> map)
	{
		this.map = map;
	}

}

 

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

/**
 * 测试类
 * 
 * @version 1.0.0
 * @since 2015年2月5日 上午10:32:38
 */
@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY)
public class User implements Serializable
{

	private String id;
	private String name;

	private int age;

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}
}

 

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

/**
 * 测试类
 * 
 * @version 1.0.0
 * @since 2015年2月5日 上午10:32:38
 */
@JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY)
public class User1 implements Serializable
{

	/** */
	private static final long serialVersionUID = 5723985672168696454L;
	private String id;
	private String name;

	private int age;

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}
}

 

 

d

自定义ObjectMapper

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号