<commit_before>package com.bananity.util.serialization;

import java.util.Collection;
import java.util.Map;

/**
 * This class encapsulates static methods to serialize objects to JSON representations 
 *
 * @author 	<NAME>
 * @see 	com.bananity.util.serialization.IJsonSerializable
 */
public class JsonSerializer {

	/**
	 * Generic method to serialize "any" object to a JSON string
	 *
	 * @param 	jsonObject 	Object to be serialized
	 * @return 				JSON string
	 */
	public static String ObjectToJsonString (Object jsonObject) {
		// TODO : Add support to
		// - Character, char[], boolean[], int[], long[], float[], double[]

		if (jsonObject == null) {
			return "null";
		} else if (jsonObject instanceof Boolean) {
			return ((Boolean)jsonObject).toString();
		} else if (jsonObject instanceof Integer) {
			return ((Integer)jsonObject).toString();
		} else if (jsonObject instanceof Long) {
			return ((Long)jsonObject).toString();
		} else if (jsonObject instanceof Float) {
			return ((Float)jsonObject).toString();
		} else if (jsonObject instanceof Double) {
			return ((Double)jsonObject).toString();
		} else if (jsonObject instanceof String) {
			return StringToJsonString((String)jsonObject);
		} else if (jsonObject instanceof Collection) {
			return CollectionToJsonString((Collection)jsonObject);
		} else if (jsonObject instanceof Object[]) {
			return ArrayToJsonString((Object[])jsonObject);
		} else if (jsonObject instanceof Map) {
			return MapToJsonString((Map)jsonObject);
		} else if (jsonObject instanceof IJsonSerializable) {
			return ((IJsonSerializable)jsonObject).toJsonStr();
		} else {
			return ""; // TODO : throw an Exception
		}
	}

	/**
	 * @return 	Escaped and quoted valid JSON string value
	 */
	public static String StringToJsonString (String str) {
		return new StringBuilder()
			.append("\"")
			.append(
				str
					.replace("\"", "\\\"")
					.replace("\n", "\\n")
					.replace("\t", "\\t")
					.replace("\r", "\\r")
			)
			.append("\"")
			.toString();
	}

	/**<commit_msg>Added sequence scapes in JsonSerializer<commit_after>package com.bananity.util.serialization;

import java.util.Collection;
import java.util.Map;

/**
 * This class encapsulates static methods to serialize objects to JSON representations 
 *
 * @author 	<NAME>
 * @see 	com.bananity.util.serialization.IJsonSerializable
 */
public class JsonSerializer {

	/**
	 * Generic method to serialize "any" object to a JSON string
	 *
	 * @param 	jsonObject 	Object to be serialized
	 * @return 				JSON string
	 */
	public static String ObjectToJsonString (Object jsonObject) {
		// TODO : Add support to
		// - Character, char[], boolean[], int[], long[], float[], double[]

		if (jsonObject == null) {
			return "null";
		} else if (jsonObject instanceof Boolean) {
			return ((Boolean)jsonObject).toString();
		} else if (jsonObject instanceof Integer) {
			return ((Integer)jsonObject).toString();
		} else if (jsonObject instanceof Long) {
			return ((Long)jsonObject).toString();
		} else if (jsonObject instanceof Float) {
			return ((Float)jsonObject).toString();
		} else if (jsonObject instanceof Double) {
			return ((Double)jsonObject).toString();
		} else if (jsonObject instanceof String) {
			return StringToJsonString((String)jsonObject);
		} else if (jsonObject instanceof Collection) {
			return CollectionToJsonString((Collection)jsonObject);
		} else if (jsonObject instanceof Object[]) {
			return ArrayToJsonString((Object[])jsonObject);
		} else if (jsonObject instanceof Map) {
			return MapToJsonString((Map)jsonObject);
		} else if (jsonObject instanceof IJsonSerializable) {
			return ((IJsonSerializable)jsonObject).toJsonStr();
		} else {
			return ""; // TODO : throw an Exception
		}
	}

	/**
	 * @return 	Escaped and quoted valid JSON string value
	 */
	public static String StringToJsonString (String str) {
		return new StringBuilder()
			.append("\"")
			.append(
				str
					.replace("\"", "\\\"")
					.replace("\b", "\\b")
					.replace("\f", "\\f")
					.replace("\n", "\\n")
					.replace("\r", "\\r")
					.replace("\t", "\\t")
			)
			.append("\"")
			.toString();
	}

	/**
{'fraction_non_alphanumeric': 0.14313346228239845, 'fraction_numerical': 0.0, 'mean_word_length': 3.695800227014756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2404221', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1190, 'n_words': 348}