Search in sources :

Example 1 with IterableX

use of com.oath.cyclops.types.traversable.IterableX in project cyclops by aol.

the class IterableXDeserializer method deserialize.

@Override
public IterableX<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    if (!p.isExpectedStartArrayToken()) {
        return (IterableX) ctxt.handleUnexpectedToken(handledType(), p);
    }
    List multi = new ArrayList();
    JsonToken t;
    while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
        Object value;
        if (t == JsonToken.VALUE_NULL) {
            value = null;
        } else if (typeDeserializerForValue == null) {
            value = valueDeserializer.deserialize(p, ctxt);
        } else {
            value = valueDeserializer.deserializeWithType(p, ctxt, typeDeserializerForValue);
        }
        multi.add(value);
    }
    if (Vector.class.isAssignableFrom(elementType))
        return Vector.fromIterable(multi);
    if (Seq.class.isAssignableFrom(elementType))
        return Seq.fromIterable(multi);
    if (LazySeq.class.isAssignableFrom(elementType))
        return LazySeq.fromIterable(multi);
    if (LazyString.class.isAssignableFrom(elementType))
        return LazyString.fromLazySeq((LazySeq) LazySeq.fromIterable(multi));
    if (IntMap.class.isAssignableFrom(elementType))
        return IntMap.fromIterable(multi);
    if (ReactiveSeq.class.isAssignableFrom(elementType))
        return ReactiveSeq.fromIterable(multi);
    if (Streamable.class.isAssignableFrom(elementType))
        return Streamable.fromIterable(multi);
    if (BankersQueue.class.isAssignableFrom(elementType))
        return BankersQueue.fromIterable(multi);
    if (Bag.class.isAssignableFrom(elementType))
        return Bag.fromIterable(multi);
    if (cyclops.data.HashSet.class.isAssignableFrom(elementType))
        return HashSet.fromIterable(multi);
    if (cyclops.data.TrieSet.class.isAssignableFrom(elementType))
        return TrieSet.fromIterable(multi);
    if (cyclops.data.TreeSet.class.isAssignableFrom(elementType))
        return TreeSet.fromIterable(multi, (Comparator) Comparator.naturalOrder());
    Optional<Method> m = streamMethod.computeIfAbsent(elementType, c -> Stream.of(c.getMethods()).filter(method -> "fromIterable".equals(method.getName())).filter(method -> method.getParameterCount() == 1).findFirst().map(m2 -> {
        m2.setAccessible(true);
        return m2;
    }));
    IterableX x = m.map(mt -> (IterableX) new Invoker().executeMethod(multi, mt, itX)).orElse(null);
    return x;
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) ArrayList(java.util.ArrayList) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) Map(java.util.Map) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Method(java.lang.reflect.Method) ConstantCallSite(java.lang.invoke.ConstantCallSite) CollectionLikeType(com.fasterxml.jackson.databind.type.CollectionLikeType) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) ContextualDeserializer(com.fasterxml.jackson.databind.deser.ContextualDeserializer) JsonParser(com.fasterxml.jackson.core.JsonParser) AsArrayTypeDeserializer(com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer) cyclops.data(cyclops.data) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) MethodHandles(java.lang.invoke.MethodHandles) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Stream(java.util.stream.Stream) CallSite(java.lang.invoke.CallSite) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) Optional(java.util.Optional) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectArrayDeserializer(com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer) Comparator(java.util.Comparator) Streamable(cyclops.companion.Streamable) cyclops.data(cyclops.data) IterableX(com.oath.cyclops.types.traversable.IterableX) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JsonToken(com.fasterxml.jackson.core.JsonToken) Method(java.lang.reflect.Method) Comparator(java.util.Comparator)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ContextualDeserializer (com.fasterxml.jackson.databind.deser.ContextualDeserializer)1 ObjectArrayDeserializer (com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer)1 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)1 TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)1 AsArrayTypeDeserializer (com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer)1 CollectionLikeType (com.fasterxml.jackson.databind.type.CollectionLikeType)1 IterableX (com.oath.cyclops.types.traversable.IterableX)1 ExceptionSoftener (com.oath.cyclops.util.ExceptionSoftener)1 Streamable (cyclops.companion.Streamable)1 cyclops.data (cyclops.data)1 ReactiveSeq (cyclops.reactive.ReactiveSeq)1 IOException (java.io.IOException)1 CallSite (java.lang.invoke.CallSite)1