本文整理汇总了Java中com.fasterxml.jackson.databind.type.TypeFactory.constructCollectionType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeFactory.constructCollectionType方法的具体用法?Java TypeFactory.constructCollectionType怎么用?Java TypeFactory.constructCollectionType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.databind.type.TypeFactory
的用法示例。
在下文中一共展示了TypeFactory.constructCollectionType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deserializeJsonStr
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
/**
* Helper method for deserializing a chat JSON response to a collection of objects.
*
* @param jsonStr
* The chat JSON response.
* @param mapElement
* Chat JSON responses are actually maps with a single element. This argument is
* the value of the element to pull out from the map.
* @param colClassElements
* The types of objects that the collection object will contain.
* @param objMapper
* The JSON object mapper used to deserialize the JSON string.
* @return A collection of elements of type <code>colClassElements</code>.
*/
private <T> Collection<T> deserializeJsonStr(String jsonStr, String mapElement,
Class<T> colClassElements,
ObjectMapper objMapper) {
Map<String, Collection<T>> re;
try {
TypeFactory typeFactory = objMapper.getTypeFactory();
CollectionType type = typeFactory.constructCollectionType(List.class, colClassElements);
MapType thetype = typeFactory.constructMapType(HashMap.class,
typeFactory.constructType(String.class),
type);
re = objMapper.readValue(jsonStr, thetype);
} catch (IOException e) {
LOG.error("Got exception when trying to deserialize list of {}", colClassElements, e);
return Lists.newArrayListWithExpectedSize(0);
}
return re.get(mapElement);
}
示例2: getChangedContainers
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
private Map<String, ContainerQuota> getChangedContainers( final String quotaContainers ) throws java.io.IOException
{
Map<String, ContainerQuota> changedContainersFiltered = new HashMap<>();
TypeFactory typeFactory = mapper.getTypeFactory();
CollectionType arrayType = typeFactory.constructCollectionType( ArrayList.class, ChangedContainerDto.class );
List<ChangedContainerDto> changedContainers = mapper.readValue( quotaContainers, arrayType );
for ( ChangedContainerDto cont : changedContainers )
{
ContainerQuotaDto containerQuotaDto = cont.getQuota();
ContainerSize containerSize = containerQuotaDto.getContainerSize();
ContainerQuota defaultQuota = ContainerSize.getDefaultContainerQuota( containerSize );
if ( containerSize == ContainerSize.CUSTOM )
{
defaultQuota = containerQuotaDto.getContainerQuota();
}
changedContainersFiltered.put( cont.getHostId(), defaultQuota );
}
return changedContainersFiltered;
}
示例3: deserializeJsonStr
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
/**
* Helper method for deserializing lists of elements of type <code>T</code> from a response JSON
* string
*
* @param jsonStr
* The API returned JSON string
* @param listElements
* A list of ordered elements in the original response JSON to traverse down to to
* get to the element that contains the list of items of type <code>T</code> to
* deserialize
* <p/>
* For example given the following JSON string
*
* <pre>
* {
* "someElement": {
* "someInnerElement": [
* {...},
* {...}
* ],
* "otherElement": "value"
* }
* }
* </pre>
*
* If the collection of <code>T</code>s we were interested in were inside
* <code>someInnerEllement</code> then the list would equal:
* <code>[someElement, someInnterElement]</code>
* @param clazz
* The class to deserialize The JSON object mapper used to deserialize the JSON
* string.
* @return A collection of elements of type <code>clazz</code>.
*/
private <T> Collection<T> deserializeJsonStr(String jsonStr, List<String> listElements,
Class<T> clazz,
ObjectMapper objMapper) {
TypeFactory typeFactory = objMapper.getTypeFactory();
CollectionType type = typeFactory.constructCollectionType(List.class, clazz);
try {
JsonNode jsonNode = objMapper.readTree(jsonStr);
// traverse down the order of elements until the desired one is reached
for (String jsonEl : listElements) {
jsonNode = jsonNode.get(jsonEl);
if (jsonNode == null) {
return ImmutableList.of();
}
}
return objMapper.readValue(jsonNode.toString(), type);
} catch (IOException e) {
LOG.error("Got exception when trying to deserialize list of {}", clazz, e);
return ImmutableList.of();
}
}
示例4: parseNodes
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
private List<NodeSchemaDto> parseNodes( final String nodes ) throws java.io.IOException
{
TypeFactory typeFactory = mapper.getTypeFactory();
CollectionType arrayType = typeFactory.constructCollectionType( ArrayList.class, NodeSchemaDto.class );
return mapper.readValue( nodes, arrayType );
}
示例5: serialize
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
@Override
public void serialize(List<Link> value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
// sort links according to their relation
Map<String, List<Object>> sortedLinks = new LinkedHashMap<String, List<Object>>();
List<Link> links = new ArrayList<Link>();
boolean prefixingRequired = curieProvider != null;
boolean curiedLinkPresent = false;
boolean skipCuries = !jgen.getOutputContext().getParent().inRoot();
Object currentValue = jgen.getCurrentValue();
if (currentValue instanceof Resources) {
if (mapper.hasCuriedEmbed((Resources<?>) currentValue)) {
curiedLinkPresent = true;
}
}
for (Link link : value) {
if (link.equals(CURIES_REQUIRED_DUE_TO_EMBEDS)) {
continue;
}
String rel = prefixingRequired ? curieProvider.getNamespacedRelFrom(link) : link.getRel();
if (!link.getRel().equals(rel)) {
curiedLinkPresent = true;
}
if (sortedLinks.get(rel) == null) {
sortedLinks.put(rel, new ArrayList<Object>());
}
links.add(link);
sortedLinks.get(rel).add(toHalLink(link));
}
if (!skipCuries && prefixingRequired && curiedLinkPresent) {
ArrayList<Object> curies = new ArrayList<Object>();
curies.add(curieProvider.getCurieInformation(new Links(links)));
sortedLinks.put("curies", curies);
}
TypeFactory typeFactory = provider.getConfig().getTypeFactory();
JavaType keyType = typeFactory.uncheckedSimpleType(String.class);
JavaType valueType = typeFactory.constructCollectionType(ArrayList.class, Object.class);
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);
MapSerializer serializer = MapSerializer.construct(new String[]{}, mapType, true, null,
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property), null);
serializer.serialize(sortedLinks, jgen, provider);
}
示例6: 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;
}
示例7: decodeToList
import com.fasterxml.jackson.databind.type.TypeFactory; //导入方法依赖的package包/类
/***
* 将传入的json,转换为T类型的List集合
* @param json json
* @param t t
* @param <T> T
* @return result
* @throws IOException error
*/
public static <T> List<T> decodeToList(String json, Class<T> t) throws IOException {
final TypeFactory factory = mapper.getTypeFactory();
final JavaType listOfT = factory.constructCollectionType(List.class, t);
return mapper.readValue(json, listOfT);
}