本文整理汇总了Java中com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance方法的典型用法代码示例。如果您正苦于以下问题:Java TypeFactory.defaultInstance方法的具体用法?Java TypeFactory.defaultInstance怎么用?Java TypeFactory.defaultInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.databind.type.TypeFactory
的用法示例。
在下文中一共展示了TypeFactory.defaultInstance方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateValueType
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
/**
* Generates a {@link JavaType} that matches the target Thrift field represented by the provided
* {@code <E>} enumerated value. If the field's type includes generics, the generics will
* be added to the generated {@link JavaType} to support proper conversion.
* @param thriftInstance The Thrift-generated class instance that will be converted to/from JSON.
* @param field A {@code <E>} enumerated value that represents a field in a Thrift-based entity.
* @return The {@link JavaType} representation of the type associated with the field.
* @throws NoSuchFieldException if unable to determine the field's type.
* @throws SecurityException if unable to determine the field's type.
*/
protected JavaType generateValueType(final T thriftInstance, final E field) throws NoSuchFieldException, SecurityException {
final TypeFactory typeFactory = TypeFactory.defaultInstance();
final Field declaredField = thriftInstance.getClass().getDeclaredField(field.getFieldName());
if(declaredField.getType().equals(declaredField.getGenericType())) {
log.debug("Generating JavaType for type '{}'.", declaredField.getType());
return typeFactory.constructType(declaredField.getType());
} else {
final ParameterizedType type = (ParameterizedType)declaredField.getGenericType();
final Class<?>[] parameterizedTypes = new Class<?>[type.getActualTypeArguments().length];
for(int i=0; i<type.getActualTypeArguments().length; i++) {
parameterizedTypes[i] = (Class<?>)type.getActualTypeArguments()[i];
}
log.debug("Generating JavaType for type '{}' with generics '{}'", declaredField.getType(), parameterizedTypes);
return typeFactory.constructParametricType(declaredField.getType(), parameterizedTypes);
}
}
示例2: loadProps
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
<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();
}
示例3: mapJsonToObjectList
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
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;
}
示例4: mapJsonToMap
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
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;
}
示例5: init
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
@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();
}
示例6: setup
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
@Before
public void setup() throws Exception {
// checks that all required dependencies are provided.
assertNotNull("WebApplicationContext isn't provided.", wac);
assertNotNull("ObjectMapper isn't provided.", objectMapper);
typeFactory = TypeFactory.defaultInstance();
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
示例7: CustomObjectMapper
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
public CustomObjectMapper() {
//使用默认时区
BaseSettings baseSettings = new BaseSettings(new BasicClassIntrospector(),
DEFAULT_ANNOTATION_INTROSPECTOR, STD_VISIBILITY_CHECKER, null, TypeFactory.defaultInstance(),
null, null, null,
Locale.getDefault(),
TimeZone.getDefault(),
Base64Variants.getDefaultVariant()
);
_serializationConfig = new SerializationConfig(baseSettings, _subtypeResolver, _mixInAnnotations);
_deserializationConfig = new DeserializationConfig(baseSettings, _subtypeResolver, _mixInAnnotations);
configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true);
// 序列化枚举时的处理
SimpleModule serializerModule = new SimpleModule("enumSerializer");
serializerModule.addSerializer(Enum.class, new EnumSerializer());
registerModule(serializerModule);
// 反序列化枚举时的处理
SimpleModule deserializerModule = new SimpleModule("enumDeserializer");
deserializerModule.addDeserializer(Enum.class, new EnumDeserializer());
registerModule(deserializerModule);
//日期的处理
//默认是将日期类型转换为yyyy-MM-dd HH:mm
//如果需要自定义的,则在pojo对象的Date类型上加上注解
//@com.fasterxml.jackson.annotation.JsonFormat(pattern = "时间格式化")
setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
示例8: setUp
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
@Before
public void setUp() {
resolver = new ImmutableEventTypeResolver();
tf = TypeFactory.defaultInstance();
resolver.init(tf.constructType(OrderEvent.class));
}
示例9: convertToJavaType
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
private static <T> JavaType convertToJavaType(ParameterizeTypeReference<T> typeReference) {
TypeFactory typeFactory = TypeFactory.defaultInstance();
return typeFactory.constructType(typeReference.getType());
}
示例10: redisCacheManager
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
@Bean
public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheManager cacheManager = new RedisCacheManager(redisConnectionFactory);
TypeFactory typeFactory = TypeFactory.defaultInstance();
// Post Cache
JsonRedisTemplate<? extends Object> postTemplate = new JsonRedisTemplate<>(redisConnectionFactory, PostService.CACHE_TYPE);
cacheManager.withCache(PostService.CACHE_NAME, postTemplate, cacheTimeToLive);
// Post List Cache
JavaType posts = typeFactory.constructParametricType(ArrayList.class, PostService.CACHE_TYPE);
JsonRedisTemplate<? extends Object> postsTemplate = new JsonRedisTemplate<>(redisConnectionFactory, posts);
cacheManager.withCache(PostService.CACHES_NAME, postsTemplate, cacheTimeToLive);
JsonRedisTemplate<? extends Object> realtimePosts = new JsonRedisTemplate<>(redisConnectionFactory, posts);
cacheManager.withCache(PostService.CACHES_REALTIME_NAME, realtimePosts, cacheTimeToLiveShort);
// Tag Cache
JsonRedisTemplate<? extends Object> tagTemplate = new JsonRedisTemplate<>(redisConnectionFactory, TagService.CACHE_TYPE);
cacheManager.withCache(TagService.CACHE_NAME, tagTemplate, cacheTimeToLive);
// Tag List Cache
JavaType tags = typeFactory.constructParametricType(ArrayList.class, TagService.CACHE_TYPE);
JsonRedisTemplate<? extends Object> tagsTemplate = new JsonRedisTemplate<>(redisConnectionFactory, tags);
cacheManager.withCache(TagService.CACHES_NAME, tagsTemplate, cacheTimeToLive);
// Category Cache
JsonRedisTemplate<? extends Object> cateTemplate = new JsonRedisTemplate<>(redisConnectionFactory, CategoryService.CACHE_TYPE);
cacheManager.withCache(CategoryService.CACHE_NAME, cateTemplate, cacheTimeToLive);
// Category List Cache
JavaType cates = typeFactory.constructParametricType(ArrayList.class, CategoryService.CACHE_TYPE);
JsonRedisTemplate<? extends Object> catesTemplate = new JsonRedisTemplate<>(redisConnectionFactory, cates);
cacheManager.withCache(CategoryService.CACHES_NAME, catesTemplate, cacheTimeToLive);
// Reply Cache
JsonRedisTemplate<? extends Object> replyTemplate = new JsonRedisTemplate<>(redisConnectionFactory, ReplyService.CACHE_TYPE);
cacheManager.withCache(ReplyService.CACHE_NAME, replyTemplate, cacheTimeToLive);
// Reply List Cache
JavaType replies = typeFactory.constructParametricType(ArrayList.class, ReplyService.CACHE_TYPE);
JsonRedisTemplate<? extends Object> repliesTemplate = new JsonRedisTemplate<>(redisConnectionFactory, replies);
cacheManager.withCache(ReplyService.CACHES_NAME, repliesTemplate, cacheTimeToLive);
// Permission
JavaType permissions = typeFactory.constructCollectionType(ArrayList.class, PermissionService.CACHE_TYPE);
JsonRedisTemplate<Object> permissionsTemplate = new JsonRedisTemplate<>(redisConnectionFactory, permissions);
cacheManager.withCache(PermissionService.CACHES_NAME, permissionsTemplate, cacheTimeToLive);
JsonRedisTemplate<? extends Object> permissionTemplate = new JsonRedisTemplate<>(redisConnectionFactory, PermissionService.CACHE_TYPE);
cacheManager.withCache(PermissionService.CACHE_NAME, permissionTemplate, cacheTimeToLive);
// Role
JsonRedisTemplate<? extends Object> roleTemplte = new JsonRedisTemplate<>(redisConnectionFactory, RoleService.CACHE_TYPE);
cacheManager.withCache(RoleService.CACHE_NAME, roleTemplte, cacheTimeToLive);
//Oauth
JsonRedisTemplate<String> oauthTemplte = new JsonRedisTemplate<>(redisConnectionFactory, String.class);
cacheManager.withCache(OAuth2Service.CACHE_CODE, oauthTemplte, OAuth2Service.cacheTimeToLiveCode);
JsonRedisTemplate<String> tokenTemplte = new JsonRedisTemplate<>(redisConnectionFactory, String.class);
cacheManager.withCache(OAuth2Service.CACHE_ACCESSTOKEN, tokenTemplte, OAuth2Service.cacheTimeToLiveToken);
return cacheManager;
}