jackson注記
2744 ワード
@JsonIgnoreProperties
クラスにシーケンス化と逆シーケンス化に関与しない属性をマークする
@JsonIgnoreProperties(value = { "age" })
public class Person {
private String name;
private String age;
}
@JsonIgnore
シーケンス化と逆シーケンス化に関与しないプロパティに寸法を付ける
public class Person {
private String hello;
@JsonIgnore
private String word;
}
@JsonFormat
シーケンス化された文字列のフォーマット
public class Person{
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8")
private Date time;
private String hello;
private String word;
}
@JsonSerialize
シーケンス化するときに書き換える方法でgetメソッドに追加したり、属性に直接追加したりすることができます.
public class Person {
private String hello;
private String word;
@JsonSerialize(using = CustomDoubleSerialize.class)
private Double money;
}
public class CustomDoubleSerialize extends JsonSerializer {
private DecimalFormat df = new DecimalFormat("#.##");
@Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(df.format(value));
}
}
@JsonDeserialize
逆シーケンス化の場合は書き換える方法でsetメソッドに追加したり、属性に直接追加したりすることができます
@Data
public class Person {
private String hello;
private String word;
@JsonDeserialize(using = CustomDateDeserialize.class)
private Date time;
}
public class CustomDateDeserialize extends JsonDeserializer {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
Date date = null;
try {
date = sdf.parse(jp.getText());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
@JsonInclude
Include.Include.ALWAYS
デフォルトInclude.NON_DEFAULT
属性がデフォルトで直列化されていないInclude.NON_EMPTY
属性が空("")またはNULLで直列化されていないInclude.NON_NULL
属性がNULLで直列化されていない@Data
public class OrderProcessTime {
@JsonInclude(JsonInclude.Include.ALWAYS)
private Date plan;
}
@JsonProperty
プログラミング仕様に合致しない変数の名前など、Jsonシーケンス化と逆シーケンス化を表すときに使用される名前
public class Person {
private String hello;
private String word;
private Date time;
@JsonProperty(value = "DeliveryTime")
private Integer deliveryTime;
}