com.elepy.mongo.ElepyCodecRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elepy-mongo Show documentation
Show all versions of elepy-mongo Show documentation
A Restful API generator and Headless CMS
package com.elepy.mongo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DBObject;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.mongojack.JacksonCodecRegistry;
import org.mongojack.internal.stream.JacksonCodec;
import org.mongojack.internal.stream.JacksonDecoder;
import org.mongojack.internal.stream.JacksonEncoder;
import org.mongojack.internal.util.DocumentSerializationUtils;
import java.util.concurrent.ConcurrentHashMap;
public class ElepyCodecRegistry extends JacksonCodecRegistry {
private final ObjectMapper objectMapper;
private final Class> view;
private ConcurrentHashMap, Codec>> codecCache = new ConcurrentHashMap<>();
public ElepyCodecRegistry(ObjectMapper objectMapper, Class> view) {
super(objectMapper, view);
this.objectMapper = objectMapper;
this.view = view;
}
public Codec get(Class clazz, CodecRegistry registry) {
return this.addCodecForClass(clazz);
// return !DocumentSerializationUtils.isKnownClass(clazz) && !DBObject.class.isAssignableFrom(clazz) && !Document.class.isAssignableFrom(clazz) && !Bson.class.isAssignableFrom(clazz) ? this.addCodecForClass(clazz) : null;
}
@SuppressWarnings("unchecked")
@Override
public Codec addCodecForClass(Class clazz) {
return (Codec) this.codecCache.computeIfAbsent(clazz, (k) -> {
JacksonEncoder encoder = new JacksonEncoder(clazz, this.view, this.objectMapper);
JacksonDecoder decoder = new JacksonDecoder(clazz, this.view, this.objectMapper);
return new ElepyCodec<>(encoder, decoder);
});
}
}