<commit_before>/**
 * Copyright (C) 2015 Red Hat, 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 io.fabric8.kubernetes.client.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.client.utils.serialization.UnmatchedFieldTypeModule;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public class Serialization {
  private Serialization() { }

  public static final UnmatchedFieldTypeModule UNMATCHED_FIELD_TYPE_MODULE = new UnmatchedFieldTypeModule();

  private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
  static {
    JSON_MAPPER.registerModules(new JavaTimeModule(), UNMATCHED_FIELD_TYPE_MODULE);
  }

  private static final ObjectMapper YAML_MAPPER = new ObjectMapper(
    new YAMLFactory().disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID)
  );
  static {
    YAML_MAPPER.registerModules(UNMATCHED_FIELD_TYPE_MODULE);
  }

  private static final String DOCUMENT_DELIMITER = "---";

  /**
   * {@link ObjectMapper} singleton instance used internally by the Kubernetes client.
   *
   * <p> The ObjectMapper has an {@link UnmatchedFieldTypeModule} module registered. This module allows the client
   * to work with Resources that contain properties that don't match the target field type. This is especially useful
   * and necessary to work with OpenShift Templates.
   *
   * <p> n.b. the use of this module gives precedence to properties present in the additionalProperties Map present
   * in most KubernetesResource instances. If a property is both defined in the Map and in the original field, the
   * one from the additionalProperties Map will be serialized.
   */
  public static ObjectMapper jsonMapper() {
    return JSON_MAPPER;
  }

  /**
   * {@link ObjectMapper} singleton instance used internally by the Kubernetes client.
   *
   * <p> The ObjectMapper has an {@link UnmatchedFieldTypeModule} module registered. This module allows the client
   * to work with Resources that contain properties that don't match the target field type. This is especially useful
   * and necessary to work with OpenShift Templates.
   *
   * <p> n.b. the use of this module gives precedence to properties present in the additionalProperties Map present
   * in most KubernetesResource instances. If a property is both defined in the Map and in the original field, the
   * one from the additionalProperties Map will be serialized.
   */
  public static ObjectMapper yamlMapper() {
    return YAML_MAPPER;
  }

  /**
   * Returns a JSON representation of the given object.
   *
   * <p> If the provided object contains a JsonAnyGetter annotated method with a Map that contains an entry that
   * overrides a field of the provided object, the Map entry will take precedence upon serialization. Properties won't
   * be duplicated.
   *
   * @param object the object to serialize.
   * @param <T> the type of the object being serialized.
   * @return a String containing a JSON representation of the provided object.
   */
  public static <T> String asJson(T object) {
    try {
      return JSON_MAPPER.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }

  /**
   * Returns a YAML representation of the given object.
   *
   * <p> If the provided object contains a JsonAnyGetter annotated method with a Map that contains an entry that
   * overrides a field of the provided object, the Map entry will take precedence upon serialization. Properties won't
   * be duplicated.
   *
   * @param object the object to serialize.
   * @param <T> the type of the object being serialized.
   * @return a String containing a JSON representation of the provided object.
   */
  public static <T> String asYaml(T object) {
    try {
      return YAML_MAPPER.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      throw KubernetesClientException.launderThrowable(e);
    }<commit_msg>Allow applications to clear out YAML ObjectMapper<commit_after>/**
 * Copyright (C) 2015 Red Hat, 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 io.fabric8.kubernetes.client.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.client.utils.serialization.UnmatchedFieldTypeModule;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public class Serialization {
  private Serialization() { }

  public static final UnmatchedFieldTypeModule UNMATCHED_FIELD_TYPE_MODULE = new UnmatchedFieldTypeModule();

  private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
  static {
    JSON_MAPPER.registerModules(new JavaTimeModule(), UNMATCHED_FIELD_TYPE_MODULE);
  }

  private static volatile ObjectMapper YAML_MAPPER;

  private static final String DOCUMENT_DELIMITER = "---";

  /**
   * {@link ObjectMapper} singleton instance used internally by the Kubernetes client.
   *
   * <p> The ObjectMapper has an {@link UnmatchedFieldTypeModule} module registered. This module allows the client
   * to work with Resources that contain properties that don't match the target field type. This is especially useful
   * and necessary to work with OpenShift Templates.
   *
   * <p> n.b. the use of this module gives precedence to properties present in the additionalProperties Map present
   * in most KubernetesResource instances. If a property is both defined in the Map and in the original field, the
   * one from the additionalProperties Map will be serialized.
   */
  public static ObjectMapper jsonMapper() {
    return JSON_MAPPER;
  }

  /**
   * {@link ObjectMapper} singleton instance used internally by the Kubernetes client.
   *
   * <p> The ObjectMapper has an {@link UnmatchedFieldTypeModule} module registered. This module allows the client
   * to work with Resources that contain properties that don't match the target field type. This is especially useful
   * and necessary to work with OpenShift Templates.
   *
   * <p> n.b. the use of this module gives precedence to properties present in the additionalProperties Map present
   * in most KubernetesResource instances. If a property is both defined in the Map and in the original field, the
   * one from the additionalProperties Map will be serialized.
   */
  public static ObjectMapper yamlMapper() {
    if (YAML_MAPPER == null) {
      synchronized (Serialization.class) {
        if (YAML_MAPPER == null) {
          YAML_MAPPER = new ObjectMapper(
            new YAMLFactory().disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID)
          );
          YAML_MAPPER.registerModules(UNMATCHED_FIELD_TYPE_MODULE);
        }
      }
    }
    return YAML_MAPPER;
  }

  /**
   * Allow application to clear out the YAML mapper in order to allow its garbage collection.
   * This is useful because in a lot of cases the YAML mapper is only need at application startup
   * when the client is created, so there is no reason to keep the very heavy (in terms of memory) mapper
   * around indefinitely.
   */
  public static void clearYamlMapper() {
    YAML_MAPPER = null;
  }

  /**
   * Returns a JSON representation of the given object.
   *
   * <p> If the provided object contains a JsonAnyGetter annotated method with a Map that contains an entry that
   * overrides a field of the provided object, the Map entry will take precedence upon serialization. Properties won't
   * be duplicated.
   *
   * @param object the object to serialize.
   * @param <T> the type of the object being serialized.
   * @return a String containing a JSON representation of the provided object.
   */
  public static <T> String asJson(T object) {
    try {
      return JSON_MAPPER.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }

  /**
   * Returns a YAML representation of the given object.
   *
   * <p> If the provided object contains a JsonAnyGetter annotated method with a Map that contains an entry that
   * overrides a field of the provided object, the Map entry will take precedence upon serialization. Properties won't
   * be duplicated.
   *
   * @param object the object to serialize.
   * @param <T> the type of the object being serialized.
   * @return a String containing a JSON representation of the provided object.
   */
  public static <T> String asYaml(T object) {
    try {
      return yamlMapper().writeValueAsString(object);
    } catch (JsonProcessingException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
{'fraction_non_alphanumeric': 0.07119797574382689, 'fraction_numerical': 0.0027920774801500742, 'mean_word_length': 4.018388791593695, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, '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': '987395', 'n_tokens_mistral': 3156, 'n_tokens_neox': 2981, 'n_words': 1167}