Java Code Examples for com.fasterxml.jackson.databind.JavaType#getContentType()

The following examples show how to use com.fasterxml.jackson.databind.JavaType#getContentType() . 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: EnumNotPackedReadSchemas.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public EnumNotPackedReaders(Field protoField, JavaType javaType) {
  super(protoField, EnumSchemaUtils.constructEnumArrayClass(javaType));
  this.enumMeta = new EnumMeta(protoField, javaType.getContentType());

  collectionReader = (input, collection) -> {
    while (true) {
      int value = input.readEnum();
      Enum<?> enumValue = enumMeta.getEnumByValue(value);
      if (enumValue == null) {
        throw new IllegalStateException(
            String.format("invalid enum value %d for %s, proto field=%s:%s",
                value,
                javaType.getRawClass().getName(),
                ((Type) protoField.getParent()).getCanonicalName(),
                protoField.getName()));
      }

      collection.add(enumValue);

      int fieldNumber = input.readFieldNumber();
      if (fieldNumber != this.fieldNumber) {
        return fieldNumber;
      }
    }
  };
}
 
Example 2
Source File: EnumPackedReadSchemas.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public EnumPackedReaders(Field protoField, JavaType javaType) {
  super(protoField, EnumSchemaUtils.constructEnumArrayClass(javaType));
  this.enumMeta = new EnumMeta(protoField, javaType.getContentType());

  collectionReader = (input, collection) -> {
    while (true) {
      int value = input.readPackedEnum();
      Enum<?> enumValue = enumMeta.getEnumByValue(value);
      if (enumValue == null) {
        throw new IllegalStateException(
            String.format("invalid enum value %d for %s, proto field=%s:%s",
                value,
                javaType.getRawClass().getName(),
                ((Type) protoField.getParent()).getCanonicalName(),
                protoField.getName()));
      }

      collection.add(enumValue);

      int fieldNumber = input.readFieldNumber();
      if (fieldNumber != this.fieldNumber) {
        return fieldNumber;
      }
    }
  };
}
 
Example 3
Source File: ResponseRootDeserializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static boolean needConvert(Object obj, JavaType invocationTimeType) {
  if (obj == null || ClassUtils.isPrimitiveOrWrapper(obj.getClass()) || invocationTimeType.isPrimitive()
      || ProtoConst.OBJECT_TYPE.equals(invocationTimeType)) {
    return false;
  }

  if (obj.getClass() == invocationTimeType.getRawClass()) {
    return false;
  }

  if (invocationTimeType.getRawClass().isAssignableFrom(obj.getClass())) {
    if (invocationTimeType.getContentType() == null) {
      return false;
    }
  }

  return true;
}
 
Example 4
Source File: TypeFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Method for constructing a {@link CollectionType}.
 *<p>
 * NOTE: type modifiers are NOT called on Collection type itself; but are called
 * for contained types.
 */
public CollectionType constructCollectionType(Class<? extends Collection> collectionClass,
        JavaType elementType)
{
    TypeBindings bindings = TypeBindings.createIfNeeded(collectionClass, elementType);
    CollectionType result = (CollectionType) _fromClass(null, collectionClass, bindings);
    // 17-May-2017, tatu: As per [databind#1415], we better verify bound values if (but only if)
    //    type being resolved was non-generic (i.e.element type was ignored)
    if (bindings.isEmpty() && (elementType != null)) {
        JavaType t = result.findSuperType(Collection.class);
        JavaType realET = t.getContentType();
        if (!realET.equals(elementType)) {
            throw new IllegalArgumentException(String.format(
                    "Non-generic Collection class %s did not resolve to something with element type %s but %s ",
                    ClassUtil.nameOf(collectionClass), elementType, realET));
        }
    }
    return result;
}
 
Example 5
Source File: TypeFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Method for constructing a {@link MapType} instance
 *<p>
 * NOTE: type modifiers are NOT called on constructed type itself; but are called
 * for contained types.
 */
public MapType constructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) {
    TypeBindings bindings = TypeBindings.createIfNeeded(mapClass, new JavaType[] { keyType, valueType });
    MapType result = (MapType) _fromClass(null, mapClass, bindings);
    // 17-May-2017, tatu: As per [databind#1415], we better verify bound values if (but only if)
    //    type being resolved was non-generic (i.e.element type was ignored)
    if (bindings.isEmpty()) {
        JavaType t = result.findSuperType(Map.class);
        JavaType realKT = t.getKeyType();
        if (!realKT.equals(keyType)) {
            throw new IllegalArgumentException(String.format(
                    "Non-generic Map class %s did not resolve to something with key type %s but %s ",
                    ClassUtil.nameOf(mapClass), keyType, realKT));
        }
        JavaType realVT = t.getContentType();
        if (!realVT.equals(valueType)) {
            throw new IllegalArgumentException(String.format(
                    "Non-generic Map class %s did not resolve to something with value type %s but %s ",
                    ClassUtil.nameOf(mapClass), valueType, realVT));
        }
    }
    return result;
}
 
Example 6
Source File: BodyParamExtractionTest.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertDoubleArrayBodyParam() throws Exception {
    Map<String, Schema> definitions = ModelConverters.getInstance().read(Person.class);

    RequestBody body = new RequestBody().
            content(new Content()
                    .addMediaType("application/json",new MediaType().schema(new ArraySchema()
                            .items(new ArraySchema().items(new StringSchema())))));


    JavaType jt = utils.getTypeFromRequestBody(body, "application/json" ,definitions)[0];
    assertNotNull(jt);

    assertEquals(jt.getRawClass(), List[].class);
    JavaType inner = jt.getContentType();
    assertEquals(inner.getRawClass(), List.class);
    assertEquals(inner.getContentType().getRawClass(), String.class);

}
 
Example 7
Source File: FieldDocumentationVisitorWrapper.java    From spring-auto-restdocs with Apache License 2.0 6 votes vote down vote up
@Override
public JsonArrayFormatVisitor expectArrayFormat(JavaType arrayType)
        throws JsonMappingException {
    JavaType contentType = arrayType.getContentType();
    addFieldIfPresent(determineArrayOfType(contentType));
    if (contentType != null && shouldExpand() && (topLevelPath() || !wasVisited(contentType))) {
        log.trace("({}) {} expanding array", path, toString(contentType));
        // do not add this type to visited now, it will be done in expectObjectFormat for
        // content type of this array
        return new FieldDocumentationArrayVisitor(provider, context, path,
                typeRegistry, typeFactory, skipAccessor);
    } else {
        log.trace("({}) {} NOT expanding array", path, "<unknown>");
        return new JsonArrayFormatVisitor.Base();
    }
}
 
Example 8
Source File: CollectionLikeType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JavaType withHandlersFrom(JavaType src) {
    JavaType type = super.withHandlersFrom(src);
    JavaType srcCt = src.getContentType();
    if (srcCt != null) {
        JavaType ct = _elementType.withHandlersFrom(srcCt);
        if (ct != _elementType) {
            type = type.withContentType(ct);
        }
    }
    return type;
}
 
Example 9
Source File: JsonJacksonCodec.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected void initTypeInclusion(ObjectMapper mapObjectMapper) {
    TypeResolverBuilder<?> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {
        public boolean useForType(JavaType t) {
            switch (_appliesFor) {
            case NON_CONCRETE_AND_ARRAYS:
                while (t.isArrayType()) {
                    t = t.getContentType();
                }
                // fall through
            case OBJECT_AND_NON_CONCRETE:
                return (t.getRawClass() == Object.class) || !t.isConcrete();
            case NON_FINAL:
                while (t.isArrayType()) {
                    t = t.getContentType();
                }
                // to fix problem with wrong long to int conversion
                if (t.getRawClass() == Long.class) {
                    return true;
                }
                if (t.getRawClass() == XMLGregorianCalendar.class) {
                    return false;
                }
                return !t.isFinal(); // includes Object.class
            default:
                // case JAVA_LANG_OBJECT:
                return t.getRawClass() == Object.class;
            }
        }
    };
    mapTyper.init(JsonTypeInfo.Id.CLASS, null);
    mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
    mapObjectMapper.setDefaultTyping(mapTyper);
}