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

The following examples show how to use com.fasterxml.jackson.databind.JavaType#isArrayType() . 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: JsonUtility.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public static Type java2Type(JavaType java) {
    Type type = null;
    if (java.isArrayType()) {
        // 数组类型
        type = java.getRawClass();
    } else if (java.hasGenericTypes()) {
        // 泛型类型
        List<JavaType> javas = java.getBindings().getTypeParameters();
        Type[] generics = new Type[javas.size()];
        int index = 0;
        for (JavaType term : javas) {
            generics[index++] = java2Type(term);
        }
        Class<?> clazz = java.getRawClass();
        type = TypeUtility.parameterize(clazz, generics);
    } else {
        type = java.getRawClass();
    }
    return type;
}
 
Example 2
Source File: BasicClassIntrospector.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method used to decide whether we can omit introspection
 * for members (methods, fields, constructors); we may do so for
 * a limited number of container types JDK provides.
 */
protected boolean _isStdJDKCollection(JavaType type)
{
    if (!type.isContainerType() || type.isArrayType()) {
        return false;
    }
    Class<?> raw = type.getRawClass();
    String pkgName = ClassUtil.getPackageName(raw);
    if (pkgName != null) {
        if (pkgName.startsWith("java.lang")
                || pkgName.startsWith("java.util")) {
            /* 23-Sep-2014, tatu: Should we be conservative here (minimal number
             *    of matches), or ambitious? Let's do latter for now.
             */
            if (Collection.class.isAssignableFrom(raw)
                    || Map.class.isAssignableFrom(raw)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: CustomMessageElementVisitor.java    From caravan with Apache License 2.0 6 votes vote down vote up
protected FieldElement buildFieldElement(BeanProperty writer, Label label) throws JsonMappingException {
  FieldElement.Builder fBuilder = FieldElement.builder();

  fBuilder.name(writer.getName());
  fBuilder.tag(nextTag(writer));

  JavaType type = writer.getType();

  if (type.isArrayType() && type.getContentType().getRawClass() == byte.class) {
    fBuilder.label(label);
    fBuilder.type(ScalarType.BYTES);
  } else if (type.isArrayType() || type.isCollectionLikeType()) {
    fBuilder.label(Label.REPEATED);
    fBuilder.type(getDataType(type.getContentType()));
  } else if (type instanceof MapType) {
    Class<?> wrapperClass = DynamicClassFactory.INSTANCE.fetchOrCreatePairClass((MapType) type);

    fBuilder.label(Label.REPEATED);
    fBuilder.type(getDataType(SimpleType.constructUnsafe(wrapperClass)));
  } else {
    fBuilder.label(label);
    fBuilder.type(getDataType(type));
  }
  return fBuilder.build();
}
 
Example 4
Source File: EnumSchemaUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static <T> T constructEnumArrayClass(JavaType javaType) {
  if (javaType.isArrayType()) {
    return ReflectUtils.constructArrayType(javaType.getContentType().getRawClass());
  }

  return ReflectUtils.constructArrayType(Object.class);
}
 
Example 5
Source File: AnnotatedClassResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static AnnotatedClass resolve(MapperConfig<?> config, JavaType forType,
        MixInResolver r)
{
    if (forType.isArrayType() && skippableArray(config, forType.getRawClass())) {
        return createArrayType(config, forType.getRawClass());
    }
    return new AnnotatedClassResolver(config, forType, r).resolveFully();
}
 
Example 6
Source File: AnnotatedClassResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static AnnotatedClass resolveWithoutSuperTypes(MapperConfig<?> config, JavaType forType,
        MixInResolver r)
{
    if (forType.isArrayType() && skippableArray(config, forType.getRawClass())) {
        return createArrayType(config, forType.getRawClass());
    }
    return new AnnotatedClassResolver(config, forType, r).resolveWithoutSuperTypes();
}
 
Example 7
Source File: TypeUtils.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
static AnnotatedType toJavaType(JavaType jacksonType) {
    if (jacksonType.getRawClass().getTypeParameters().length > 0) {
        AnnotatedType[] paramTypes = jacksonType.getBindings().getTypeParameters().stream()
                .map(TypeUtils::toJavaType)
                .toArray(AnnotatedType[]::new);
        return TypeFactory.parameterizedAnnotatedClass(jacksonType.getRawClass(), jacksonType.getRawClass().getAnnotations(), paramTypes);
    }
    if (jacksonType.isArrayType()) {
        return TypeFactory.arrayOf(toJavaType(jacksonType.getContentType()), new Annotation[0]);
    }
    return GenericTypeReflector.annotate(jacksonType.getRawClass());
}
 
Example 8
Source File: TypeUtil.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
public static List<JavaType> resolveAllTypes(JavaType javaType, TypeFactory typeFactory, TypeMapping typeMapping) {
    if (javaType.isCollectionLikeType() || javaType.isArrayType()) {
        return resolveArrayTypes(javaType.getContentType(), typeFactory, typeMapping);
    } else {
        return resolveNonArrayTypes(javaType, typeFactory, typeMapping);
    }
}
 
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);
}
 
Example 10
Source File: JacksonJsonDataFormatMapper.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
protected void validateType(JavaType type, DeserializationTypeValidator validator, List<String> invalidTypes) {
  if (!type.isPrimitive()) {
    if (!type.isArrayType()) {
      validateTypeInternal(type, validator, invalidTypes);
    }
    if (type.isMapLikeType()) {
      validateType(type.getKeyType(), validator, invalidTypes);
    }
    if (type.isContainerType() || type.hasContentType()) {
      validateType(type.getContentType(), validator, invalidTypes);
    }
  }
}
 
Example 11
Source File: AbstractVariablesResource.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void validateType(JavaType type, DeserializationTypeValidator validator, List<String> invalidTypes) {
  if (!type.isPrimitive()) {
    if (!type.isArrayType()) {
      validateTypeInternal(type, validator, invalidTypes);
    }
    if (type.isMapLikeType()) {
      validateType(type.getKeyType(), validator, invalidTypes);
    }
    if (type.isContainerType() || type.hasContentType()) {
      validateType(type.getContentType(), validator, invalidTypes);
    }
  }
}
 
Example 12
Source File: StreamSpliterator.java    From riptide with MIT License 4 votes vote down vote up
StreamSpliterator(final JavaType type, final JsonParser parser) {
    this.type = type;
    this.parser = parser;
    this.isNotStreamOfArrays = !type.isArrayType() && !type.isCollectionLikeType();
}
 
Example 13
Source File: DefaultConverter.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
public Object cast(List<String> arguments, Parameter parameter, JavaType javaType, Map<String, Schema> definitions) throws ConversionException {
    if (arguments == null || arguments.size() == 0) {
        return null;
    }
    Class<?> cls = javaType.getRawClass();

    LOGGER.debug("converting array `" + arguments + "` to `" + cls + "`");
    if (javaType.isArrayType()) {
        if (parameter.getSchema() != null) {
            List<Object> output = new ArrayList<>();
            if (parameter.getSchema() instanceof ArraySchema) {
                ArraySchema arraySchema = (ArraySchema) parameter.getSchema();
                if (arraySchema.getItems() != null) {
                    Schema inner = arraySchema.getItems();

                    // TODO: this does not need to be done this way, update the helper method
                    Parameter innerParam = new QueryParameter().schema(inner);
                    JavaType innerClass = getTypeFromParameter(innerParam, definitions);
                    for (String obj : arguments) {
                        String[] parts = new String[0];
                        CSVFormat format = null;
                        if (Parameter.StyleEnum.FORM.equals(parameter.getStyle()) && !StringUtils.isEmpty(obj) && parameter.getExplode() == false) {
                            format = CSVFormat.DEFAULT;
                        } else if (Parameter.StyleEnum.PIPEDELIMITED.equals(parameter.getStyle()) && !StringUtils.isEmpty(obj)) {
                            format = CSVFormat.newFormat('|').withQuote('"');
                        } else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle()) && !StringUtils.isEmpty(obj)) {
                            format = CSVFormat.newFormat(' ').withQuote('"');
                        }
                        if (format != null) {
                            try {
                                for (CSVRecord record : CSVParser.parse(obj, format).getRecords()) {
                                    List<String> it = new ArrayList<String>();
                                    for (Iterator<String> x = record.iterator(); x.hasNext(); ) {
                                        it.add(x.next());
                                    }
                                    parts = it.toArray(new String[it.size()]);
                                }
                            } catch (IOException e) {
                            }
                        } else {
                            parts = new String[1];
                            parts[0] = obj;
                        }
                        for (String p : parts) {
                            Object ob = cast(p, inner, innerClass);
                            if (ob != null) {
                                output.add(ob);
                            }
                        }

                    }

                    return output;
                }
            }
        }
    } else if (parameter != null) {
        return cast(arguments.get(0), parameter.getSchema(), javaType);
    }
    return null;
}