下面列出了怎么用com.fasterxml.jackson.annotation.JsonInclude的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Optional<JsonIncludeStrategy> getJsonIncludeStrategy(BeanAttributeInformation attributeDesc) {
Optional<JsonInclude> includeAnnotation = attributeDesc.getAnnotation(JsonInclude.class);
if (includeAnnotation.isPresent()) {
JsonInclude.Include value = includeAnnotation.get().value();
JsonIncludeStrategy strategy;
if (NON_NULL.equals(value)) {
strategy = JsonIncludeStrategy.NOT_NULL;
}
else if (JsonInclude.Include.NON_EMPTY.equals(value)) {
strategy = JsonIncludeStrategy.NON_EMPTY;
}
else {
strategy = JsonIncludeStrategy.DEFAULT;
}
return Optional.of(strategy);
}
return Optional.empty();
}
/**
* Get firstName
* @return firstName
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_FIRST_NAME)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getFirstName() {
return firstName;
}
private static ObjectMapper defaultMapper() {
final ObjectMapper objectMapper = JsonMapper.builder()
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS, false)
.build()
.registerModule(new Jdk8Module())
.registerModule(new InterledgerModule(Encoding.BASE64));
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
return objectMapper;
}
/**
* None
* @return string
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "None")
@JsonProperty(JSON_PROPERTY_STRING)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getString() {
return string;
}
/**
* Get _byte
* @return _byte
**/
@ApiModelProperty(required = true, value = "")
@JsonProperty(JSON_PROPERTY_BYTE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
@JacksonXmlProperty(localName = "byte")
public byte[] getByte() {
return _byte;
}
@Override
public JsonInclude.Value findPropertyInclusion(Annotated a)
{
JsonInclude.Value v2 = _secondary.findPropertyInclusion(a);
JsonInclude.Value v1 = _primary.findPropertyInclusion(a);
if (v2 == null) { // shouldn't occur but
return v1;
}
return v2.withOverrides(v1);
}
/**
* Get boolItem
* @return boolItem
**/
@NotNull
@ApiModelProperty(example = "true", required = true, value = "")
@JsonProperty(JSON_PROPERTY_BOOL_ITEM)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public Boolean isBoolItem() {
return boolItem;
}
/**
* pet status in the store
* @return status
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "pet status in the store")
@JsonProperty(JSON_PROPERTY_STATUS)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public StatusEnum getStatus() {
return status;
}
/**
* Get prefixNsNumber
* @return prefixNsNumber
**/
@javax.annotation.Nullable
@ApiModelProperty(example = "1.234", value = "")
@JsonProperty(JSON_PROPERTY_PREFIX_NS_NUMBER)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public BigDecimal getPrefixNsNumber() {
return prefixNsNumber;
}
/**
* Get prefixString
* @return prefixString
**/
@javax.annotation.Nullable
@ApiModelProperty(example = "string", value = "")
@JsonProperty(JSON_PROPERTY_PREFIX_STRING)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPrefixString() {
return prefixString;
}
/**
* Get name
* @return name
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_NAME)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getName() {
return name;
}
/**
* Get breed
* @return breed
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_BREED)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getBreed() {
return breed;
}
/**
* Get propertyClass
* @return propertyClass
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPropertyClass() {
return propertyClass;
}
/**
* Get shapeType
* @return shapeType
**/
@ApiModelProperty(required = true, value = "")
@JsonProperty(JSON_PROPERTY_SHAPE_TYPE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public String getShapeType() {
return shapeType;
}
/**
* Get smallSnake
* @return smallSnake
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_SMALL_SNAKE)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getSmallSnake() {
return smallSnake;
}
private CustomizationObjectMapper (DateFormat dateFormat) {
super();
// 设置格式化
setDateFormat( dateFormat );
// <code>null<code> 不序列化
setSerializationInclusion( JsonInclude.Include.NON_NULL );
// 遇到未知属性,略过
configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
}
/**
* Get name
* @return name
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_NAME)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Integer getName() {
return name;
}
/**
* Get photoUrls
* @return photoUrls
**/
@NotNull
@ApiModelProperty(required = true, value = "")
@JsonProperty(JSON_PROPERTY_PHOTO_URLS)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public Set<String> getPhotoUrls() {
return photoUrls;
}
/**
* Get mapMapAnytype
* @return mapMapAnytype
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_MAP_MAP_ANYTYPE)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Map<String, Map<String, Object>> getMapMapAnytype() {
return mapMapAnytype;
}
/**
* Get mapBoolean
* @return mapBoolean
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_MAP_BOOLEAN)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Map<String, Boolean> getMapBoolean() {
return mapBoolean;
}
/**
* Get id
* @return id
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_ID)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Long getId() {
return id;
}
/**
* Get propertyClass
* @return propertyClass
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPropertyClass() {
return propertyClass;
}
/**
* Get id
* @return id
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_ID)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Long getId() {
return id;
}
/**
* Get password
* @return password
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PASSWORD)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPassword() {
return password;
}
/**
* Get _return
* @return _return
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_RETURN)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Integer getReturn() {
return _return;
}
/**
* Get password
* @return password
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_PASSWORD)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public String getPassword() {
return password;
}
/**
* Get nameNumber
* @return nameNumber
**/
@javax.annotation.Nullable
@ApiModelProperty(example = "1.234", value = "")
@JsonProperty(JSON_PROPERTY_NAME_NUMBER)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public BigDecimal getNameNumber() {
return nameNumber;
}
/**
* Get int64
* @return int64
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_INT64)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Long getInt64() {
return int64;
}
/**
* Get files
* @return files
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
@JsonProperty(JSON_PROPERTY_FILES)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public List<java.io.File> getFiles() {
return files;
}
/**
* Helper method used for finding inclusion definitions for structured
* container types like <code>Map</code>s and referential types
* (like <code>AtomicReference</code>).
*
* @param contentType Declared full content type of container
* @param configType Raw base type under which `configOverride`, if any, needs to be defined
*/
protected JsonInclude.Value _findInclusionWithContent(SerializerProvider prov,
BeanDescription beanDesc,
JavaType contentType, Class<?> configType)
throws JsonMappingException
{
final SerializationConfig config = prov.getConfig();
// Defaulting gets complicated because we might have two distinct
// axis to consider: Container type itself , and then value (content) type.
// Start with Container-defaults, then use more-specific value override, if any.
// Start by getting global setting, overridden by Map-type-override
JsonInclude.Value inclV = beanDesc.findPropertyInclusion(config.getDefaultPropertyInclusion());
inclV = config.getDefaultPropertyInclusion(configType, inclV);
// and then merge content-type overrides, if any. But note that there's
// content-to-value inclusion shift we have to do
JsonInclude.Value valueIncl = config.getDefaultPropertyInclusion(contentType.getRawClass(), null);
if (valueIncl != null) {
switch (valueIncl.getValueInclusion()) {
case USE_DEFAULTS:
break;
case CUSTOM:
inclV = inclV.withContentFilter(valueIncl.getContentFilter());
break;
default:
inclV = inclV.withContentInclusion(valueIncl.getValueInclusion());
}
}
return inclV;
}