|
25 | 25 | import com.fasterxml.jackson.databind.JavaType; |
26 | 26 | import com.fasterxml.jackson.databind.ObjectMapper; |
27 | 27 | import com.fasterxml.jackson.databind.type.TypeFactory; |
| 28 | +import com.google.common.reflect.TypeToken; |
28 | 29 |
|
29 | 30 | import java.io.IOException; |
30 | 31 | import java.nio.ByteBuffer; |
|
50 | 51 | */ |
51 | 52 | public class JacksonJsonCodec<T> extends TypeCodec<T> { |
52 | 53 |
|
53 | | - private final ObjectMapper objectMapper = new ObjectMapper(); |
| 54 | + private final ObjectMapper objectMapper; |
54 | 55 |
|
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) { |
56 | 93 | super(DataType.varchar(), javaType); |
| 94 | + this.objectMapper = objectMapper; |
57 | 95 | } |
58 | 96 |
|
59 | 97 | @Override |
|
0 commit comments