下面列出了com.fasterxml.jackson.annotation.JsonIdentityInfo#com.fasterxml.jackson.annotation.JsonIdentityReference 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@JsonProperty
@JacksonXmlElementWrapper( localName = "organisationUnits", namespace = DxfNamespaces.DXF_2_0 )
@JacksonXmlProperty( localName = "organisationUnit", namespace = DxfNamespaces.DXF_2_0 )
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
public Set<OrganisationUnit> getOrganisationUnits()
{
return organisationUnits;
}
private static void processBeanAnnotation( TreeLogger logger, JacksonTypeOracle typeOracle, RebindConfiguration configuration, JType
type, PropertyAccessors propertyAccessors, PropertyInfoBuilder builder ) throws UnableToCompleteException {
// identity
Optional<JsonIdentityInfo> jsonIdentityInfo = propertyAccessors.getAnnotation( JsonIdentityInfo.class );
Optional<JsonIdentityReference> jsonIdentityReference = propertyAccessors.getAnnotation( JsonIdentityReference.class );
// type info
Optional<JsonTypeInfo> jsonTypeInfo = propertyAccessors.getAnnotation( JsonTypeInfo.class );
Optional<JsonSubTypes> propertySubTypes = propertyAccessors.getAnnotation( JsonSubTypes.class );
// if no annotation is present that overrides bean processing, we just stop now
if ( !jsonIdentityInfo.isPresent() && !jsonIdentityReference.isPresent() && !jsonTypeInfo.isPresent() && !propertySubTypes
.isPresent() ) {
// no override on field
return;
}
// we need to find the bean to apply annotation on
Optional<JClassType> beanType = extractBeanType( logger, typeOracle, type, builder.getPropertyName() );
if ( beanType.isPresent() ) {
if ( jsonIdentityInfo.isPresent() || jsonIdentityReference.isPresent() ) {
builder.setIdentityInfo( BeanProcessor.processIdentity( logger, typeOracle, configuration, beanType
.get(), jsonIdentityInfo, jsonIdentityReference ) );
}
if ( jsonTypeInfo.isPresent() || propertySubTypes.isPresent() ) {
builder.setTypeInfo( BeanProcessor.processType( logger, typeOracle, configuration, beanType
.get(), jsonTypeInfo, propertySubTypes ) );
}
} else {
logger.log( Type.WARN, "Annotation present on property " + builder.getPropertyName() + " but no valid bean has been found." );
}
}
private Type processIdentity(Type propertyType, BeanPropertyWriter propertyWriter) {
final Class<?> clsT = Utils.getRawClassOrNull(propertyType);
final Class<?> clsW = propertyWriter.getType().getRawClass();
final Class<?> cls = clsT != null ? clsT : clsW;
if (cls != null) {
final JsonIdentityInfo identityInfoC = cls.getAnnotation(JsonIdentityInfo.class);
final JsonIdentityInfo identityInfoP = propertyWriter.getAnnotation(JsonIdentityInfo.class);
final JsonIdentityInfo identityInfo = identityInfoP != null ? identityInfoP : identityInfoC;
if (identityInfo == null) {
return null;
}
final JsonIdentityReference identityReferenceC = cls.getAnnotation(JsonIdentityReference.class);
final JsonIdentityReference identityReferenceP = propertyWriter.getAnnotation(JsonIdentityReference.class);
final JsonIdentityReference identityReference = identityReferenceP != null ? identityReferenceP : identityReferenceC;
final boolean alwaysAsId = identityReference != null && identityReference.alwaysAsId();
final Type idType;
if (identityInfo.generator() == ObjectIdGenerators.None.class) {
return null;
} else if (identityInfo.generator() == ObjectIdGenerators.PropertyGenerator.class) {
final BeanHelper beanHelper = getBeanHelper(cls);
if (beanHelper == null) {
return null;
}
final BeanPropertyWriter[] properties = beanHelper.getProperties();
final Optional<BeanPropertyWriter> idProperty = Stream.of(properties)
.filter(p -> p.getName().equals(identityInfo.property()))
.findFirst();
if (idProperty.isPresent()) {
final BeanPropertyWriter idPropertyWriter = idProperty.get();
final Member idMember = idPropertyWriter.getMember().getMember();
final PropertyMember idPropertyMember = wrapMember(settings.getTypeParser(), idMember, idPropertyWriter::getAnnotation, idPropertyWriter.getName(), cls);
idType = idPropertyMember.getType();
} else {
return null;
}
} else if (identityInfo.generator() == ObjectIdGenerators.IntSequenceGenerator.class) {
idType = Integer.class;
} else if (identityInfo.generator() == ObjectIdGenerators.UUIDGenerator.class) {
idType = String.class;
} else if (identityInfo.generator() == ObjectIdGenerators.StringIdGenerator.class) {
idType = String.class;
} else {
idType = Object.class;
}
return alwaysAsId
? idType
: new JUnionType(propertyType, idType);
}
return null;
}
private static Optional<BeanIdentityInfo> processIdentity( TreeLogger logger, JacksonTypeOracle typeOracle, RebindConfiguration
configuration, JClassType type ) throws UnableToCompleteException {
return processIdentity( logger, typeOracle, configuration, type, Optional.<JsonIdentityInfo>absent(), Optional
.<JsonIdentityReference>absent() );
}
@JsonProperty( "owner" )
@JsonIdentityReference
void setOwner( Owner owner ) {
this.owner = owner;
}
@JsonIdentityReference(alwaysAsId = true)
@JsonIdentityInfo(
resolver = ByidInstanceResolver.class,
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
Byid b();