Java Code Examples for com.fasterxml.jackson.databind.type.TypeFactory#defaultInstance()

The following examples show how to use com.fasterxml.jackson.databind.type.TypeFactory#defaultInstance() . 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: ParameterJavaTypeDiscovererTest.java    From evernote-rest-webapp with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters
public static Collection<Object[]> data() {
	TypeFactory typeFactory = TypeFactory.defaultInstance();

	return Arrays.asList(new Object[][]{
			// method, expected JavaTypes
			{ReflectionUtils.findMethod(Foo.class, "takeString", null),  // null indicates search by name
					new JavaType[]{typeFactory.constructType(String.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeNothing", null), new JavaType[]{}},
			{ReflectionUtils.findMethod(Foo.class, "takeStringList", null),
					new JavaType[]{typeFactory.constructCollectionType(List.class, String.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeIntegerList", null),
					new JavaType[]{typeFactory.constructCollectionType(List.class, Integer.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeLongList", null),
					new JavaType[]{typeFactory.constructCollectionType(List.class, Long.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeStringSet", null),
					new JavaType[]{typeFactory.constructCollectionType(Set.class, String.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeIntegerSet", null),
					new JavaType[]{typeFactory.constructCollectionType(Set.class, Integer.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeLongSet", null),
					new JavaType[]{typeFactory.constructCollectionType(Set.class, Long.class)}},
			{ReflectionUtils.findMethod(Foo.class, "takeStringAndInteger", null),
					new JavaType[]{typeFactory.constructType(String.class), typeFactory.constructType(Integer.class)}},
	});
}
 
Example 2
Source File: KvMapperFactory.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
<T> Map<String, T> loadProps(Class<?> clazz, Function<KvProperty, T> func) {
    ImmutableMap.Builder<String, T> b = ImmutableMap.builder();
    TypeFactory tf = TypeFactory.defaultInstance();
    while(clazz != null && !Object.class.equals(clazz)) {
        for(Field field: clazz.getDeclaredFields()) {
            KvMapping mapping = field.getAnnotation(KvMapping.class);
            if(mapping == null) {
                continue;
            }
            JavaType javaType;
            String typeStr = mapping.type();
            if(!typeStr.isEmpty()) {
                javaType = tf.constructFromCanonical(typeStr);
            } else {
                javaType = tf.constructType(field.getGenericType());
            }
            KvProperty property = new KvProperty(this, field.getName(), field, javaType);
            b.put(property.getKey(), func.apply(property));
        }
        clazz = clazz.getSuperclass();
    }
    return b.build();
}
 
Example 3
Source File: EnumTypeIdResolver.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void init(JavaType baseType) {
  this.baseType = baseType;
  Class<?> baseClass = baseType.getRawClass();
  TypesEnum typesEnum = baseClass.getAnnotation(TypesEnum.class);
  while (baseClass != null && typesEnum == null) {
    baseClass = baseClass.getSuperclass();
    typesEnum = baseClass.getAnnotation(TypesEnum.class);
  }
  if (typesEnum == null) {
    throw new NullPointerException("Missing annotation TypesEnum on " + baseType.getRawClass());
  }
  SubTypeMapping mapping = new SubTypeMapping(typesEnum);
  TypeFactory defaultInstance = TypeFactory.defaultInstance();
  StringBuilder sb = new StringBuilder();
  for (Enum<?> e : mapping.getEnumConstants()) {
    String name = e.name();
    String className = mapping.getClassName(e);
    try {
      Class<?> c = Class.forName(className, false, this.getClass().getClassLoader());
      JavaType type = defaultInstance.uncheckedSimpleType(c);
      this.nameToType.put(name.toLowerCase(), type);
      this.typeToName.put(c, name);
      sb.append(name + " => " + c.getName() + "\n");
    } catch (ClassNotFoundException e1) {
      throw new RuntimeException(String.format(
          "class not found %s for enum value %s for base type %s",
          className, name, baseType
          ) , e1);
    }
  }
  this.description = sb.toString();
}
 
Example 4
Source File: ListJacksonJsonTypeDetector.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
protected JavaType constructType(Object object) {
  TypeFactory typeFactory = TypeFactory.defaultInstance();

  if (object instanceof List && !((List<?>) object).isEmpty()) {
    List<?> list = (List<?>) object;
    Object firstElement = list.get(0);
    if (bindingsArePresent(list.getClass())) {
      final JavaType elementType = constructType(firstElement);
      return typeFactory.constructCollectionType(list.getClass(), elementType);
    }
  }
  return typeFactory.constructType(object.getClass());
}
 
Example 5
Source File: ClassAliasIdResolverTest.java    From simple-spring-memcached with MIT License 5 votes vote down vote up
@Before
public void setUp() {
    baseType = Mockito.mock(JavaType.class);
    typeFactory = TypeFactory.defaultInstance();

    resolver = new ClassAliasIdResolver(baseType, typeFactory, new HashMap<String, Class<?>>(), new HashMap<Class<?>, String>());
}
 
Example 6
Source File: UnmarshallingTester.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 7
Source File: CertificateUnmarshallingTest.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 8
Source File: AbstractTestValidationExecutor.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ObjectMapper getObjectMapper() {
	ObjectMapper om = new ObjectMapper();
	JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
	om.setAnnotationIntrospector(jai);
	om.enable(SerializationFeature.INDENT_OUTPUT);
	return om;
}
 
Example 9
Source File: KAEntityCollectionFetcherTask.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 5 votes vote down vote up
public KAEntityCollectionFetcherTask(Class<T> type, String url) {
		this.url = url;
		TypeFactory f = TypeFactory.defaultInstance();
//		this.type = new TypeReference<List<T>>() {};
//		this.type = f.constructParametricType(ArrayList.class, type);
		this.type = f.constructCollectionType(ArrayList.class, type);
	}
 
Example 10
Source File: AllureReportUtils.java    From allure1 with Apache License 2.0 5 votes vote down vote up
/**
 * Create Jackson mapper with {@link JaxbAnnotationIntrospector}
 *
 * @return {@link com.fasterxml.jackson.databind.ObjectMapper}
 */
public static ObjectMapper createMapperWithJaxbAnnotationInspector() {
    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector annotationInspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    mapper.getSerializationConfig().with(annotationInspector);
    return mapper;
}
 
Example 11
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 5 votes vote down vote up
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
    SearchHit[] hits = response.getHits().getHits();
    TypeFactory factory = TypeFactory.defaultInstance();
    MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
    List<Message> messages = new ArrayList<>(hits.length);
    for (SearchHit hit : hits) {
        String source = hit.getSourceAsString();
        Map<String, String> mapSource = objectMapper.readValue(source, type);
        Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
        messages.add(msg);
    }
    return messages;
}
 
Example 12
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
    SearchHit[] hits = response.getHits().getHits();
    TypeFactory factory = TypeFactory.defaultInstance();
    MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
    List<Message> messages = new ArrayList<>(hits.length);
    for (SearchHit hit : hits) {
        String source = hit.getSourceAsString();
        Map<String, String> mapSource = objectMapper.readValue(source, type);
        Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
        messages.add(msg);
    }
    return messages;
}
 
Example 13
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
    SearchHit[] hits = response.getHits().getHits();
    TypeFactory factory = TypeFactory.defaultInstance();
    MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
    List<Message> messages = new ArrayList<>(hits.length);
    for (SearchHit hit : hits) {
        String source = hit.getSourceAsString();
        Map<String, String> mapSource = objectMapper.readValue(source, type);
        Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
        messages.add(msg);
    }
    return messages;
}
 
Example 14
Source File: JacksonUtil.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
public static Map mapJsonToMap(String json, Class keyClass, Class valueClass) {
    Map map;
    TypeFactory t = TypeFactory.defaultInstance();
    try {
        map = OBJECT_MAPPER.readValue(json, t.constructMapType(HashMap.class, keyClass, valueClass));
    } catch (IOException e) {
        throw new IllegalArgumentException("The given string value: "
                + json + " cannot be transformed to Map<" + keyClass.getName() + ", " + valueClass.getName() + ">.");
    }
    return map;
}
 
Example 15
Source File: JacksonUtil.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
public static List mapJsonToObjectList(String json, Class clazz) {
    List list;
    TypeFactory t = TypeFactory.defaultInstance();
    try {
        list = OBJECT_MAPPER.readValue(json, t.constructCollectionType(ArrayList.class, clazz));
    } catch (IOException e) {
        throw new IllegalArgumentException("The given string value: "
                + json + " cannot be transformed to List of " + clazz.getName());
    }

    return list;
}
 
Example 16
Source File: ListJacksonJsonTypeDetector.java    From camunda-external-task-client-java with Apache License 2.0 5 votes vote down vote up
protected JavaType constructType(Object object) {
  TypeFactory typeFactory = TypeFactory.defaultInstance();

  if (object instanceof List && !((List<?>) object).isEmpty()) {
    List<?> list = (List<?>) object;
    Object firstElement = list.get(0);
    if (bindingsArePresent(list.getClass())) {
      final JavaType elementType = constructType(firstElement);
      return typeFactory.constructCollectionType(list.getClass(), elementType);
    }
  }
  return typeFactory.constructType(object.getClass());
}
 
Example 17
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 4 votes vote down vote up
protected DummyTypeIdResolver() {
	super(SimpleType.constructUnsafe(Object.class), TypeFactory.defaultInstance(), BasicPolymorphicTypeValidator
		.builder().build());
}
 
Example 18
Source File: JacksonMarshallingStrategy.java    From ob1k with Apache License 2.0 4 votes vote down vote up
private JavaType getJacksonType(final Type type) {

    final TypeFactory typeFactory = TypeFactory.defaultInstance();
    return typeFactory.constructType(type);
  }
 
Example 19
Source File: JsonRequestMarshaller.java    From ob1k with Apache License 2.0 4 votes vote down vote up
private JavaType getJacksonType(final Type type) {
  final TypeFactory typeFactory = TypeFactory.defaultInstance();
  return typeFactory.constructType(type);
}
 
Example 20
Source File: OpenEHRTypeNaming.java    From archie with Apache License 2.0 4 votes vote down vote up
protected OpenEHRTypeNaming() {
    super(TypeFactory.defaultInstance().constructType(OpenEHRBase.class), TypeFactory.defaultInstance());
}