當前位置: 首頁>>代碼示例>>Java>>正文


Java TypeFactory.defaultInstance方法代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:SciGaP,項目名稱:allocateme,代碼行數:28,代碼來源:AbstractThriftDeserializer.java

示例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();
}
 
開發者ID:codeabovelab,項目名稱:haven-platform,代碼行數:24,代碼來源:KvMapperFactory.java

示例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;
}
 
開發者ID:GoldRenard,項目名稱:JuniperBotJ,代碼行數:13,代碼來源:JacksonUtil.java

示例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;
}
 
開發者ID:GoldRenard,項目名稱:JuniperBotJ,代碼行數:12,代碼來源:JacksonUtil.java

示例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();
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:34,代碼來源:EnumTypeIdResolver.java

示例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();
}
 
開發者ID:react-dev26,項目名稱:NGB-master,代碼行數:10,代碼來源:AbstractControllerTest.java

示例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);
}
 
開發者ID:lodsve,項目名稱:lodsve-framework,代碼行數:38,代碼來源:CustomObjectMapper.java

示例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));
}
 
開發者ID:goodees,項目名稱:goodees,代碼行數:7,代碼來源:EventTypeIdResolverTest.java

示例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());
}
 
開發者ID:miaoxing-tech,項目名稱:growing-pains,代碼行數:5,代碼來源:JsonUtil.java

示例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;
}
 
開發者ID:microacup,項目名稱:microbbs,代碼行數:66,代碼來源:CacheConfig.java


注:本文中的com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。