Core annotations used for value types, used by Jackson data binding package.
—
Control which properties are included or excluded during serialization and deserialization, with support for conditional inclusion and unknown property handling.
Exclude specific properties from serialization and deserialization.
/**
* Ignore property during serialization and deserialization
* @param value Whether to ignore the property (default: true)
*/
@JsonIgnore(boolean value = true)
public @interface JsonIgnore;Usage Examples:
public class User {
private String username;
// Always ignored
@JsonIgnore
private String password;
// Conditionally ignored (could be controlled programmatically)
@JsonIgnore(false)
private String email;
private String internalId;
@JsonIgnore
public String getInternalId() {
return internalId;
}
}Ignore multiple properties by name or ignore unknown properties.
/**
* Ignore multiple properties by name or configure unknown property handling
* @param value Array of property names to ignore
* @param ignoreUnknown Whether to ignore unknown properties during deserialization
* @param allowGetters Allow getter access for otherwise ignored properties
* @param allowSetters Allow setter access for otherwise ignored properties
*/
@JsonIgnoreProperties(String[] value = {},
boolean ignoreUnknown = false,
boolean allowGetters = false,
boolean allowSetters = false)
public @interface JsonIgnoreProperties;Usage Examples:
// Ignore specific properties
@JsonIgnoreProperties({"password", "internalData", "tempField"})
public class Account {
private String username;
private String password; // Ignored
private String email;
private Object internalData; // Ignored
private String tempField; // Ignored
}
// Ignore unknown properties during deserialization
@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiResponse {
private String status;
private String message;
// Unknown properties in JSON will be ignored
}
// Allow getter but not setter for ignored properties
@JsonIgnoreProperties(value = {"readOnlyField"}, allowGetters = true)
public class Config {
private String publicField;
private String readOnlyField; // Can be serialized but not deserialized
}Ignore all properties of a specific type.
/**
* Ignore all properties of annotated type
* @param value Whether to ignore the type (default: true)
*/
@JsonIgnoreType(boolean value = true)
public @interface JsonIgnoreType;Usage Examples:
@JsonIgnoreType
public class InternalMetadata {
private String internalId;
private long timestamp;
}
public class Document {
private String title;
private String content;
// This field will be ignored because InternalMetadata is annotated with @JsonIgnoreType
private InternalMetadata metadata;
}Control inclusion of properties based on their values.
/**
* Control inclusion of properties during serialization
* @param value Primary inclusion criteria
* @param content Inclusion criteria for collection/map/array contents
* @param valueFilter Custom filter class for value inclusion decisions
* @param contentFilter Custom filter class for content inclusion decisions
*/
@JsonInclude(JsonInclude.Include value = JsonInclude.Include.ALWAYS,
JsonInclude.Include content = JsonInclude.Include.ALWAYS,
Class<?> valueFilter = Void.class,
Class<?> contentFilter = Void.class)
public @interface JsonInclude {
enum Include {
/** Always include property */
ALWAYS,
/** Include only if value is not null */
NON_NULL,
/** Include only if value is not null and not "absent" (for Optional, etc.) */
NON_ABSENT,
/** Include only if value is not empty (null, empty string, empty collection) */
NON_EMPTY,
/** Include only if value differs from default/initial value */
NON_DEFAULT,
/** Use custom filter for inclusion decision */
CUSTOM,
/** Use default inclusion settings */
USE_DEFAULTS
}
}Usage Examples:
public class Product {
private String name;
// Only include if not null
@JsonInclude(JsonInclude.Include.NON_NULL)
private String description;
// Only include if not empty
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<String> tags;
// Only include if different from default value
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean active = true;
// Only include non-null values in the map
@JsonInclude(content = JsonInclude.Include.NON_NULL)
private Map<String, String> attributes;
}Include only specified properties (whitelist approach).
/**
* Include only specified properties (whitelist approach)
* @param value Array of property names to include
*/
@JsonIncludeProperties(String[] value)
public @interface JsonIncludeProperties;Usage Examples:
@JsonIncludeProperties({"id", "name", "email"})
public class UserSummary {
private Long id;
private String name;
private String email;
private String password; // Will be excluded
private String internalData; // Will be excluded
private List<String> roles; // Will be excluded
// Only id, name, and email will be serialized
}
// Can also be used on individual properties
public class Response {
@JsonIncludeProperties({"status", "message"})
private ApiResult result; // Only status and message from ApiResult will be included
}Configuration classes for programmatic inclusion/exclusion control.
/**
* Value class for JsonInclude configuration
*/
public static class JsonInclude.Value implements JacksonAnnotationValue<JsonInclude> {
public static final JsonInclude.Value EMPTY;
public static JsonInclude.Value construct(JsonInclude.Include valueInclusion,
JsonInclude.Include contentInclusion);
public JsonInclude.Include getValueInclusion();
public JsonInclude.Include getContentInclusion();
public Class<?> getValueFilter();
public Class<?> getContentFilter();
public JsonInclude.Value withValueInclusion(JsonInclude.Include incl);
public JsonInclude.Value withContentInclusion(JsonInclude.Include incl);
}
/**
* Value class for JsonIgnoreProperties configuration
*/
public static class JsonIgnoreProperties.Value implements JacksonAnnotationValue<JsonIgnoreProperties> {
public static final JsonIgnoreProperties.Value EMPTY;
public static JsonIgnoreProperties.Value forIgnoredProperties(String... propNames);
public static JsonIgnoreProperties.Value forIgnoreUnknown(boolean ignoreUnknown);
public Set<String> getIgnored();
public boolean getIgnoreUnknown();
public boolean getAllowGetters();
public boolean getAllowSetters();
public JsonIgnoreProperties.Value withIgnored(String... propNames);
public JsonIgnoreProperties.Value withIgnoreUnknown(boolean ignoreUnknown);
}@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiResponse<T> {
private String status;
private String message;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<String> errors;
@JsonIgnore
private long processingTime;
}public class User {
private String username;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String email;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String firstName;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String lastName;
@JsonIgnore
private String passwordHash;
// Only include if user has admin role
@JsonInclude(JsonInclude.Include.NON_NULL)
private String adminNotes;
}@JsonIgnoreProperties(ignoreUnknown = true) // Handle extra fields in old data
public class LegacyRecord {
private String id;
private String name;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String migratedField; // Only present in new records
@JsonIgnoreProperties({"deprecatedField1", "deprecatedField2"})
private SubRecord details;
}Install with Tessl CLI
npx tessl i tessl/maven-com-fasterxml-jackson-core--jackson-annotations