com.fasterxml.jackson.annotation.ObjectIdGenerators Java Examples

The following examples show how to use com.fasterxml.jackson.annotation.ObjectIdGenerators. 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: Sequence.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public LogicalOperator deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
  ObjectIdGenerator<Integer> idGenerator = new ObjectIdGenerators.IntSequenceGenerator();
  JsonLocation start = jp.getCurrentLocation();
  JsonToken t = jp.getCurrentToken();
  LogicalOperator parent = null;
  LogicalOperator first = null;
  LogicalOperator prev = null;
  Integer id = null;

  while (true) {
    String fieldName = jp.getText();
    t = jp.nextToken();
    switch (fieldName) { // switch on field names.
    case "@id":
      id = _parseIntPrimitive(jp, ctxt);
      break;
    case "input":
      JavaType tp = ctxt.constructType(LogicalOperator.class);
      JsonDeserializer<Object> d = ctxt.findRootValueDeserializer(tp);
      parent = (LogicalOperator) d.deserialize(jp, ctxt);
      break;

    case "do":
      if (!jp.isExpectedStartArrayToken()) {
        throwE(
            jp,
            "The do parameter of sequence should be an array of SimpleOperators.  Expected a JsonToken.START_ARRAY token but received a "
                + t.name() + "token.");
      }

      int pos = 0;
      while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
        // logger.debug("Reading sequence child {}.", pos);
        JsonLocation l = jp.getCurrentLocation(); // get current location
                                                  // first so we can
                                                  // correctly reference the
                                                  // start of the object in
                                                  // the case that the type
                                                  // is wrong.
        LogicalOperator o = jp.readValueAs(LogicalOperator.class);

        if (pos == 0) {
          if (!(o instanceof SingleInputOperator) && !(o instanceof SourceOperator)) {
            throwE(
                l,
                "The first operator in a sequence must be either a ZeroInput or SingleInput operator.  The provided first operator was not. It was of type "
                    + o.getClass().getName());
          }
          first = o;
        } else {
          if (!(o instanceof SingleInputOperator)) {
            throwE(l, "All operators after the first must be single input operators.  The operator at position "
                + pos + " was not. It was of type " + o.getClass().getName());
          }
          SingleInputOperator now = (SingleInputOperator) o;
          now.setInput(prev);
        }
        prev = o;

        pos++;
      }
      break;
    default:
      throwE(jp, "Unknown field name provided for Sequence: " + jp.getText());
    }

    t = jp.nextToken();
    if (t == JsonToken.END_OBJECT) {
      break;
    }
  }

  if (first == null) {
    throwE(start, "A sequence must include at least one operator.");
  }
  if ((parent == null && first instanceof SingleInputOperator)
      || (parent != null && first instanceof SourceOperator)) {
    throwE(start,
        "A sequence must either start with a ZeroInputOperator or have a provided input. It cannot have both or neither.");
  }

  if (parent != null && first instanceof SingleInputOperator) {
    ((SingleInputOperator) first).setInput(parent);
  }

  // set input reference.
  if (id != null) {

    ReadableObjectId rid = ctxt.findObjectId(id, idGenerator, null);
    rid.bindItem(prev);
    // logger.debug("Binding id {} to item {}.", rid.id, rid.item);

  }

  return first;
}
 
Example #5
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 #6
Source File: ObjectIdDeserializationTester.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
public IdParameterWrapper( @JsonProperty( "node" ) @JsonIdentityInfo( generator = ObjectIdGenerators.IntSequenceGenerator.class,
        property = "@id" ) ValueParameterNode node ) {
    this.test = node;
}
 
Example #7
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 #8
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();