<commit_before>package com.coshx.drekkar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @class DataSerializer
 * @brief
 */
class DataSerializer {
    private static Map toMap(JSONObject object) throws JSONException {
        Map dictionary = new HashMap();
        Iterator<String> iterator = object.keys();

        while (iterator.hasNext()) {
            String key = iterator.next();
            Object value = object.get(key);
            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            }
            dictionary.put(key, value);
        }

        return dictionary;
    }

    private static List toList(JSONArray array) throws JSONException {
        List list = new ArrayList();
        for (int i = 0, s = array.length(); i < s; i++) {
            Object o = array.get(i);
            if (o instanceof JSONObject) {
                o = toMap((JSONObject) o);
            }
            list.add(o);
        }
        return list;
    }

    static String serialize(Object data) {
        String outcome;

        if (data instanceof Boolean) {
            outcome = ((Boolean) data) ? "true" : "false";
        } else if (data instanceof Integer || data instanceof Float || data instanceof Double) {
            outcome = data.toString();
        } else if (data instanceof String) {
            // As this string is going to be unwrapped from quotes, when passed to JS, all quotes need to be escaped
            outcome = data.toString();
            outcome = outcome.replaceAll("\"", "\\\"");
            outcome = outcome.replaceAll("'", "\'");
        } else if (data instanceof List) {
            // Array and Dictionary are serialized to JSON.
            // They should wrap only "basic" data (same types than supported ones)
            outcome = new JSONArray((List) data).toString();
        } else if (data instanceof Map) {
            outcome = new JSONObject((Map) data).toString();
        } else {
            outcome = null;
        }

        return outcome;
    }

    static Object deserialize(String input) {
        if (input.length() > 0) {
            if (input.length() >= 2) {
                char first = input.charAt(0), last = input.charAt(input.length() - 1);

                if (first == '[' && last == ']') {
                    try {
                        return toList(new JSONArray(input));
                    } catch (Exception e) {
                        return null;<commit_msg>Fixes quote escaping<commit_after>package com.coshx.drekkar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @class DataSerializer
 * @brief
 */
class DataSerializer {
    private static Map toMap(JSONObject object) throws JSONException {
        Map dictionary = new HashMap();
        Iterator<String> iterator = object.keys();

        while (iterator.hasNext()) {
            String key = iterator.next();
            Object value = object.get(key);
            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            }
            dictionary.put(key, value);
        }

        return dictionary;
    }

    private static List toList(JSONArray array) throws JSONException {
        List list = new ArrayList();
        for (int i = 0, s = array.length(); i < s; i++) {
            Object o = array.get(i);
            if (o instanceof JSONObject) {
                o = toMap((JSONObject) o);
            }
            list.add(o);
        }
        return list;
    }

    static String serialize(Object data) {
        String outcome;

        if (data instanceof Boolean) {
            outcome = ((Boolean) data) ? "true" : "false";
        } else if (data instanceof Integer || data instanceof Float || data instanceof Double) {
            outcome = data.toString();
        } else if (data instanceof String) {
            // As this string is going to be unwrapped from quotes, when passed to JS, all quotes need to be escaped
            outcome = data.toString();
            outcome = outcome.replaceAll("\"", "\\\\\\\"");
            outcome = outcome.replaceAll("'", "\\\\\\'");
            outcome = "\"" + outcome + "\"";
        } else if (data instanceof List) {
            // Array and Dictionary are serialized to JSON.
            // They should wrap only "basic" data (same types than supported ones)
            outcome = new JSONArray((List) data).toString();
        } else if (data instanceof Map) {
            outcome = new JSONObject((Map) data).toString();
        } else {
            outcome = null;
        }

        return outcome;
    }

    static Object deserialize(String input) {
        if (input.length() > 0) {
            if (input.length() >= 2) {
                char first = input.charAt(0), last = input.charAt(input.length() - 1);

                if (first == '[' && last == ']') {
                    try {
                        return toList(new JSONArray(input));
                    } catch (Exception e) {
                        return null;
{'fraction_non_alphanumeric': 0.1173953488372093, 'fraction_numerical': 0.0018604651162790699, 'mean_word_length': 1.8043818466353678, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1561952', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1377, 'n_words': 437}