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;
}
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);
}
}
Aggregations