com.fasterxml.jackson.annotation.JsonIdentityReference Java Examples

The following examples show how to use com.fasterxml.jackson.annotation.JsonIdentityReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Option.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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;
}
 
Example #2
Source File: PropertyProcessor.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
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." );
    }
}
 
Example #3
Source File: Jackson2Parser.java    From typescript-generator with MIT License 4 votes vote down vote up
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;
    }
 
Example #4
Source File: BeanProcessor.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
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() );
}
 
Example #5
Source File: ObjectIdBackReferenceTester.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@JsonProperty( "owner" )
@JsonIdentityReference
void setOwner( Owner owner ) {
    this.owner = owner;
}
 
Example #6
Source File: ByIdProperty.java    From immutables with Apache License 2.0 4 votes vote down vote up
@JsonIdentityReference(alwaysAsId = true)
@JsonIdentityInfo(
    resolver = ByidInstanceResolver.class,
    generator = ObjectIdGenerators.PropertyGenerator.class,
    property = "id")
Byid b();