Skip to content

Commit 33617e1

Browse files
Alexandre Dutraolim7t
authored andcommitted
Provide alternate constructors for JacksonJsonCodec
1 parent 0b125e0 commit 33617e1

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

driver-extras/src/main/java/com/datastax/driver/extras/codecs/json/JacksonJsonCodec.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)