Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 7 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
View
4 pom.xml
@@ -9,7 +9,7 @@
<groupId>net.vz.mongodb.jackson</groupId>
<artifactId>mongo-jackson-mapper</artifactId>
- <version>1.4.1-SNAPSHOT</version>
+ <version>1.4.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MongoDB Jackson POJO Mapper</name>
@@ -30,7 +30,7 @@
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.2</version>
+ <version>1.9.5</version>
</dependency>
<dependency>
<groupId>de.undercouch</groupId>
View
61 src/main/java/net/vz/mongodb/jackson/JacksonDBCollection.java
@@ -15,44 +15,29 @@
*/
package net.vz.mongodb.jackson;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
+import com.mongodb.*;
import net.vz.mongodb.jackson.internal.FetchableDBRef;
+import net.vz.mongodb.jackson.internal.stream.JacksonEncoderFactory;
+import net.vz.mongodb.jackson.internal.util.IdHandler;
+import net.vz.mongodb.jackson.internal.util.IdHandlerFactory;
import net.vz.mongodb.jackson.internal.JacksonCollectionKey;
-import net.vz.mongodb.jackson.internal.MongoAnnotationIntrospector;
-import net.vz.mongodb.jackson.internal.MongoJacksonHandlerInstantiator;
import net.vz.mongodb.jackson.internal.MongoJacksonMapperModule;
import net.vz.mongodb.jackson.internal.object.BsonObjectGenerator;
import net.vz.mongodb.jackson.internal.object.BsonObjectTraversingParser;
import net.vz.mongodb.jackson.internal.stream.JacksonDBObject;
import net.vz.mongodb.jackson.internal.stream.JacksonDecoderFactory;
-import net.vz.mongodb.jackson.internal.stream.JacksonEncoderFactory;
-import net.vz.mongodb.jackson.internal.util.IdHandler;
-import net.vz.mongodb.jackson.internal.util.IdHandlerFactory;
import net.vz.mongodb.jackson.internal.util.SerializationUtils;
-
-import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
-import com.mongodb.BasicDBObject;
-import com.mongodb.CommandResult;
-import com.mongodb.DB;
-import com.mongodb.DBCollection;
-import com.mongodb.DBObject;
-import com.mongodb.GroupCommand;
-import com.mongodb.MapReduceCommand;
-import com.mongodb.MongoException;
-import com.mongodb.QueryBuilder;
-import com.mongodb.ReadPreference;
-import com.mongodb.WriteConcern;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* A DBCollection that marshals/demarshals objects to/from Jackson annotated classes. It provides a very thin wrapper
@@ -94,15 +79,7 @@ public boolean isEnabledByDefault() {
}
}
- private static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper();
-
- static {
- // Configure to use the object id annotation introspector
- DEFAULT_OBJECT_MAPPER.registerModule(MongoJacksonMapperModule.INSTANCE);
- DEFAULT_OBJECT_MAPPER.setHandlerInstantiator(new MongoJacksonHandlerInstantiator(
- new MongoAnnotationIntrospector(DEFAULT_OBJECT_MAPPER.getDeserializationConfig())));
- DEFAULT_OBJECT_MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- }
+ private static final ObjectMapper DEFAULT_OBJECT_MAPPER = MongoJacksonMapperModule.configure(new ObjectMapper());
private final DBCollection dbCollection;
private final JavaType type;
@@ -174,20 +151,24 @@ protected JacksonDBCollection(DBCollection dbCollection, JavaType type, JavaType
*/
public static <T, K> JacksonDBCollection<T, K> wrap(DBCollection dbCollection, Class<T> type, Class<K> keyType, Class<?> view) {
ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.withModule(MongoJacksonMapperModule.INSTANCE);
objectMapper.setSerializationConfig(objectMapper.getSerializationConfig().withView(view));
- objectMapper.setHandlerInstantiator(new MongoJacksonHandlerInstantiator(
- new MongoAnnotationIntrospector(objectMapper.getDeserializationConfig())));
+ MongoJacksonMapperModule.configure(objectMapper);
return new JacksonDBCollection<T, K>(dbCollection, DEFAULT_OBJECT_MAPPER.constructType(type),
DEFAULT_OBJECT_MAPPER.constructType(keyType), objectMapper, null);
}
/**
- * Wraps a DB collection in a JacksonDBCollection, using the given object mapper
+ * Wraps a DB collection in a JacksonDBCollection, using the given object mapper.
+ *
+ * JacksonDBCollection requires a specially configured object mapper to work. It does not automatically configure
+ * the object mapper passed into this method, because the same object mapper might be passed into multiple calls to
+ * this method. Consequently, it is up to the caller to ensure that the object mapper has been configured for use
+ * by JacksonDBCollection. This can be done by passing the object mapper to
+ * {@link MongoJacksonMapperModule#configure(org.codehaus.jackson.map.ObjectMapper)}.
*
* @param dbCollection The DB collection to wrap
* @param type The type of objects to deserialise to
- * @param objectMapper The ObjectMapper
+ * @param objectMapper The ObjectMapper to configure.
* @return The wrapped collection
*/
public static <T, K> JacksonDBCollection<T, K> wrap(DBCollection dbCollection, Class<T> type, Class<K> keyType, ObjectMapper objectMapper) {
View
9 src/main/java/net/vz/mongodb/jackson/internal/MongoJacksonDeserializers.java
@@ -16,22 +16,13 @@
package net.vz.mongodb.jackson.internal;
import net.vz.mongodb.jackson.DBRef;
-import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.BeanDescription;
import org.codehaus.jackson.map.BeanProperty;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.DeserializerProvider;
-import org.codehaus.jackson.map.Deserializers;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonMappingException;
-import org.codehaus.jackson.map.KeyDeserializer;
-import org.codehaus.jackson.map.TypeDeserializer;
import org.codehaus.jackson.map.module.SimpleDeserializers;
-import org.codehaus.jackson.map.type.ArrayType;
-import org.codehaus.jackson.map.type.CollectionLikeType;
-import org.codehaus.jackson.map.type.CollectionType;
-import org.codehaus.jackson.map.type.MapLikeType;
-import org.codehaus.jackson.map.type.MapType;
import org.codehaus.jackson.type.JavaType;
import java.util.Calendar;
View
16 src/main/java/net/vz/mongodb/jackson/internal/MongoJacksonMapperModule.java
@@ -18,6 +18,7 @@
import net.vz.mongodb.jackson.internal.stream.ServerErrorProblemHandler;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.Module;
+import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
@@ -29,6 +30,21 @@
public class MongoJacksonMapperModule extends Module {
public static final Module INSTANCE = new MongoJacksonMapperModule();
+ /**
+ * Configure the given object mapper to be used with the Mongo Jackson Mapper. Please call this method rather than
+ * calling objectMapper.with(MongoJacksonMapperModule.INSTANCE), because Jacksons module system doesn't allow the
+ * mongo jackson mapper to do all the configuration it needs to do. This method will do that configuration though.
+ *
+ * @param objectMapper The object mapper to configure
+ * @return This object mapper (for chaining)
+ */
+ public static ObjectMapper configure(ObjectMapper objectMapper) {
+ objectMapper.registerModule(INSTANCE);
+ objectMapper.setHandlerInstantiator(new MongoJacksonHandlerInstantiator(
+ new MongoAnnotationIntrospector(objectMapper.getDeserializationConfig())));
+ return objectMapper;
+ }
+
@Override
public String getModuleName() {
return "Object ID Module";
View
6 src/site/markdown/changelog.md
@@ -1,6 +1,12 @@
Changelog
=========
+1.4.1
+-----
+
+* Added convenience method for configuring a custom object mapper
+* Documented the necessary use of custom mongo configuration when supplying a custom object mapper
+
1.4.0
-----
View
4 src/site/markdown/tutorial.md
@@ -122,10 +122,10 @@ If you're using your data objects for both storage and web views, you might want
JacksonDBCollection<BlogPost, String> coll = JacksonDBCollection.wrap(DBCollection dbCollection, BlogPost.class,
String.class, DatabaseView.class);
-Of course, if you really want to control things and Jackson's annotations aren't enough, the wrap method is also overloaded to accept an `ObjectMapper`. For convenience, you should add the object ID module in order to get the object id and id annotation mapping features:
+Of course, if you really want to control things and Jackson's annotations aren't enough, the wrap method is also overloaded to accept an `ObjectMapper`. When doing this, you must make sure to configure the object mapper to use the mongo custom jackson configuration:
ObjectMapper myObjectMapper = ...
- myObjectMapper.withModule(net.vz.mongodb.jackson.internal.ObjectIdModule.INSTANCE);
+ MongoJacksonMapperModule.configure(myObjectMapper);
JacksonDBCollection<BlogPost, String> coll = JacksonDBCollection.wrap(DBCollection dbCollection, BlogPost.class,
String.class, myObjectMapper);

No commit comments for this range

Something went wrong with that request. Please try again.