<commit_before>/*
 * This file is provided to you 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.basho.riak.client.convert;

import static com.basho.riak.client.convert.KeyUtil.getKey;
import static com.basho.riak.client.util.CharsetUtils.asString;
import static com.basho.riak.client.util.CharsetUtils.getCharset;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.RiakLink;
import com.basho.riak.client.builders.RiakObjectBuilder;
import com.basho.riak.client.cap.VClock;
import com.basho.riak.client.http.util.Constants;
import com.basho.riak.client.query.indexes.RiakIndexes;

/**
 * Converts a RiakObject's value to an instance of T. T must have a field
 * annotated with {@link RiakKey} or you must construct the converter with a key to use. RiakObject's value *must* be a JSON string.
 * 
 * <p>
 * At present user meta data and {@link RiakLink}s are not converted. This means
 * they are essentially lost in translation.
 * </p>
 * 
 * @author russell
 * 
 */
public class JSONConverter<T> implements Converter<T> {

    private final ObjectMapper objectMapper = new ObjectMapper();
    private final Class<T> clazz;
    private final String bucket;
    private final UsermetaConverter<T> usermetaConverter;
    private final RiakIndexConverter<T> riakIndexConverter;
    private final RiakLinksConverter<T> riakLinksConverter;
    private String defaultKey;

    /**
     * Create a JSONConverter for creating instances of <code>clazz</code> from
     * JSON and instances of {@link IRiakObject} with a JSON paylaod from
     * instances of <code>clazz</code>
     * 
     * @param clazz the type to convert to/from
     * @param b the bucket
     */
    public JSONConverter(Class<T> clazz, String bucket) {
        this(clazz, bucket, null);
    }

    /**
     * Create a JSONConverter for creating instances of <code>clazz</code> from
     * JSON and instances of {@link IRiakObject} with a JSON paylaod from
     * instances of <code>clazz</code>
     * 
     * @param clazz the type to convert to/from
     * @param bucket the bucket
     * @param defaultKey
     *            for cases where <code>clazz</code> does not have a
     *            {@link RiakKey} annotated field, pass the key to use in this
     *            conversion.
     */
    public JSONConverter(Class<T> clazz, String bucket, String defaultKey) {
        this.clazz = clazz;
        this.bucket = bucket;
        this.defaultKey = defaultKey;
        this.usermetaConverter = new UsermetaConverter<T>();
        this.riakIndexConverter = new RiakIndexConverter<T>();
        this.riakLinksConverter = new RiakLinksConverter<T>();
        objectMapper.registerModule(new RiakJacksonModule());
    }

    /**
     * Converts <code>domainObject</code> to a JSON string and sets that as the
     * payload of a {@link IRiakObject}. Also set the <code>content-type</code>
     * to <code>application/json;charset=UTF-8</code>
     * 
     * @param domainObject
     *            to be converted
     * @param vclock
     *            the vector clock from Riak
     */<commit_msg>Minor spelling fix to comments.<commit_after>/*
 * This file is provided to you 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.basho.riak.client.convert;

import static com.basho.riak.client.convert.KeyUtil.getKey;
import static com.basho.riak.client.util.CharsetUtils.asString;
import static com.basho.riak.client.util.CharsetUtils.getCharset;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.RiakLink;
import com.basho.riak.client.builders.RiakObjectBuilder;
import com.basho.riak.client.cap.VClock;
import com.basho.riak.client.http.util.Constants;
import com.basho.riak.client.query.indexes.RiakIndexes;

/**
 * Converts a RiakObject's value to an instance of T. T must have a field
 * annotated with {@link RiakKey} or you must construct the converter with a key to use. RiakObject's value *must* be a JSON string.
 * 
 * <p>
 * At present user meta data and {@link RiakLink}s are not converted. This means
 * they are essentially lost in translation.
 * </p>
 * 
 * @author russell
 * 
 */
public class JSONConverter<T> implements Converter<T> {

    private final ObjectMapper objectMapper = new ObjectMapper();
    private final Class<T> clazz;
    private final String bucket;
    private final UsermetaConverter<T> usermetaConverter;
    private final RiakIndexConverter<T> riakIndexConverter;
    private final RiakLinksConverter<T> riakLinksConverter;
    private String defaultKey;

    /**
     * Create a JSONConverter for creating instances of <code>clazz</code> from
     * JSON and instances of {@link IRiakObject} with a JSON payload from
     * instances of <code>clazz</code>
     * 
     * @param clazz the type to convert to/from
     * @param b the bucket
     */
    public JSONConverter(Class<T> clazz, String bucket) {
        this(clazz, bucket, null);
    }

    /**
     * Create a JSONConverter for creating instances of <code>clazz</code> from
     * JSON and instances of {@link IRiakObject} with a JSON payload from
     * instances of <code>clazz</code>
     * 
     * @param clazz the type to convert to/from
     * @param bucket the bucket
     * @param defaultKey
     *            for cases where <code>clazz</code> does not have a
     *            {@link RiakKey} annotated field, pass the key to use in this
     *            conversion.
     */
    public JSONConverter(Class<T> clazz, String bucket, String defaultKey) {
        this.clazz = clazz;
        this.bucket = bucket;
        this.defaultKey = defaultKey;
        this.usermetaConverter = new UsermetaConverter<T>();
        this.riakIndexConverter = new RiakIndexConverter<T>();
        this.riakLinksConverter = new RiakLinksConverter<T>();
        objectMapper.registerModule(new RiakJacksonModule());
    }

    /**
     * Converts <code>domainObject</code> to a JSON string and sets that as the
     * payload of a {@link IRiakObject}. Also set the <code>content-type</code>
     * to <code>application/json;charset=UTF-8</code>
     * 
     * @param domainObject
     *            to be converted
     * @param vclock
     *            the vector clock from Riak
     */
{'fraction_non_alphanumeric': 0.09128137384412154, 'fraction_numerical': 0.001321003963011889, 'mean_word_length': 3.210789766407119, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '980263', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2169, 'n_words': 736}