下面列出了怎么用com.fasterxml.jackson.databind.type.CollectionLikeType的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config,
CollectionLikeType collectionType, BeanDescription beanDesc,
TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) {
Class<?> raw = collectionType.getRawClass();
if (raw == CharSeq.class) {
return new CharSeqSerializer(collectionType);
}
if (Seq.class.isAssignableFrom(raw)) {
return new ArraySerializer<>(collectionType);
}
if (Set.class.isAssignableFrom(raw)) {
return new ArraySerializer<>(collectionType);
}
if (PriorityQueue.class.isAssignableFrom(raw)) {
return new ArraySerializer<>(collectionType);
}
return super.findCollectionLikeSerializer(config, collectionType, beanDesc, elementTypeSerializer, elementValueSerializer);
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType collectionType,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
Class<?> raw = collectionType.getRawClass();
if (raw == CharSeq.class) {
return new CharSeqDeserializer(collectionType);
}
if (Seq.class.isAssignableFrom(raw)) {
return new SeqDeserializer(collectionType, collectionType.getContentType(), elementTypeDeserializer,
elementDeserializer, settings.deserializeNullAsEmptyCollection());
}
if (Set.class.isAssignableFrom(raw)) {
return new SetDeserializer(collectionType, collectionType.getContentType(), elementTypeDeserializer,
elementDeserializer, settings.deserializeNullAsEmptyCollection());
}
if (PriorityQueue.class.isAssignableFrom(raw)) {
return new PriorityQueueDeserializer(collectionType, collectionType.getContentType(),
elementTypeDeserializer, elementDeserializer, settings.deserializeNullAsEmptyCollection());
}
return super.findCollectionLikeDeserializer(collectionType, config, beanDesc, elementTypeDeserializer, elementDeserializer);
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type, DeserializationConfig config,
BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer) throws JsonMappingException {
for (Deserializers deserializers : deserializersList) {
JsonDeserializer<?> deserializer = deserializers.findCollectionLikeDeserializer(type, config, beanDesc, elementTypeDeserializer, elementDeserializer);
if (deserializer != null) {
return deserializer;
}
}
return null;
}
protected Serialized fromSerializedString(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JacksonReladomoModule());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
CollectionLikeType customClassCollection = mapper.getTypeFactory().constructCollectionLikeType(Serialized.class, Order.class);
return (Serialized)mapper.readValue(json, customClassCollection);
}
@Override
public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config,
CollectionLikeType containerType, BeanDescription beanDesc, JsonFormat.Value formatOverrides,
TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)
{
if (ObjectContainer.class.isAssignableFrom(containerType.getRawClass())) {
// hmmh. not sure if we can find 'forceStaticTyping' anywhere...
boolean staticTyping = config.isEnabled(MapperFeature.USE_STATIC_TYPING);
ObjectArraySerializer ser = new ObjectArraySerializer(containerType.getContentType(),
staticTyping, elementTypeSerializer, elementValueSerializer);
return new ObjectContainerSerializer(containerType, ser);
}
return null;
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type, DeserializationConfig config, BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) throws JsonMappingException {
Class<?> raw = type.getRawClass();
if (IterableX.class.isAssignableFrom(type.getRawClass())) {
return new IterableXDeserializer(raw,type.containedTypeOrUnknown(0).getRawClass(),elementTypeDeserializer,elementDeserializer, type);
}
return super.findCollectionLikeDeserializer(type, config, beanDesc, elementTypeDeserializer, elementDeserializer);
}
public IterableXDeserializer(Class<?> vc, Class<?> elementType,TypeDeserializer typeDeserializerForValue, JsonDeserializer<?> valueDeserializer,CollectionLikeType type) {
super(vc);
this.itX = vc;
this.elementType = elementType;
this.valueDeserializer = valueDeserializer;
this.typeDeserializerForValue = typeDeserializerForValue;
this.type = type;
}
@Override
public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, TypeFactory typeFactory) {
if (type.isReferenceType() || type.isContainerType()) {
return type;
}
final Class<?> raw = type.getRawClass();
if (raw == Option.class)
return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
if (raw == Eval.class)
return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
if (raw == Trampoline.class)
return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
if (raw == Either.class)
return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
if (raw == Value.class)
return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
for(Class c : collectionLikeTypes){
if (c.isAssignableFrom(raw)) {
// return CollectionType.upgradeFrom(type, type.containedTypeOrUnknown(0));
return CollectionLikeType.upgradeFrom(type, type.containedTypeOrUnknown(0));
}
}
return type;
}
@Override
public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config, CollectionLikeType type, BeanDescription beanDesc, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) {
if (!Collection.class.isAssignableFrom(type.getRawClass()) && IterableX.class.isAssignableFrom(type.getRawClass())) {
return new IterableXSerializer();
}
return super.findCollectionLikeSerializer(config, type, beanDesc, elementTypeSerializer, elementValueSerializer);
}
@Override
public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config,
CollectionLikeType type, BeanDescription beanDesc,
TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) {
return findSerializer(config, type, beanDesc);
}
/**
* @since 2.2
*/
public JsonDeserializer<?> modifyCollectionLikeDeserializer(DeserializationConfig config,
CollectionLikeType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
return deserializer;
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type, DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException {
return getDeserializer(type.getRawClass());
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type, DeserializationConfig config, BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) {
return forJavaType(type);
}
ArraySerializer(CollectionLikeType collectionType, BeanProperty property) {
super(collectionType, property);
this.collectionType = collectionType;
}
ArraySerializer(CollectionLikeType collectionType) {
this(collectionType, null);
}
/**
* 封装 map type
*
* @param elementClass 集合值类型
* @return CollectionLikeType
*/
public static CollectionLikeType getListType(Class<?> elementClass) {
return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass);
}
/**
* jackson json path 语法读取节点
*
* @param jsonPtrExpr json path 表达式
* @param valueType value value type
* @return List
*/
default <T> List<T> atJsonPathList(String jsonPtrExpr, Class<T> valueType) {
CollectionLikeType collectionLikeType = JsonUtil.getListType(valueType);
return JsonUtil.convertValue(atJsonPath(jsonPtrExpr), collectionLikeType);
}