<commit_before>package com.cflint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.xml.transform.TransformerException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

public class JSONOutput {

    boolean prettyPrint = true;

    public boolean isPrettyPrint() {
        return prettyPrint;
    }

    public void setPrettyPrint(final boolean prettyPrint) {
        this.prettyPrint = prettyPrint;
    }

    public void output(final BugList bugList, final Writer writer, final boolean showStats, CFLintStats stats) throws IOException {
        final BugCounts counts = stats.getCounts();
        // final StringBuilder sb = new StringBuilder();
        final JsonFactory jsonF = new JsonFactory();
        final JsonGenerator jg = jsonF.createGenerator(writer);
        if (prettyPrint) {
            jg.useDefaultPrettyPrinter();
        }
        jg.writeStartArray();
        List<String> keys = new ArrayList<String>(bugList.getBugList().keySet());
        Collections.sort(keys);
        for (final String key : keys) {
            List<BugInfo> currentList = bugList.getBugList().get(key);
            final Iterator<BugInfo> iterator = currentList.iterator();
            BugInfo bugInfo = iterator.hasNext() ? iterator.next() : null;
            BugInfo prevbugInfo;

            while (bugInfo != null) {
                final String severity = currentList.get(0).getSeverity();
                final String code = currentList.get(0).getMessageCode();

                jg.writeStartObject();
                jg.writeStringField("severity", notNull(severity));
                jg.writeStringField("id", code);
                jg.writeStringField("message", code);
                jg.writeStringField("category", "CFLINT");
                jg.writeStringField("abbrev", abbrev(code));
                jg.writeFieldName("locations");
                jg.writeStartArray();
                do {
                    jg.writeStartObject();
                    jg.writeStringField("file", notNull(bugInfo.getFilename()));
                    jg.writeStringField("fileName", filename(bugInfo.getFilename()));
                    jg.writeStringField("function", filename(bugInfo.getFunction()));
                    jg.writeStringField("column", Integer.valueOf(bugInfo.getColumn()).toString());
                    jg.writeStringField("line", Integer.valueOf(bugInfo.getLine()).toString());
                    jg.writeStringField("message", notNull(bugInfo.getMessage()));
                    jg.writeStringField("variable", notNull(bugInfo.getVariable()));
                    jg.writeStringField("expression", notNull(bugInfo.getExpression()));
                    jg.writeEndObject();
                    prevbugInfo = bugInfo;
                    bugInfo = iterator.hasNext() ? iterator.next() : null;
                } while (isGrouped(prevbugInfo, bugInfo));
                jg.writeEndArray();
                jg.writeEndObject();
            }
        }

        if (showStats) {
            for (final String code : counts.bugTypes()) {
                jg.writeStartObject();
                jg.writeStringField("code", code);
                jg.writeStringField("count", Integer.toString(counts.getCode(code)));
                jg.writeEndObject();
            }
            for (final String severity : BugCounts.levels) {
                if (counts.getSeverity(severity) > 0) {
                    jg.writeStartObject();
                    jg.writeStringField("severity", severity);
                    jg.writeStringField("count", Integer.toString(counts.getSeverity(severity)));
                    jg.writeEndObject();
                }
            }
        }

        jg.writeEndArray();
        jg.close();
        writer.close();
    }

    List<String> CODE_GROUPBY_FUNCTION = Arrays.asList("PARSE_ERROR");

    private boolean isGrouped(final BugInfo prevbugInfo, final BugInfo bugInfo) {
        if (prevbugInfo == null || bugInfo == null) {
            return false;
        }
        // Different message types are not grouped
        if (!safeEquals(prevbugInfo.getMessageCode(), bugInfo.getMessageCode())) {
            return false;
        }<commit_msg>#318 - Changing JSON output for numeric values to not wrap it inside of a string anymore.<commit_after>package com.cflint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.xml.transform.TransformerException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

public class JSONOutput {

    boolean prettyPrint = true;

    public boolean isPrettyPrint() {
        return prettyPrint;
    }

    public void setPrettyPrint(final boolean prettyPrint) {
        this.prettyPrint = prettyPrint;
    }

    public void output(final BugList bugList, final Writer writer, final boolean showStats, CFLintStats stats) throws IOException {
        final BugCounts counts = stats.getCounts();
        // final StringBuilder sb = new StringBuilder();
        final JsonFactory jsonF = new JsonFactory();
        final JsonGenerator jg = jsonF.createGenerator(writer);
        if (prettyPrint) {
            jg.useDefaultPrettyPrinter();
        }
        jg.writeStartArray();
        List<String> keys = new ArrayList<String>(bugList.getBugList().keySet());
        Collections.sort(keys);
        for (final String key : keys) {
            List<BugInfo> currentList = bugList.getBugList().get(key);
            final Iterator<BugInfo> iterator = currentList.iterator();
            BugInfo bugInfo = iterator.hasNext() ? iterator.next() : null;
            BugInfo prevbugInfo;

            while (bugInfo != null) {
                final String severity = currentList.get(0).getSeverity();
                final String code = currentList.get(0).getMessageCode();

                jg.writeStartObject();
                jg.writeStringField("severity", notNull(severity));
                jg.writeStringField("id", code);
                jg.writeStringField("message", code);
                jg.writeStringField("category", "CFLINT");
                jg.writeStringField("abbrev", abbrev(code));
                jg.writeFieldName("locations");
                jg.writeStartArray();
                do {
                    jg.writeStartObject();
                    jg.writeStringField("file", notNull(bugInfo.getFilename()));
                    jg.writeStringField("fileName", filename(bugInfo.getFilename()));
                    jg.writeStringField("function", filename(bugInfo.getFunction()));
                    jg.writeNumberField("column", bugInfo.getColumn());
                    jg.writeNumberField("line", bugInfo.getLine());
                    jg.writeStringField("message", notNull(bugInfo.getMessage()));
                    jg.writeStringField("variable", notNull(bugInfo.getVariable()));
                    jg.writeStringField("expression", notNull(bugInfo.getExpression()));
                    jg.writeEndObject();
                    prevbugInfo = bugInfo;
                    bugInfo = iterator.hasNext() ? iterator.next() : null;
                } while (isGrouped(prevbugInfo, bugInfo));
                jg.writeEndArray();
                jg.writeEndObject();
            }
        }

        if (showStats) {
            for (final String code : counts.bugTypes()) {
                jg.writeStartObject();
                jg.writeStringField("code", code);
                jg.writeNumberField("count", counts.getCode(code));
                jg.writeEndObject();
            }
            for (final String severity : BugCounts.levels) {
                if (counts.getSeverity(severity) > 0) {
                    jg.writeStartObject();
                    jg.writeStringField("severity", severity);
                    jg.writeNumberField("count", counts.getSeverity(severity));
                    jg.writeEndObject();
                }
            }
        }

        jg.writeEndArray();
        jg.close();
        writer.close();
    }

    List<String> CODE_GROUPBY_FUNCTION = Arrays.asList("PARSE_ERROR");

    private boolean isGrouped(final BugInfo prevbugInfo, final BugInfo bugInfo) {
        if (prevbugInfo == null || bugInfo == null) {
            return false;
        }
        // Different message types are not grouped
        if (!safeEquals(prevbugInfo.getMessageCode(), bugInfo.getMessageCode())) {
            return false;
        }
{'fraction_non_alphanumeric': 0.11175402768323123, 'fraction_numerical': 0.0010211027910142954, 'mean_word_length': 2.120353982300885, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '6332257', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2236, 'n_words': 447}