Elffuss BraidHub

kikocis/java-driver public

Provide alternate constructors for JacksonJsonCodec
Changesetfab1098d2b7cb8807f9c6d2aae5310a9f9d931aa8fbeb5475b6b34058e0bc036
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-07-27 15:08 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-07-27 10:52 -07:00
Parents17c4f72ffc15

Browse the files at this changeset

1 file changed

modified driver-extras/src/main/java/com/datastax/driver/extras/codecs/json/JacksonJsonCodec.java +40 −2
@@ -25,6 +25,7 @@
2525 import com.fasterxml.jackson.databind.JavaType;
2626 import com.fasterxml.jackson.databind.ObjectMapper;
2727 import com.fasterxml.jackson.databind.type.TypeFactory;
28+import com.google.common.reflect.TypeToken;
2829
2930 import java.io.IOException;
3031 import java.nio.ByteBuffer;
@@ -50,10 +51,47 @@
5051 */
5152 public class JacksonJsonCodec<T> extends TypeCodec<T> {
5253
53- private final ObjectMapper objectMapper = new ObjectMapper();
54+ private final ObjectMapper objectMapper;
5455
55- public JacksonJsonCodec(Class<T> javaType) {
56+ /**
57+ * Creates a new instance for the provided {@code javaClass},
58+ * using a default, newly-allocated {@link ObjectMapper}.
59+ *
60+ * @param javaClass the Java class this codec maps to.
61+ */
62+ public JacksonJsonCodec(Class<T> javaClass) {
63+ this(javaClass, new ObjectMapper());
64+ }
65+
66+ /**
67+ * Creates a new instance for the provided {@code javaType},
68+ * using a default, newly-allocated {@link ObjectMapper}.
69+ *
70+ * @param javaType the Java type this codec maps to.
71+ */
72+ public JacksonJsonCodec(TypeToken<T> javaType) {
73+ this(javaType, new ObjectMapper());
74+ }
75+
76+ /**
77+ * Creates a new instance for the provided {@code javaClass},
78+ * and using the provided {@link ObjectMapper}.
79+ *
80+ * @param javaClass the Java class this codec maps to.
81+ */
82+ public JacksonJsonCodec(Class<T> javaClass, ObjectMapper objectMapper) {
83+ this(TypeToken.of(javaClass), objectMapper);
84+ }
85+
86+ /**
87+ * Creates a new instance for the provided {@code javaType},
88+ * and using the provided {@link ObjectMapper}.
89+ *
90+ * @param javaType the Java type this codec maps to.
91+ */
92+ public JacksonJsonCodec(TypeToken<T> javaType, ObjectMapper objectMapper) {
5693 super(DataType.varchar(), javaType);
94+ this.objectMapper = objectMapper;
5795 }
5896
5997 @Override