Jackson Annotations
Rishabh Rahul
2 min read
Table of contents
Annotation | Purpose | Usage |
@JsonAnyGetter | Enables the flexibility of using a Map field as standard properties. | Use when you want to serialize a Map's key-value pairs as JSON properties. |
@JsonGetter | Marks a method as a getter for a specific property, alternative to @JsonProperty. | Useful when the method name doesn't follow JavaBean naming conventions or for custom serialization. |
@JsonPropertyOrder | Specifies the serialization order of JSON properties. | Apply when you need a custom order in your serialized JSON, different from the default alphabetical order. |
@JsonRawValue | Instructs Jackson to serialize a property exactly as it is. | Good for embedding custom JSON or pre-serialized JSON content. |
@JsonValue | Indicates a single method to use for serializing the entire instance. | Useful for serializing a complex object through a single method, often used with enums. |
@JsonRootName | Specifies the name of the root wrapper if wrapping is enabled. | Use when you need to wrap the JSON output in a root element with a custom name. |
@JsonSerialize | Indicates a custom serializer to use for a property or class. | Implement when you need custom serialization logic, different from the default Jackson provides. |
@JsonCreator | Marks a constructor or factory method for use in deserialization. | Helpful when the JSON structure doesn't match the Java object structure directly. |
@JacksonInject | Indicates that a property value will be obtained from the injection, not JSON. | Use for properties that need to be injected, not read from the JSON. |
@JsonAnySetter | Allows for the flexibility of adding unknown properties to a Map. | Apply when you want to handle additional, undeclared properties in JSON. |
@JsonSetter | Marks a method as a setter for a specific property, alternative to @JsonProperty. | Useful for custom deserialization logic or when method names do not follow JavaBean naming conventions. |
@JsonDeserialize | Indicates the use of a custom deserializer. | Use when custom deserialization logic is needed. |
@JsonAlias | Defines one or more alternative names for a property during deserialization. | Apply when a JSON property may have multiple aliases. |
0
Subscribe to my newsletter
Read articles from Rishabh Rahul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by