<commit_before>package org.batfish.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;

public final class BatfishObjectMapper {
  private static final ObjectMapper MAPPER = baseMapper();

  private static final ObjectWriter WRITER = MAPPER.writer();

  private static final PrettyPrinter PRETTY_PRINTER = new PrettyPrinter();

  private static final ObjectWriter PRETTY_WRITER =
      baseMapper().enable(SerializationFeature.INDENT_OUTPUT).writer(PRETTY_PRINTER);

  private static final ObjectWriter VERBOSE_WRITER =
      baseMapper()
          .enable(SerializationFeature.INDENT_OUTPUT)
          .setSerializationInclusion(Include.ALWAYS)
          .writer(PRETTY_PRINTER);

  /**
   * Returns a {@link ObjectMapper} configured to Batfish JSON standards. The JSON produced is not
   * pretty-printed; see {@link #prettyWriter} for that.
   */
  public static ObjectMapper mapper() {
    return MAPPER;
  }

  /** Uses Jackson to clone the given object using the given type. */
  public static <T> T clone(Object o, Class<T> clazz) throws IOException {
    return MAPPER.readValue(WRITER.writeValueAsBytes(o), clazz);
  }

  /**
   * Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is not
   * pretty-printed; see {@link #prettyWriter} for that.
   */
  @SuppressWarnings("unused") // Seems like it should be used some day.
  public static ObjectWriter writer() {
    return WRITER;
  }

  /**
   * Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is not
   * pretty-printed; for a more concise encoding use {@link #writer}.
   */
  public static ObjectWriter prettyWriter() {
    return PRETTY_WRITER;
  }

  /**
   * Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is
   * verbosely pretty-printed; all fields are included.
   *
   * @see BatfishObjectMapper#writer()
   * @see BatfishObjectMapper#prettyWriter()
   */
  public static ObjectWriter verboseWriter() {
    return VERBOSE_WRITER;<commit_msg>BatfishObjectMapper: add #clone(TypeReference) (#1077)<commit_after>package org.batfish.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;

public final class BatfishObjectMapper {
  private static final ObjectMapper MAPPER = baseMapper();

  private static final ObjectWriter WRITER = MAPPER.writer();

  private static final PrettyPrinter PRETTY_PRINTER = new PrettyPrinter();

  private static final ObjectWriter PRETTY_WRITER =
      baseMapper().enable(SerializationFeature.INDENT_OUTPUT).writer(PRETTY_PRINTER);

  private static final ObjectWriter VERBOSE_WRITER =
      baseMapper()
          .enable(SerializationFeature.INDENT_OUTPUT)
          .setSerializationInclusion(Include.ALWAYS)
          .writer(PRETTY_PRINTER);

  /**
   * Returns a {@link ObjectMapper} configured to Batfish JSON standards. The JSON produced is not
   * pretty-printed; see {@link #prettyWriter} for that.
   */
  public static ObjectMapper mapper() {
    return MAPPER;
  }

  /** Uses Jackson to clone the given object using the given type. */
  public static <T> T clone(Object o, Class<T> clazz) throws IOException {
    return MAPPER.readValue(WRITER.writeValueAsBytes(o), clazz);
  }

  /** Uses Jackson to clone the given object using the given type. */
  public static <T> T clone(Object o, TypeReference<T> type) throws IOException {
    return MAPPER.readValue(WRITER.writeValueAsBytes(o), type);
  }

  /**
   * Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is not
   * pretty-printed; see {@link #prettyWriter} for that.
   */
  @SuppressWarnings("unused") // Seems like it should be used some day.
  public static ObjectWriter writer() {
    return WRITER;
  }

  /**
   * Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is not
   * pretty-printed; for a more concise encoding use {@link #writer}.
   */
  public static ObjectWriter prettyWriter() {
    return PRETTY_WRITER;
  }

  /**
   * Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is
   * verbosely pretty-printed; all fields are included.
   *
   * @see BatfishObjectMapper#writer()
   * @see BatfishObjectMapper#prettyWriter()
   */
  public static ObjectWriter verboseWriter() {
    return VERBOSE_WRITER;
{'fraction_non_alphanumeric': 0.09208683473389356, 'fraction_numerical': 0.0024509803921568627, 'mean_word_length': 4.4254510921177586, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 25}, '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': '5401066', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1572, 'n_words': 446}