diff --git a/pom.xml b/pom.xml index 0940f09..4435ab0 100644 --- a/pom.xml +++ b/pom.xml @@ -117,7 +117,8 @@ fr.javatic.mongo mongo-jackson-codec 3.2.0__0.4 - + de.undercouch diff --git a/src/main/java/fr/javatic/mongo/jacksonCodec/Entity.java b/src/main/java/fr/javatic/mongo/jacksonCodec/Entity.java new file mode 100644 index 0000000..6177553 --- /dev/null +++ b/src/main/java/fr/javatic/mongo/jacksonCodec/Entity.java @@ -0,0 +1,7 @@ +package fr.javatic.mongo.jacksonCodec; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface Entity {} \ No newline at end of file diff --git a/src/main/java/fr/javatic/mongo/jacksonCodec/JacksonCodecProvider.java b/src/main/java/fr/javatic/mongo/jacksonCodec/JacksonCodecProvider.java new file mode 100644 index 0000000..43686aa --- /dev/null +++ b/src/main/java/fr/javatic/mongo/jacksonCodec/JacksonCodecProvider.java @@ -0,0 +1,25 @@ +package fr.javatic.mongo.jacksonCodec; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.bson.codecs.Codec; +import org.bson.codecs.configuration.CodecProvider; +import org.bson.codecs.configuration.CodecRegistry; + +public class JacksonCodecProvider implements CodecProvider { + + private final ObjectMapper bsonObjectMapper; + + public JacksonCodecProvider(final ObjectMapper bsonObjectMapper) { + this.bsonObjectMapper = bsonObjectMapper; + } + + @Override + public Codec get(final Class type, final CodecRegistry registry) { + if (type.getAnnotationsByType(Entity.class).length > 0) { + return new JacksonCodec<>(bsonObjectMapper, registry, type); + } else { + return null; + } + } + +} \ No newline at end of file diff --git a/src/main/java/fr/javatic/mongo/jacksonCodec/objectId/Id.java b/src/main/java/fr/javatic/mongo/jacksonCodec/objectId/Id.java new file mode 100644 index 0000000..90bfb95 --- /dev/null +++ b/src/main/java/fr/javatic/mongo/jacksonCodec/objectId/Id.java @@ -0,0 +1,16 @@ +package fr.javatic.mongo.jacksonCodec.objectId; + +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@JsonProperty("_id") +@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) +@JsonDeserialize +public @interface Id {} \ No newline at end of file diff --git a/src/main/java/fr/javatic/mongo/jacksonCodec/package-info.java b/src/main/java/fr/javatic/mongo/jacksonCodec/package-info.java new file mode 100644 index 0000000..4230c40 --- /dev/null +++ b/src/main/java/fr/javatic/mongo/jacksonCodec/package-info.java @@ -0,0 +1,15 @@ +/** + * Uses maven-shade-plugin to override classes in fr.javatic.mongo:mongo-jackson-codec + * + * Similar to what's done with org.apache.commons:commons-pool2 in qLib, we modify and create + * classes in this codebase in order to override fr.javatic.mongo.jacksonCodec classes in the final jar. + * + * These changes add an @Entity annotation and fix serializers in the @Id annotation. + * Without these changes the mongodb driver fails to deserialize documents. Additionally, an @Entity + * annotation allows us to selectively include types, instead of mapping ALL types. + * + * The files present here were copied from: + * https://github.com/FrozenOrb/mongo-jackson-codec/commit/b3a168e5f5f7464c3721fd9ebc81a4cca7d4db2f (entire common) + * https://github.com/FrozenOrb/mongo-jackson-codec/commit/20c161337900bb7daabd3d9d99ead1be35c0c41b (non-build files only) + */ +package fr.javatic.mongo.jacksonCodec; \ No newline at end of file