顯示具有 JSON 標籤的文章。 顯示所有文章
顯示具有 JSON 標籤的文章。 顯示所有文章

2017-12-28

com.fasterxml.jackson.core 的 Jackson Annotations 2.9.3

蠻常用 Jackson 將 Java 物件轉成 JSON(writeValueAsString),或者將 JSON 轉回 Java 物件(readValue)。
public class Book implements Serializable {

  private String title;
  private int year;

  public String getTitle() {
    return this.title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public int getYear() {
    return this.year;
  }

  public void setYear(int year) {
    this.year = year;
  }

  @SuppressWarnings("unchecked")
  public static void main(String[] args) throws IOException {
    Book b = new Book();
    b.setTitle("Jackson");
    b.setYear(2017);
    String json = new ObjectMapper().writeValueAsString(b);
    System.out.println(json); // {"title":"Jackson","year":2017}
    Map<String, String> m = new ObjectMapper().readValue(json, Map.class);
    System.out.println(m); // {title=Jackson, year=2017}
  }
}
但聽說它有非常多特別的 Annotation。