jackson在實際應用中給我們提供了一系列注解,提高了開發的靈活性,下面介紹一下最常用的一些注解
@JsonIgnoreProperties
此注解是類注解,作用是json序列化時將Java bean中的一些屬性忽略掉,序列化和反序列化都受影響。
@JsonIgnore
此注解用於屬性或者方法上(最好是屬性上),作用和上面的@JsonIgnoreProperties一樣。
@JsonFormat
此注解用於屬性或者方法上(最好是屬性上),可以方便的把Date類型直接轉化為我們想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")
@JsonSerialize
此注解用於屬性或者getter方法上,用於在序列化時嵌入我們自定義的代碼,比如序列化一個double時在其后面限制兩位小數點,將一個Date類型轉化成指定類型字符串。
public class JsonDoubleSerialize extends JsonSerializer<Double> { private DecimalFormat df = new DecimalFormat("##.000"); @Override public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(df.format(value)); } }
/** * 把Date類型序列化成指定合適的字符串 */ public class JsonDateSerialize extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { String formattedDate = ""; if (date != null) { //把日期序列化成yyyy-MM-dd格式的字符串 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); formattedDate = simpleDateFormat.format(date); } jgen.writeString(formattedDate); } }
@JsonDeserialize
此注解用於屬性或者setter方法上,用於在反序列化時可以嵌入我們自定義的代碼,類似於上面的@JsonSerialize
/** * 將一個字符串反序列化成一個Date類型 */ public class JsonDateDeserialize extends JsonDeserializer<Date> { @Override public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { //拿到的是"yyyy-MM-dd"形式的字符串,現在要在json反序列化的時候轉化成Date類型 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String date = jp.getText(); if (date == null || date.trim().length() == 0) { return null; } try { return format.parse(date); } catch (Exception e) { } return null; } }
完整例子
//表示序列化時忽略的屬性 @JsonIgnoreProperties(value = {"word"}) public class Person { private String name; private int age; private boolean sex; @JsonSerialize(using = JsonDateSerialize.class) @JsonDeserialize(using = JsonDateDeserialize.class) private Date birthday; private String word; @JsonSerialize(using = JsonDoubleSerialize.class) private double salary; 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; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Person(String name, int age) { this.name = name; this.age = age; } public Person(String name, int age, boolean sex, Date birthday, String word, double salary) { super(); this.name = name; this.age = age; this.sex = sex; this.birthday = birthday; this.word = word; this.salary = salary; } public Person() { } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", word=" + word + ", salary=" + salary + "]"; } }
public class Demo { public static void main(String[] args) { //writeJsonObject(); readJsonObject(); } // 直接寫入一個對象(所謂序列化) public static void writeJsonObject() { ObjectMapper mapper = new ObjectMapper(); Person person = new Person("zhangsan", 25, true, new Date(), "coder", 2500.0); try { String string = mapper.writeValueAsString(person); //{"name":"zhangsan","age":25,"sex":true,"birthday":"2016-12-03 22:02:23","salary":"2500.000"} System.out.println(string); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 直接將一個json轉化為對象(所謂反序列化) public static void readJsonObject() { ObjectMapper mapper = new ObjectMapper(); try { String string = "{\"name\":\"zhangsan\",\"age\":25,\"sex\":true,\"birthday\":\"2016-12-03 22:02:23\",\"word\":\"coder\",\"salary\":\"2500.000\"}"; Person person = mapper.readValue(string, Person.class); //Person [name=zhangsan, age=25, sex=true, birthday=Sat Dec 03 00:00:00 CST 2016, word=null, salary=2500.0] System.out.println(person.toString()); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }