kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | fab1098d2b7cb8807f9c6d2aae5310a9f9d931aa8fbeb5475b6b34058e0bc036 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-07-27 15:08 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-27 10:52 -07:00 |
| Parents | 17c4f72ffc15 |
1 file changed
modified driver-extras/src/main/java/com/datastax/driver/extras/codecs/json/JacksonJsonCodec.java +40 −2
| @@ -25,6 +25,7 @@ | ||
| 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,10 +51,47 @@ | ||
| 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 |