Jackson 反序列化处理带泛型

一、ObjectMapper 有一些特殊配置可以在封装为工具类时用

 

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


public class JacksonTester {
	public static void main(String args[]){
		//map json to student
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			String jsonString = "[{\"name\":\"Mahesh\", \"agea\":21, \"id\":\"\"}]";
			List student = mapper.readValue(jsonString, new TypeReference>(){});
//			List student = mapper.readValue(jsonString, mapper.getTypeFactory().constructParametricType(List.class, Student.class));
			System.out.println(student);
			System.err.println(student.get(0).getName());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.err.println("-------------------------------");

		//map student to json
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
			TimeZone china = TimeZone.getTimeZone("GMT+08:00");
			mapper.setTimeZone(china);
			mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
			mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

			Student student = new Student("haha",null,"",new Date());
			System.out.println(student);
			List list = new ArrayList<>();
			list.add(student);
			String jsonString = mapper.writeValueAsString(list);
			System.out.println(jsonString);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


//@JsonIgnoreProperties(ignoreUnknown = true)
class Student {
	private String name;
	private Long age;
	private String id;
	private Date time;
	Student(){}

	public Student(String name, Long age, String id, Date time) {
		this.name = name;
		this.age = age;
		this.id = id;
		this.time = time;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getAge() {
		return age;
	}
	public void setAge(Long age) {
		this.age = age;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public String getId() {
		return id;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", age=" + age +
				", id='" + id + '\'' +
				", time=" + time +
				'}';
	}
}

二、序列化注意事项:

 1、定义字段时名字不要带is,否则会出现问题,如下所示:

在用工具生成getset时会将is默认截掉

Jackson 反序列化处理带泛型_第1张图片

 序列化后生成的字段也是没有is的

即使将getset方法改为带is的命名

Jackson 反序列化处理带泛型_第2张图片

序列化后的结果仍然是错误的null

这就是为什么阿里规约不让用带is的原因

2、Date类型的字段在序列化时会被转为long型的时间戳,不论是jackson还是fastjson

但实际通过rest接口返回的并不是long型而是日期格式的字符串

Jackson 反序列化处理带泛型_第3张图片

所以在用工具打印json输出日志时需要注意 

你可能感兴趣的:(随笔)