Search in sources :

Example 1 with PersistentMap

use of com.oath.cyclops.types.persistent.PersistentMap in project cyclops by aol.

the class PersistentMapDeserializer method deserialize.

@Override
public PersistentMap<?, ?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonDeserializer deser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructRawMapType(Map.class));
    Object o = deser.deserialize(p, ctxt);
    if (HashMap.class.isAssignableFrom(mapType))
        return HashMap.fromMap((Map) o);
    if (TreeMap.class.isAssignableFrom(mapType))
        return TreeMap.fromMap((Comparator) Comparator.naturalOrder(), (Map) o);
    /**
     * if(TrieMap.class.isAssignableFrom(mapType))
     *      return TrieMap.fromMap((Map)o);
     *    if(LinkedMap.class.isAssignableFrom(mapType))
     *      return LinkedMap.fromMap((Map)o);
     */
    Optional<Method> m = streamMethod.computeIfAbsent(mapType, c -> Stream.of(c.getMethods()).filter(method -> "fromMap".equals(method.getName())).filter(method -> method.getParameterCount() == 1).filter(method -> method.getParameterTypes()[0].isAssignableFrom(Map.class)).findFirst().map(m2 -> {
        m2.setAccessible(true);
        return m2;
    }));
    PersistentMap<?, ?> x = m.map(mt -> (PersistentMap) new Invoker().executeMethod(o, mt, mapType)).orElse(null);
    return x;
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) JsonParser(com.fasterxml.jackson.core.JsonParser) HashMap(cyclops.data.HashMap) MethodHandles(java.lang.invoke.MethodHandles) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeMap(cyclops.data.TreeMap) Stream(java.util.stream.Stream) CallSite(java.lang.invoke.CallSite) Map(java.util.Map) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) Optional(java.util.Optional) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Comparator(java.util.Comparator) Method(java.lang.reflect.Method) ConstantCallSite(java.lang.invoke.ConstantCallSite) PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) Method(java.lang.reflect.Method) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) HashMap(cyclops.data.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(cyclops.data.TreeMap) Map(java.util.Map) Comparator(java.util.Comparator)

Example 2 with PersistentMap

use of com.oath.cyclops.types.persistent.PersistentMap in project cyclops by aol.

the class PersistentMapSerializer method serialize.

@Override
public void serialize(PersistentMap<?, ?> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (value.iterator().hasNext()) {
        Tuple2<?, ?> keyAndValue = value.iterator().next();
        MapType type = TypeFactory.defaultInstance().constructMapType(Map.class, keyAndValue._1().getClass(), keyAndValue._2().getClass());
        serializers.findTypedValueSerializer(type, true, null).serialize(value.mapView(), gen, serializers);
    } else {
        serializers.findValueSerializer(Map.class).serialize(new HashMap<>(), gen, serializers);
    }
}
Also used : PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) HashMap(java.util.HashMap) Map(java.util.Map) MapType(com.fasterxml.jackson.databind.type.MapType)

Aggregations

PersistentMap (com.oath.cyclops.types.persistent.PersistentMap)2 Map (java.util.Map)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)1 MapType (com.fasterxml.jackson.databind.type.MapType)1 ExceptionSoftener (com.oath.cyclops.util.ExceptionSoftener)1 HashMap (cyclops.data.HashMap)1 TreeMap (cyclops.data.TreeMap)1 IOException (java.io.IOException)1 CallSite (java.lang.invoke.CallSite)1 ConstantCallSite (java.lang.invoke.ConstantCallSite)1 MethodHandles (java.lang.invoke.MethodHandles)1 Method (java.lang.reflect.Method)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1