# Jackson

参考这篇博客

# 通用注解

序列化和反序列化都生效

# @JsonProperty 🔥

添加@JsonProperty注释来表示JSON中的属性名。

public class MyBean {
    public int id;
    private String name;
 
    @JsonProperty("name1")
    public void setTheName(String name) {
        this.name = name;
    }
 
    @JsonProperty("name1")
    public String getTheName() {
        return name;
    }
}
{"id":1,"name1":"My bean"}

# 序列化注解

将 Java 对象转为与平台无关的二进制字节流

# @JsonAnyGetter

@JsonAnyGetter注释允许灵活地使用Map类型字段作为标准属性。

@Data
public class ExtendableBean {
    private String name;

    private Map<String, String> map = new HashMap<>(10);

    private Map<String, String> properties = new HashMap<>(10);

    @JsonAnyGetter
    public Map<String, String> getProperties() {
        return properties;
    }
}

当我们序列化这个实体的一个实例时,我们将得到映射中的所有键值作为标准的普通属性

{
  "name": "Tom",
  "map": {},
  "attr2": "val2",
  "attr1": "val1",
  "attr3": "val3"
}

示例

@Test
public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException {
    ExtendableBean bean = new ExtendableBean();
    bean.setName("Tom");
    bean.getProperties().put("attr1", "val1");
    bean.getProperties().put("attr3", "val3");
    bean.getProperties().put("attr2", "val2");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);
}

# @JsonGetter 🔥

@JsonGetter注释是@JsonProperty注释的替代方法,用于将方法标记为getter方法,替代原样getter。

public class MyBean {
    public int id;
    private String name;
 
    @JsonGetter("name1")
    public String getTheName() {
        return name;
    }
}
{"id":1,"name1":"My bean"}

示例

@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {

    MyBean bean = new MyBean(1, "My bean");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);

}

# @JsonPropertyOrder

我们可以使用@JsonPropertyOrder注释来指定序列化中属性的顺序。

我们还可以使用@JsonPropertyOrder(alphabetic=true)字母顺序排列属性。

@JsonPropertyOrder({ "name", "id" })
public class MyBean {
    public int id;
    public String name;
}
{
    "name":"My bean",
    "id":1
}

# @JsonRawValue

@JsonRawValue注释可以指示Jackson按原样序列化属性。 可以设置true或false来定义此注释是否处于活动状态(一般不用)

public class RawBean {
    public String name;
 
    @JsonRawValue
    public String json;
}
{
    "name":"My bean",
    "json":{
        "attr":false
    }
}

示例

@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {

    MyBean bean = new MyBean(1, "My bean");
    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);
}

# @JsonValue 🔥

@JsonValue表示该库将用于序列化整个实例的单个方法。

例如,在enum中,我们使用@JsonValue注释getName,以便通过它的名称序列化任何这样的实体

public enum TypeEnumWithValue {
    TYPE1(1, "Type A"), TYPE2(2, "Type 2");
 
    private Integer id;
    private String name;
 
    // standard constructors
 
    @JsonValue
    public String getName() {
        return name;
    }
}
"Type A"

示例

@Test
public void whenSerializingUsingJsonValue_thenCorrect() throws IOException {

    String enumAsString = new ObjectMapper().writeValueAsString(TypeEnumWithValue.TYPE1);
    System.out.println(enumAsString);
}

# 反序列化注解

# @JsonSetter 🔥

@JsonSetter 注释是@JsonProperty注释的替代方法,用于将方法标记为setter方法,替代原样setter。

{"id":1,"name1":"My bean"}
public class MyBean {
    public int id;
    private String name;
 
    @JsonSetter("name1")
    public String getTheName() {
        return name;
    }
}

示例

@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {

    MyBean bean = new MyBean(1, "My bean");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);

}

# 属性包含注解

# @JsonIgnoreProperties 🔥

标记Jackson将忽略的属性或属性列表。可用在类、方法、构造器、字段上

设置@JsonIgnoreProperties注释的ignoreUnknown=true。 定义可以在反序列化期间仅忽略任何无法识别的属性的属性。如果为true,则所有无法识别的属性(即没有setter或builder接受它们)都将在没有警告的情况下被忽略(尽管仍会调用未知属性的处理程序)。对序列化没有任何影响。即常用于RequesBody

@JsonIgnoreProperties({ "id" })
public class BeanWithIgnore {
    public int id;
    public String name;
}
{"name":"My bean"}

示例

@Test
public void whenSerializingUsingJsonIgnoreProperties_thenCorrect()
    throws JsonProcessingException {

    BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);
}