<commit_before>package org.openforis.ceo.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class JsonUtils {

    private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");

	public static String expandResourcePath(String filename) {
        try {
            var uri = JsonUtils.class.getResource(filename).toURI();
            return Paths.get(uri).toFile().getAbsolutePath();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public static JsonElement parseJson(String jsonString) {
        return (new JsonParser()).parse(jsonString == null || jsonString.equals("null") ? "[]" : jsonString);
    }
    
    public static String toJson(Object obj) {
    	return (new Gson()).toJson(obj);
    }

    public static synchronized JsonElement readJsonFile(String filename) {
        var jsonDataDir = expandResourcePath("/json/");
        try (var fileReader = new FileReader(new File(jsonDataDir, filename))) {
            return (new JsonParser()).parse(fileReader);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static JsonArray elementToArray(JsonElement json) {
        return json.isJsonArray() ? json.getAsJsonArray(): new JsonArray();
    }

    public static JsonObject elementToObject(JsonElement json) {
        return json.isJsonObject() ? json.getAsJsonObject(): new JsonObject();
    }

    public static synchronized void writeJsonFile(String filename, JsonElement data) {
        var jsonDataDir = expandResourcePath("/json/");
        try (var fileWriter = new FileWriter(new File(jsonDataDir, filename))) {
            fileWriter.write(data.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Note: The JSON array may contain elements of any type.
    public static Stream<JsonElement> toElementStream(JsonArray array) {
        return StreamSupport.stream(array.spliterator(), false);
    }

    // Note: The JSON array must only contain JSON objects.
    public static Stream<JsonObject> toStream(JsonArray array) {
        return toElementStream(array).map(element -> element.getAsJsonObject());
    }

    public static Collector<JsonElement, ?, JsonArray> intoJsonArray =
        Collector.of(JsonArray::new, JsonArray::add,<commit_msg>Fixed spacing in ternary operators in JsonUtils<commit_after>package org.openforis.ceo.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class JsonUtils {

    private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");

	public static String expandResourcePath(String filename) {
        try {
            var uri = JsonUtils.class.getResource(filename).toURI();
            return Paths.get(uri).toFile().getAbsolutePath();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public static JsonElement parseJson(String jsonString) {
        return (new JsonParser()).parse(jsonString == null || jsonString.equals("null") ? "[]" : jsonString);
    }
    
    public static String toJson(Object obj) {
    	return (new Gson()).toJson(obj);
    }

    public static synchronized JsonElement readJsonFile(String filename) {
        var jsonDataDir = expandResourcePath("/json/");
        try (var fileReader = new FileReader(new File(jsonDataDir, filename))) {
            return (new JsonParser()).parse(fileReader);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static JsonArray elementToArray(JsonElement json) {
        return json.isJsonArray() ? json.getAsJsonArray() : new JsonArray();
    }

    public static JsonObject elementToObject(JsonElement json) {
        return json.isJsonObject() ? json.getAsJsonObject() : new JsonObject();
    }

    public static synchronized void writeJsonFile(String filename, JsonElement data) {
        var jsonDataDir = expandResourcePath("/json/");
        try (var fileWriter = new FileWriter(new File(jsonDataDir, filename))) {
            fileWriter.write(data.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Note: The JSON array may contain elements of any type.
    public static Stream<JsonElement> toElementStream(JsonArray array) {
        return StreamSupport.stream(array.spliterator(), false);
    }

    // Note: The JSON array must only contain JSON objects.
    public static Stream<JsonObject> toStream(JsonArray array) {
        return toElementStream(array).map(element -> element.getAsJsonObject());
    }

    public static Collector<JsonElement, ?, JsonArray> intoJsonArray =
        Collector.of(JsonArray::new, JsonArray::add,
{'fraction_non_alphanumeric': 0.10625814863102999, 'fraction_numerical': 0.0, 'mean_word_length': 3.5025678650036682, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '411001', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1690, 'n_words': 417}