コード例 #1
0
 @Override
 public TypeResolverBuilder<?> findPropertyTypeResolver(
     MapperConfig<?> config, AnnotatedMember am, JavaType baseType) {
   /* As per definition of @JsonTypeInfo, should only apply to contents of container
    * (collection, map) types, not container types themselves:
    */
   if (baseType.isContainerType()) return null;
   // No per-member type overrides (yet)
   return _findTypeResolver(config, am, baseType);
 }
コード例 #2
0
 @Override
 public TypeResolverBuilder<?> findPropertyContentTypeResolver(
     MapperConfig<?> config, AnnotatedMember am, JavaType containerType) {
   /* First: let's ensure property is a container type: caller should have
    * verified but just to be sure
    */
   if (!containerType.isContainerType()) {
     throw new IllegalArgumentException(
         "Must call method with a container type (got " + containerType + ")");
   }
   return _findTypeResolver(config, am, containerType);
 }
コード例 #3
0
 /**
  * Helper method used for figuring out if given raw type is a collection ("indexed") type; in
  * which case a wrapper element is typically added.
  */
 public static boolean isIndexedType(JavaType type) {
   if (type.isContainerType()) {
     Class<?> cls = type.getRawClass();
     // One special case; byte[] will be serialized as base64-encoded String, not real array, so:
     // (actually, ditto for char[]; thought to be a String)
     if (cls == byte[].class || cls == char[].class) {
       return false;
     }
     // issue#5: also, should not add wrapping for Maps
     if (Map.class.isAssignableFrom(cls)) {
       return false;
     }
     return true;
   }
   return false;
 }
  @Override
  protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
      throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator generator =
        this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
      writePrefix(generator, object);

      Class<?> serializationView = null;
      FilterProvider filters = null;
      Object value = object;
      JavaType javaType = null;
      if (object instanceof MappingJacksonValue) {
        MappingJacksonValue container = (MappingJacksonValue) object;
        value = container.getValue();
        serializationView = container.getSerializationView();
        filters = container.getFilters();
      }
      if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
        javaType = getJavaType(type, null);
      }
      ObjectWriter objectWriter;
      if (serializationView != null) {
        objectWriter = this.objectMapper.writerWithView(serializationView);
      } else if (filters != null) {
        objectWriter = this.objectMapper.writer(filters);
      } else {
        objectWriter = this.objectMapper.writer();
      }
      if (javaType != null && javaType.isContainerType()) {
        objectWriter = objectWriter.forType(javaType);
      }
      objectWriter.writeValue(generator, value);

      writeSuffix(generator, object);
      generator.flush();

    } catch (JsonProcessingException ex) {
      throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
  }
コード例 #5
0
 protected JsonDeserializer<?> _createDeserializer2(
     DeserializationContext ctxt,
     DeserializerFactory factory,
     JavaType type,
     BeanDescription beanDesc)
     throws JsonMappingException {
   final DeserializationConfig config = ctxt.getConfig();
   // If not, let's see which factory method to use:
   if (type.isEnumType()) {
     return factory.createEnumDeserializer(ctxt, type, beanDesc);
   }
   if (type.isContainerType()) {
     if (type.isArrayType()) {
       return factory.createArrayDeserializer(ctxt, (ArrayType) type, beanDesc);
     }
     if (type.isMapLikeType()) {
       MapLikeType mlt = (MapLikeType) type;
       if (mlt.isTrueMapType()) {
         return factory.createMapDeserializer(ctxt, (MapType) mlt, beanDesc);
       }
       return factory.createMapLikeDeserializer(ctxt, mlt, beanDesc);
     }
     if (type.isCollectionLikeType()) {
       /* 03-Aug-2012, tatu: As per [Issue#40], one exception is if shape
        *   is to be Shape.OBJECT. Ideally we'd determine it bit later on
        *   (to allow custom handler checks), but that won't work for other
        *   reasons. So do it here.
        */
       JsonFormat.Value format = beanDesc.findExpectedFormat(null);
       if (format == null || format.getShape() != JsonFormat.Shape.OBJECT) {
         CollectionLikeType clt = (CollectionLikeType) type;
         if (clt.isTrueCollectionType()) {
           return factory.createCollectionDeserializer(ctxt, (CollectionType) clt, beanDesc);
         }
         return factory.createCollectionLikeDeserializer(ctxt, clt, beanDesc);
       }
     }
   }
   if (JsonNode.class.isAssignableFrom(type.getRawClass())) {
     return factory.createTreeDeserializer(config, type, beanDesc);
   }
   return factory.createBeanDeserializer(ctxt, type, beanDesc);
 }
コード例 #6
0
  /**
   * 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;
  }