jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解
参考:http://blog.csdn.net/sdyy321/article/details/40298081
1.@JsonIgnoreProperties
此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉。序列化和反序列化都受影响。
2.@JsonIgnore
词注解用于属性或者方法上,作用和@JsonIgonreProperties一样。反序列化时,如何该字段被标记了JsonIgnore,则返回null
3.@JsonFormat
用在属性和方法上,可以方便的进行格式转换,如把Date转换为我们要的模式@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")
4.@JsonSerialize
用于属性和getter()方法 ,在序列化时嵌入我们自定义的代码,比如序列化一igedouble时,在其后面限制2位小数
5.@JsonDeserialize
此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码
代码:
实体类:
public class Student {
private int id;
private String name;
private String password;
@JsonSerialize(using=MyDateSerializer.class) //序列化转换器
@JsonDeserialize(using=MyDateDeSerializer.class) //反序列化转换器
private Date date;
public Student(){
System.out.println("初始化Student对象");
}
//setter,getter省略
}
序列化转换器:
/**
* 日期序列化转换器
* @author root
*
*/
public class MyDateSerializer extends JsonSerializer<Date>{
@Override
public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
jgen.writeString(str);
}
}
日期反序列化转换器:
/**
* 日期反序列化转换器
* @author root
*
*/
public class MyDateDeSerializer extends JsonDeserializer<Date>{
@Override
public Date deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
System.out.println("反序列化");
String dateStr = parser.getValueAsString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
}
6.@JsonUnwrapped
当实体类中成员属性是一个类的对象时候,忽略包装。。(真的不好解释,看看例子就明白了)
User类:
public class User {
private String name;
private String password;
@JsonIgnore
private List<String> strs ;
@JsonUnwrapped
private Person p;
//setter,getter省略
}
Person类:
public class Person {
private String pName;
private int age;
private boolean sex;
@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")
private Date birthday;
private String word;
private double salary;
//setter,getter省略
}
@Test
public void testJsonUnWrapped() throws IOException{
User u = new User("guofeipeng","123");
List<String> ls = new ArrayList<String>();
ls.add("123");
ls.add("234");
ls.add("345");
u.setStrs(ls);
Person person = new Person("nomouse", 25, true, new Date(), "程序员",
2500.0);
u.setP(person);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(u); //java 2 json
System.out.println(json);
User user = mapper.readValue(json, User.class);//json 2 java
System.out.println(user);
}
注: 1.User类中Person属性不加@JsonUnwarpped,转化后的json为:{"name":"guofeipeng","password":"123","p":{"age":25,"sex":true,"birthday":"2014-12-22 07-15-29","word":"程序员","pname":"nomouse","salary":"2500.00"}}
2.加了@JsonUnwarpped注解,则去掉了p的键(包装),转化后的json为:
{"name":"guofeipeng","password":"123","age":25,"sex":true,"birthday":"2014-12-22 07-16-38","word":"程序 员","pname":"nomouse","salary":"2500.00"}
7.@JsonProperty
作用在字段或方法上,用来对属性的序列化/反序列化,可以用来避免遗漏属性,同时提供对属性名称重命名,比如在很多场景下Java对象的属性是按照规范的驼峰书写,但是实际展示的却是类似C-style或C++/Microsolft style
@JsonProperty(value="stu_name")
private String name;
/**
* 日期反序列化转换器
* @author root
*
*/
public class MyDateDeSerializer extends JsonDeserializer<Date>{
@Override
public Date deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
System.out.println("反序列化");
String dateStr = parser.getValueAsString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
}