- 杰克逊注释教程
- 杰克逊 - 主页
- 序列化注释
- 杰克逊 - @JsonAnyGetter
- 杰克逊 - @JsonGetter
- @JsonPropertyOrder
- 杰克逊 - @JsonRawValue
- 杰克逊 - @JsonValue
- 杰克逊 - @JsonRootName
- 杰克逊 - @JsonSerialize
- 反序列化注释
- 杰克逊 - @JsonCreator
- 杰克逊 - @JacksonInject
- 杰克逊 - @JsonAnySetter
- 杰克逊 - @JsonSetter
- 杰克逊 - @JsonDeserialize
- @JsonEnumDefaultValue
- 属性包含注释
- @JsonIgnoreProperties
- 杰克逊 - @JsonIgnore
- 杰克逊 - @JsonIgnoreType
- 杰克逊 - @JsonInclude
- 杰克逊 - @JsonAutoDetect
- 类型处理注释
- 杰克逊 - @JsonTypeInfo
- 杰克逊 - @JsonSubTypes
- 杰克逊 - @JsonTypeName
- 一般注释
- 杰克逊 - @JsonProperty
- 杰克逊 - @JsonFormat
- 杰克逊 - @JsonUnwrapped
- 杰克逊 - @JsonView
- @JsonManagedReference
- @JsonBackReference
- 杰克逊 - @JsonIdentityInfo
- 杰克逊 - @JsonFilter
- 其他
- 自定义注释
- 组合注释
- 禁用注释
- 杰克逊注释资源
- 杰克逊 - 快速指南
- 杰克逊 - 有用资源
- 杰克逊 - 讨论
杰克逊注释 - @JsonIdentityInfo
@JsonIdentityInfo 用于具备父子关系的对象。@JsonIdentityInfo 用于指明在序列化/反序列化期间将使用对象标识。
示例 - @JsonIdentityInfo
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]) throws IOException, ParseException{
ObjectMapper mapper = new ObjectMapper();
Student student = new Student(1,13, "Mark");
Book book1 = new Book(1,"Learn HTML", student);
Book book2 = new Book(2,"Learn JAVA", student);
student.addBook(book1);
student.addBook(book2);
String jsonString = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(book1);
System.out.println(jsonString);
}
}
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
class Student {
public int id;
public int rollNo;
public String name;
public List<Book> books;
Student(int id, int rollNo, String name){
this.id = id;
this.rollNo = rollNo;
this.name = name;
this.books = new ArrayList<Book>();
}
public void addBook(Book book){
books.add(book);
}
}
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
class Book{
public int id;
public String name;
Book(int id, String name, Student owner){
this.id = id;
this.name = name;
this.owner = owner;
}
public Student owner;
}
输出
{
"id" : 1,
"name" : "Learn HTML",
"owner" : {
"id" : 1,
"rollNo" : 13,
"name" : "Mark",
"books" : [
1, {
"id" : 2,
"name" : "Learn JAVA",
"owner" : 1
}
]
}
}
广告