ジャックソン
4547 ワード
1.@JsonPropertyOrder
@JsonPropertyOrder(value={"name","id","json"})の役割:jsonシーケンス化後の出力順序を変更する
@JsonPropertyOrder(value ={"name","id","json"})
public class MyBean {
@JsonProperty("id")
private int id;
@JsonProperty("name")
private String name;
@JsonRawValue
private String json;
public MyBean(int id, String name, String json) {
this.id = id;
this.name = name;
this.json = json;
}
}
public static void main(String[] args) throws JsonProcessingException{
MyBean myBean = new MyBean(1,"zz","{'attr':false}");
String result = new ObjectMapper().writeValueAsString(myBean);
System.out.println(result);
}
結果:
{"name":"zz","id":1,"json":{'attr':false}}
2.@JsonRawValue
役割:json文字列をjsonにシーケンス化し、コードは上記のようにします.
3.@JsonValue
≪アクション|Action|oraolap≫:指定した列挙値を出力します.
public enum BeanEnum {
TYPE1(1,"Type A"),TYPE2(2,"Type B");
private Integer id;
private String name;
private BeanEnum(Integer id, String name) {
this.id = id;
this.name = name;
}
@JsonValue
public String getName(){
return name;
}
}
public static void main(String[] args) throws JsonProcessingException{
String result = new ObjectMapper().writeValueAsString(BeanEnum.TYPE1);
System.out.println(result);
}
結果
"Type A"
4.@JsonRootName
役割:シーケンス化されたjsonにルート名を追加する
@jsonRootName(value="user")
public class User{
public int id;
public String name;
}
結果:
{
"user"{
"id":1.
"name":"zz"
}
}