java2018년 3월 25일2 min read

Q&A Collection on Jackson

A collection of Q&A on Jackson.

FFrank Advenoh
#Q&A#QA#faq

This is material where I personally jot down things I don't know and briefly organize what I come to learn. If you know something among the unanswered questions, please leave a comment. Thank you.

Full Q&A List

[Answered]

1. @JsonInclude(Include.NON_NULL)?

This annotation excludes class fields that are null from being serialized to JSON. In the code above, the stringValue variable is not stored in the JSON.

Terminology

  • The task of converting a Java object to JSON is referred to as serialize, and JSON -> object is called deserialize

Reference

2. @JsonIgnore?

This is an annotation you declare above a variable when you don't want to include that field during serialization. In this example, the @JsonIgnore annotation was applied because the password must not be present when fetching a domain object through JPA.

Reference

3. @JsonIgnoreProperties(ignoreUnknown = true)?

This is an annotation that ignores the Exception that occurs when a property that does not exist on the object is included in the JSON.

Reference

4. What happens if you set allowGetters to true in @JsonIgnoreProperties?

When specifying the properties to ignore with @JsonIgnoreProperties, setting allowGetters to true means that the specified fields are applied for JSON serialization (Object -> JSON), but are excluded for deserialization (JSON -> Object).

Reference

5. What do serialization and deserialization mean in Jackson?

  • Jackson
    • Serialization
      • Converting a Java Object -> Jackson JSON.
    • Deserialization
      • Converting Jackson JSON -> Java Object
  • Java (for reference)
    • Serialization
      • Java Object -> byte form
    • Deserialization
      • byte form -> Java Object

Reference


[Unanswered Questions]

- What is the difference between @DateTimeFormat vs @JsonFormat?

  • DateTimeFormat : DateTimeFormat
  • JsonFormat : jackson

- @JsonTypInfo, @JsonSubTypes?

Reference

- @JsonManagedReference

- @JsonBackReference

- What are @JsonIdentityInfo and @JsonIdentityReference?

  • When querying data from entities connected with @OneToMany, @ManyToOne, an Infinite recursion JsonMappingException occurs
  • Solution
    • Jackson 1/6+
      • Use @JsonManagedReference, @JsonBackReference
    • Jackson 2.0+
      • Use @JsonIdentityInfo

관련 글