Java Code Examples for com.fasterxml.jackson.annotation.ObjectIdGenerators#PropertyGenerator

The following examples show how to use com.fasterxml.jackson.annotation.ObjectIdGenerators#PropertyGenerator . 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: EntityAnnotationIntrospector.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectIdInfo findObjectIdInfo(Annotated annotated) {
    Class<?> rawClass = annotated.getType().getRawClass();
    for (Type<?> type : model.getTypes()) {
        if (type.getClassType() == rawClass && type.getSingleKeyAttribute() != null) {
            Attribute<?, ?> attribute = type.getSingleKeyAttribute();
            String name = removePrefix(attribute.getPropertyName());
            if (useTableNames) {
                name = attribute.getName();
            }

            // if the name is overridden use that
            Class<?> superClass = rawClass.getSuperclass();
            while (superClass != Object.class && superClass != null) {
                try {
                    Field field = superClass.getDeclaredField(attribute.getPropertyName());
                    JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
                    if (jsonProperty != null) {
                        name = jsonProperty.value();
                        break;
                    }
                } catch (NoSuchFieldException ignored) {
                }
                superClass = superClass.getSuperclass();
            }

            return new ObjectIdInfo(new PropertyName(name), rawClass,
                    ObjectIdGenerators.PropertyGenerator.class,
                    EntityStoreResolver.class);
        }
    }
    return super.findObjectIdInfo(annotated);
}
 
Example 3
Source File: AbstractDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
    public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
            BeanProperty property) throws JsonMappingException
    {
        final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
        if (property != null && intr != null) {
            final AnnotatedMember accessor = property.getMember();
            if (accessor != null) {
                ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
                if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
                    JavaType idType;
                    ObjectIdGenerator<?> idGen;
                    SettableBeanProperty idProp = null;
                    ObjectIdResolver resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);

                    // 2.1: allow modifications by "id ref" annotations as well:
                    objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);
                    Class<?> implClass = objectIdInfo.getGeneratorType();

                    if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
                        PropertyName propName = objectIdInfo.getPropertyName();
                        idProp = (_properties == null) ? null : _properties.get(propName.getSimpleName());
                        if (idProp == null) {
                            ctxt.reportBadDefinition(_baseType, String.format(
                                    "Invalid Object Id definition for %s: cannot find property with name '%s'",
                                    handledType().getName(), propName));
                        }
                        idType = idProp.getType();
                        idGen = new PropertyBasedObjectIdGenerator(objectIdInfo.getScope());
/*
                         ctxt.reportBadDefinition(_baseType, String.format(
/
"Invalid Object Id definition for abstract type %s: cannot use `PropertyGenerator` on polymorphic types using property annotation",
handledType().getName()));
*/
                    } else { // other types simpler
                        resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
                        JavaType type = ctxt.constructType(implClass);
                        idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
                        idGen = ctxt.objectIdGeneratorInstance(accessor, objectIdInfo);
                    }
                    JsonDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
                    ObjectIdReader oir = ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(),
                             idGen, deser, idProp, resolver);
                    return new AbstractDeserializer(this, oir, null);
                }
            }
        }
        if (_properties == null) {
            return this;
        }
        // Need to ensure properties are dropped at this point, regardless
        return new AbstractDeserializer(this, _objectIdReader, null);
    }
 
Example 4
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 5
Source File: ObjectIdDeserializationTester.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
public IdParameterWrapperExt( @JsonProperty( "node" ) @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "customId" ) ValueParameterNodeExt node ) {
    this.test = node;
}
 
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();