/*
 *      Copyright (C) 2012-2015 DataStax Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.datastax.driver.extras.codecs.json;

import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ParseUtils;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import com.datastax.driver.core.utils.Bytes;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * A JSON codec that uses the <a href="http://wiki.fasterxml.com/JacksonHome">Jackson</a>
 * library to perform serialization and deserialization of JSON objects.
 * <p/>
 * This codec maps a single Java object to a single JSON structure at a time;
 * mapping of arrays or collections to root-level JSON arrays is not supported,
 * but such a codec can be easily crafted after this one.
 * <p/>
 * Note that this codec requires the presence of Jackson library at runtime.
 * If you use Maven, this can be done by declaring the following dependency in your project:
 * <p/>
 * <pre>{@code
 * <dependency>
 *   <groupId>com.fasterxml.jackson.core</groupId>
 *   <artifactId>jackson-databind</artifactId>
 *   <version>2.6.3</version>
 * </dependency>
 * }</pre>
 */
public class JacksonJsonCodec<T> extends TypeCodec<T> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    public JacksonJsonCodec(Class<T> javaType) {
        super(DataType.varchar(), javaType);
    }

    @Override
    public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException {
        if (value == null)
            return null;
        try {
            return ByteBuffer.wrap(objectMapper.writeValueAsBytes(value));
        } catch (JsonProcessingException e) {
            throw new InvalidTypeException(e.getMessage(), e);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
        if (bytes == null)
            return null;
        try {
            return (T) objectMapper.readValue(Bytes.getArray(bytes), toJacksonJavaType());
        } catch (IOException e) {
            throw new InvalidTypeException(e.getMessage(), e);
        }
    }

    @Override
    public String format(T value) throws InvalidTypeException {
        if (value == null)
            return "NULL";
        String json;
        try {
            json = objectMapper.writeValueAsString(value);
        } catch (IOException e) {
            throw new InvalidTypeException(e.getMessage(), e);
        }
        return ParseUtils.quote(json);
    }

    @Override
    @SuppressWarnings("unchecked")
    public T parse(String value) throws InvalidTypeException {
        if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
            return null;
        if (!ParseUtils.isQuoted(value))
            throw new InvalidTypeException("JSON strings must be enclosed by single quotes");
        String json = ParseUtils.unquote(value);
        try {
            return (T) objectMapper.readValue(json, toJacksonJavaType());
        } catch (IOException e) {
            throw new InvalidTypeException(e.getMessage(), e);
        }
    }

    /**
     * This method acts as a bridge between Guava's {@link com.google.common.reflect.TypeToken TypeToken} API, which is used
     * by the driver, and Jackson's {@link JavaType} API.
     *
     * @return A {@link JavaType} instance corresponding to the codec's {@link #getJavaType() Java type}.
     */
    protected JavaType toJacksonJavaType() {
        return TypeFactory.defaultInstance().constructType(getJavaType().getType());
    }

}

@@ -1172,16 +1172,60 @@
 Factory;
+%0Aimport com.google.common.reflect.TypeToken;
 %0A%0Aimport
@@ -2143,18 +2143,302 @@
 ctMapper
- =
+;%0A%0A    /**%0A     * Creates a new instance for the provided %7B@code javaClass%7D,%0A     * using a default, newly-allocated %7B@link ObjectMapper%7D.%0A     *%0A     * @param javaClass the Java class this codec maps to.%0A     */%0A    public JacksonJsonCodec(Class%3CT%3E javaClass) %7B%0A        this(javaClass,
  new Obj
@@ -2452,56 +2452,939 @@
 er()
+)
 ;%0A
-%0A    public JacksonJsonCodec(Class%3CT%3E javaType
+    %7D%0A%0A    /**%0A     * Creates a new instance for the provided %7B@code javaType%7D,%0A     * using a default, newly-allocated %7B@link ObjectMapper%7D.%0A     *%0A     * @param javaType the Java type this codec maps to.%0A     */%0A    public JacksonJsonCodec(TypeToken%3CT%3E javaType) %7B%0A        this(javaType, new ObjectMapper());%0A    %7D%0A%0A    /**%0A     * Creates a new instance for the provided %7B@code javaClass%7D,%0A     * and using the provided %7B@link ObjectMapper%7D.%0A     *%0A     * @param javaClass the Java class this codec maps to.%0A     */%0A    public JacksonJsonCodec(Class%3CT%3E javaClass, ObjectMapper objectMapper) %7B%0A        this(TypeToken.of(javaClass), objectMapper);%0A    %7D%0A%0A    /**%0A     * Creates a new instance for the provided %7B@code javaType%7D,%0A     * and using the provided %7B@link ObjectMapper%7D.%0A     *%0A     * @param javaType the Java type this codec maps to.%0A     */%0A    public JacksonJsonCodec(TypeToken%3CT%3E javaType, ObjectMapper objectMapper
 ) %7B%0A
@@ -3424,24 +3424,66 @@
  javaType);%0A
+        this.objectMapper = objectMapper;%0A
     %7D%0A%0A    @

Provide alternate constructors for JacksonJsonCodec