@Override
 public JsonArrayFormatVisitor expectArrayFormat(JavaType type) throws JsonMappingException {
   serializerProvider
       .findValueSerializer(type.getContentType())
       .acceptJsonFormatVisitor(createSubtraverser(baseName), type.getContentType());
   return new JsonArrayFormatVisitor.Base(serializerProvider);
 }
 public void acceptJsonFormatVisitor(
     JsonFormatVisitorWrapper paramJsonFormatVisitorWrapper, JavaType paramJavaType)
     throws JsonMappingException {
   JsonArrayFormatVisitor localJsonArrayFormatVisitor =
       paramJsonFormatVisitorWrapper.expectArrayFormat(paramJavaType);
   if (localJsonArrayFormatVisitor != null) {
     JavaType localJavaType =
         paramJsonFormatVisitorWrapper
             .getProvider()
             .getTypeFactory()
             .moreSpecificType(this._elementType, paramJavaType.getContentType());
     if (localJavaType == null) {
       throw new JsonMappingException("Could not resolve type");
     }
     JsonSerializer localJsonSerializer = this._elementSerializer;
     paramJavaType = localJsonSerializer;
     if (localJsonSerializer == null) {
       paramJavaType =
           paramJsonFormatVisitorWrapper
               .getProvider()
               .findValueSerializer(localJavaType, this._property);
     }
     localJsonArrayFormatVisitor.itemsFormat(paramJavaType, localJavaType);
   }
 }
 @Override
 public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
     throws JsonMappingException {
   // May need to resolve types for delegate-based creators:
   JsonDeserializer<Object> delegate = null;
   if (_valueInstantiator != null) {
     AnnotatedWithParams delegateCreator = _valueInstantiator.getDelegateCreator();
     if (delegateCreator != null) {
       JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
       delegate = findDeserializer(ctxt, delegateType, property);
     }
   }
   JsonDeserializer<?> valueDeser = _valueDeserializer;
   if (valueDeser == null) {
     // #125: May have a content converter
     valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
     if (valueDeser == null) {
       // And we may also need to get deserializer for String
       valueDeser =
           ctxt.findContextualValueDeserializer(_collectionType.getContentType(), property);
     }
   } else { // if directly assigned, probably not yet contextual, so:
     valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property);
   }
   if (isDefaultDeserializer(valueDeser)) {
     valueDeser = null;
   }
   return withResolved(delegate, valueDeser);
 }
Ejemplo n.º 4
0
  /** @since 2.8 */
  public static MapSerializer construct(
      Set<String> ignoredEntries,
      JavaType mapType,
      boolean staticValueType,
      TypeSerializer vts,
      JsonSerializer<Object> keySerializer,
      JsonSerializer<Object> valueSerializer,
      Object filterId) {
    JavaType keyType, valueType;

    if (mapType == null) {
      keyType = valueType = UNSPECIFIED_TYPE;
    } else {
      keyType = mapType.getKeyType();
      valueType = mapType.getContentType();
    }
    // If value type is final, it's same as forcing static value typing:
    if (!staticValueType) {
      staticValueType = (valueType != null && valueType.isFinal());
    } else {
      // also: Object.class can not be handled as static, ever
      if (valueType.getRawClass() == Object.class) {
        staticValueType = false;
      }
    }
    MapSerializer ser =
        new MapSerializer(
            ignoredEntries,
            keyType,
            valueType,
            staticValueType,
            vts,
            keySerializer,
            valueSerializer);
    if (filterId != null) {
      ser = ser.withFilterId(filterId);
    }
    return ser;
  }
  @Override
  public Object findInjectableValue(
      Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) {
    // if a container, get content type

    JavaType type = forProperty.getType();
    if (type.isCollectionLikeType() | type.isArrayType()) {
      type = type.getContentType();
    }
    // checking annotation catches properties with interface types
    if (ObjBase.class.isAssignableFrom(type.getRawClass())
        || forProperty.getMember().getAnnotated().isAnnotationPresent(Reference.class)) {
      try {
        return super.findInjectableValue(valueId, ctxt, forProperty, beanInstance);
      } catch (IllegalArgumentException e) {
        Object value = getValue(valueId, type);
        return value;
      }

    } else {
      return super.findInjectableValue(valueId, ctxt, forProperty, beanInstance);
    }
  }
 /**
  * Method called to finalize setup of this deserializer, when it is known for which property
  * deserializer is needed for.
  */
 @Override
 public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
     throws JsonMappingException {
   // note: instead of finding key deserializer, with enums we actually
   // work with regular deserializers (less code duplication; but not
   // quite as clean as it ought to be)
   KeyDeserializer kd = _keyDeserializer;
   if (kd == null) {
     kd = ctxt.findKeyDeserializer(_mapType.getKeyType(), property);
   }
   JsonDeserializer<?> vd = _valueDeserializer;
   final JavaType vt = _mapType.getContentType();
   if (vd == null) {
     vd = ctxt.findContextualValueDeserializer(vt, property);
   } else { // if directly assigned, probably not yet contextual, so:
     vd = ctxt.handleSecondaryContextualization(vd, property, vt);
   }
   TypeDeserializer vtd = _valueTypeDeserializer;
   if (vtd != null) {
     vtd = vtd.forProperty(property);
   }
   return withResolved(kd, vd, vtd);
 }
Ejemplo n.º 7
0
  public static MapSerializer construct(
      String[] ignoredList,
      JavaType mapType,
      boolean staticValueType,
      TypeSerializer vts,
      JsonSerializer<Object> keySerializer,
      JsonSerializer<Object> valueSerializer) {
    HashSet<String> ignoredEntries = toSet(ignoredList);
    JavaType keyType, valueType;

    if (mapType == null) {
      keyType = valueType = UNSPECIFIED_TYPE;
    } else {
      keyType = mapType.getKeyType();
      valueType = mapType.getContentType();
    }
    // If value type is final, it's same as forcing static value typing:
    if (!staticValueType) {
      staticValueType = (valueType != null && valueType.isFinal());
    }
    return new MapSerializer(
        ignoredEntries, keyType, valueType, staticValueType, vts, keySerializer, valueSerializer);
  }
 @Override
 public JavaType getContentType() {
   return _mapType.getContentType();
 }
  /**
   * Method called to see if given method has annotations that indicate a more specific type than
   * what the argument specifies. If annotations are present, they must specify compatible Class;
   * instance of which can be assigned using the method. This means that the Class has to be raw
   * class of type, or its sub-class (or, implementing class if original Class instance is an
   * interface).
   *
   * @param a Method or field that the type is associated with
   * @param type Type derived from the setter argument
   * @return Original type if no annotations are present; or a more specific type derived from it if
   *     type annotation(s) was found
   * @throws JsonMappingException if invalid annotation is found
   */
  private JavaType modifyTypeByAnnotation(DeserializationContext ctxt, Annotated a, JavaType type)
      throws JsonMappingException {
    // first: let's check class for the instance itself:
    AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
    Class<?> subclass = intr.findDeserializationType(a, type);
    if (subclass != null) {
      try {
        type = type.narrowBy(subclass);
      } catch (IllegalArgumentException iae) {
        throw new JsonMappingException(
            "Failed to narrow type "
                + type
                + " with concrete-type annotation (value "
                + subclass.getName()
                + "), method '"
                + a.getName()
                + "': "
                + iae.getMessage(),
            null,
            iae);
      }
    }

    // then key class
    if (type.isContainerType()) {
      Class<?> keyClass = intr.findDeserializationKeyType(a, type.getKeyType());
      if (keyClass != null) {
        // illegal to use on non-Maps
        if (!(type instanceof MapLikeType)) {
          throw new JsonMappingException(
              "Illegal key-type annotation: type " + type + " is not a Map(-like) type");
        }
        try {
          type = ((MapLikeType) type).narrowKey(keyClass);
        } catch (IllegalArgumentException iae) {
          throw new JsonMappingException(
              "Failed to narrow key type "
                  + type
                  + " with key-type annotation ("
                  + keyClass.getName()
                  + "): "
                  + iae.getMessage(),
              null,
              iae);
        }
      }
      JavaType keyType = type.getKeyType();
      /* 21-Mar-2011, tatu: ... and associated deserializer too (unless already assigned)
       *   (not 100% why or how, but this does seem to get called more than once, which
       *   is not good: for now, let's just avoid errors)
       */
      if (keyType != null && keyType.getValueHandler() == null) {
        Object kdDef = intr.findKeyDeserializer(a);
        if (kdDef != null) {
          KeyDeserializer kd = ctxt.keyDeserializerInstance(a, kdDef);
          if (kd != null) {
            type = ((MapLikeType) type).withKeyValueHandler(kd);
            keyType = type.getKeyType(); // just in case it's used below
          }
        }
      }

      // and finally content class; only applicable to structured types
      Class<?> cc = intr.findDeserializationContentType(a, type.getContentType());
      if (cc != null) {
        try {
          type = type.narrowContentsBy(cc);
        } catch (IllegalArgumentException iae) {
          throw new JsonMappingException(
              "Failed to narrow content type "
                  + type
                  + " with content-type annotation ("
                  + cc.getName()
                  + "): "
                  + iae.getMessage(),
              null,
              iae);
        }
      }
      // ... as well as deserializer for contents:
      JavaType contentType = type.getContentType();
      if (contentType.getValueHandler()
          == null) { // as with above, avoid resetting (which would trigger exception)
        Object cdDef = intr.findContentDeserializer(a);
        if (cdDef != null) {
          JsonDeserializer<?> cd = null;
          if (cdDef instanceof JsonDeserializer<?>) {
            cdDef = (JsonDeserializer<?>) cdDef;
          } else {
            Class<?> cdClass =
                _verifyAsClass(cdDef, "findContentDeserializer", JsonDeserializer.None.class);
            if (cdClass != null) {
              cd = ctxt.deserializerInstance(a, cdClass);
            }
          }
          if (cd != null) {
            type = type.withContentValueHandler(cd);
          }
        }
      }
    }
    return type;
  }
 @Override
 public JavaType getContentType() {
   return _collectionType.getContentType();
 }