<commit_before>package com.snapbundle.util.json;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public final class JsonUtil
{
    private static final Logger LOG = LoggerFactory.getLogger(JsonUtil.class);

    private static final ObjectMapper mapper = new ObjectMapper();

    private JsonUtil()
    {
    }

    static
    {
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

    }

    public static <T> T fromJson(String json, Class<T> entityClass)
    {
        T instance = null;

        try
        {
            instance = mapper.readValue(json, entityClass);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return instance;
    }

    public static <T> T fromJson(String json, Class<T> entityClass, JsonAutoDetect.Visibility visibility)
    {
        T instance = null;

        // Need to make this local to be thread safe
        ObjectMapper localMapper = new ObjectMapper();
        localMapper.setVisibility(PropertyAccessor.FIELD, visibility);

        try
        {
            instance = localMapper.readValue(json, entityClass);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return instance;
    }

    public static String toJson(Object object)
    {
        return toJson(object, JsonGenerationView.Standard.class);
    }

    public static String toJson(Object object, ViewType viewType)
    {
        return JsonUtil.toJson(object, viewType.getViewClass());
    }

    public static String toJson(Object object, Class<? extends JsonGenerationView.Published> viewClass)
    {
        String json = null;

        Preconditions.checkArgument((viewClass != null), "The viewClass must not be null");
        Preconditions.checkArgument((object != null), "The object must not be null");

        try
        {
            json = mapper.writerWithView(viewClass).writeValueAsString(object);
        } catch (JsonProcessingException e)
        {
            if (LOG.isDebugEnabled())
            {
                e.printStackTrace();
            }

            LOG.error("Unable to convert object to JSON: {}", e.getMessage());
        }

        return json;
    }

    public static String toJson(Object object, JsonAutoDetect.Visibility visibility)
    {
        String json = null;

        // Need to make this local to be thread safe
        ObjectMapper localMapper = new ObjectMapper();
        localMapper.setVisibility(PropertyAccessor.FIELD, visibility);

        try
        {
            json = localMapper.writeValueAsString(object);
        } catch (JsonProcessingException e)
        {
            if (LOG.isDebugEnabled())
            {
                e.printStackTrace();
            }
            LOG.error("Unable to convert object to JSON: {}", e.getMessage());
        }
        return json;
    }
}

<commit_msg>Added additional fromJson() helper that takes a JSONObject as an arg instead of a String<commit_after>package com.snapbundle.util.json;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public final class JsonUtil
{
    private static final Logger LOG = LoggerFactory.getLogger(JsonUtil.class);

    private static final ObjectMapper mapper = new ObjectMapper();

    private JsonUtil()
    {
    }

    static
    {
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

    }

    public static <T> T fromJson(JSONObject jsonObject, Class<T> entityClass)
    {
        return JsonUtil.fromJson(jsonObject.toString(), entityClass);
    }

    public static <T> T fromJson(String json, Class<T> entityClass)
    {
        T instance = null;

        try
        {
            instance = mapper.readValue(json, entityClass);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return instance;
    }

    public static <T> T fromJson(String json, Class<T> entityClass, JsonAutoDetect.Visibility visibility)
    {
        T instance = null;

        // Need to make this local to be thread safe
        ObjectMapper localMapper = new ObjectMapper();
        localMapper.setVisibility(PropertyAccessor.FIELD, visibility);

        try
        {
            instance = localMapper.readValue(json, entityClass);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return instance;
    }

    public static String toJson(Object object)
    {
        return toJson(object, JsonGenerationView.Standard.class);
    }

    public static String toJson(Object object, ViewType viewType)
    {
        return JsonUtil.toJson(object, viewType.getViewClass());
    }

    public static String toJson(Object object, Class<? extends JsonGenerationView.Published> viewClass)
    {
        String json = null;

        Preconditions.checkArgument((viewClass != null), "The viewClass must not be null");
        Preconditions.checkArgument((object != null), "The object must not be null");

        try
        {
            json = mapper.writerWithView(viewClass).writeValueAsString(object);
        } catch (JsonProcessingException e)
        {
            if (LOG.isDebugEnabled())
            {
                e.printStackTrace();
            }

            LOG.error("Unable to convert object to JSON: {}", e.getMessage());
        }

        return json;
    }

    public static String toJson(Object object, JsonAutoDetect.Visibility visibility)
    {
        String json = null;

        // Need to make this local to be thread safe
        ObjectMapper localMapper = new ObjectMapper();
        localMapper.setVisibility(PropertyAccessor.FIELD, visibility);

        try
        {
            json = localMapper.writeValueAsString(object);
        } catch (JsonProcessingException e)
        {
            if (LOG.isDebugEnabled())
            {
                e.printStackTrace();
            }
            LOG.error("Unable to convert object to JSON: {}", e.getMessage());
        }
        return json;
    }
}


{'fraction_non_alphanumeric': 0.08977407847800238, 'fraction_numerical': 0.0005945303210463733, 'mean_word_length': 2.3594608087868196, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '2136797', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1701, 'n_words': 443}