<commit_before>package com.github.rahulrvp.android_utils;

import android.text.TextUtils;
import android.util.Log;

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

import java.util.Iterator;

/**
 * @author <NAME>
 *         Created on 23/1/17 @ 5:27 PM
 *         https://github.com/rahulrvp
 */

public class JsonFormatter {

    private String colorBraces;
    private String colorSqBracket;
    private String colorComma;
    private String colorKey;
    private String colorString;
    private String colorNumber;
    private String colorBoolean;
    private OutputFormat outputFormat;

    private JsonFormatter() {
        colorBraces = "#729fcf";
        colorSqBracket = "#a4074f";
        colorComma = "#370007";
        colorKey = <KEY>";
        colorString = "#53a06b";
        colorNumber = "#af83a8";
        colorBoolean = "#ddab1f";
        outputFormat = OutputFormat.String;
    }

    public String format(String text) {
        StringBuilder stringBuilder = new StringBuilder();

        try {
            formatJson(stringBuilder, 0, new JSONObject(text), false);
        } catch (Exception e) {
            try {
                formatJson(stringBuilder, 0, new JSONArray(text), false);
            } catch (Exception err) {
                Log.e("JsonFormatter", e.getMessage());
            }
        }

        return stringBuilder.toString();
    }

    public void formatJson(StringBuilder builder, int level, JSONObject jsonObject, boolean isArrayElement) throws JSONException {
        if (jsonObject != null && builder != null) {
            String levelZeroTab = tabs(level);
            String levelOneTab = tabs(level + 1);
            String newLine = getNewlineString();

            if (isArrayElement) {
                builder.append(levelZeroTab);
            }

            addNonQuotedElement(builder, "{", colorBraces);

            Iterator<String> keys = jsonObject.keys();
            while (keys.hasNext()) {
                builder.append(getNewlineString());

                String key = keys.next();
                Object value = jsonObject.get(key);

                builder.append(levelOneTab);

                addQuotedElement(builder, key, colorKey);

                builder.append(" : ");

                if (value instanceof JSONArray) {
                    formatJson(builder, level + 1, (JSONArray) value, false);
                } else if (value instanceof JSONObject) {
                    formatJson(builder, level + 1, (JSONObject) value, false);
                } else {
                    addValueElement(builder, value);
                }

                if (keys.hasNext()) {
                    addNonQuotedElement(builder, ",", colorComma);
                }
            }

            builder
                    .append(newLine)
                    .append(levelZeroTab);

            addNonQuotedElement(builder, "}", colorBraces);
        }
    }

    public void formatJson(StringBuilder builder, int level, JSONArray jsonArray, boolean isArrayElement) throws JSONException {
        if (jsonArray != null && builder != null) {
            int len = jsonArray.length();
            String levelZeroTab = tabs(level);
            String levelOneTab = tabs(level + 1);
            String newLine = getNewlineString();

            if (isArrayElement) {
                builder.append(levelZeroTab);
            }

            addNonQuotedElement(builder, "[", colorSqBracket);

            for (int i = 0; i < len; i++) {
                builder.append(newLine);

                Object element = jsonArray.get(i);
                if (element instanceof JSONArray) {
                    formatJson(builder, level + 1, (JSONArray) element, true);
                } else if (element instanceof JSONObject) {
                    formatJson(builder, level + 1, (JSONObject) element, true);
                } else {
                    builder.append(levelOneTab);
                    addValueElement(builder, element);
                }

                if (len - i != 1) {
                    addNonQuotedElement(builder, ",", colorComma);
                }
            }

            builder
                    .append(newLine)
                    .append(levelZeroTab);

            addNonQuotedElement(builder, "]", colorSqBracket);
        }
    }

    private void addValueElement(StringBuilder builder, Object element) {
        if (element instanceof String) {
            addQuotedElement(builder, element, colorString);
        } else if (element instanceof Boolean) {
            addNonQuotedElement(builder, element, colorBoolean);
        } else {
            addNonQuotedElement(builder, element, colorNumber);
        }
    }

    private void addQuotedElement(StringBuilder builder, Object value, String colorHash) {
        if (outputFormat != null && outputFormat == OutputFormat.Html) {
            builder
                    .append(fontTagOpen(colorHash))
                    .append("\"")
                    .append(value)
                    .append("\"")
                    .append(fontTagClose());
        } else {
            builder.<commit_msg>removed newline before brackets in empty objects/arrays<commit_after>package com.github.rahulrvp.android_utils;

import android.text.TextUtils;
import android.util.Log;

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

import java.util.Iterator;

/**
 * @author <NAME>
 *         Created on 23/1/17 @ 5:27 PM
 *         https://github.com/rahulrvp
 */

public class JsonFormatter {

    private String colorBraces;
    private String colorSqBracket;
    private String colorComma;
    private String colorKey;
    private String colorString;
    private String colorNumber;
    private String colorBoolean;
    private OutputFormat outputFormat;

    private JsonFormatter() {
        colorBraces = "#729fcf";
        colorSqBracket = "#a4074f";
        colorComma = "#370007";
        colorKey = <KEY>";
        colorString = "#53a06b";
        colorNumber = "#af83a8";
        colorBoolean = "#ddab1f";
        outputFormat = OutputFormat.String;
    }

    public String format(String text) {
        StringBuilder stringBuilder = new StringBuilder();

        try {
            formatJson(stringBuilder, 0, new JSONObject(text), false);
        } catch (Exception e) {
            try {
                formatJson(stringBuilder, 0, new JSONArray(text), false);
            } catch (Exception err) {
                Log.e("JsonFormatter", e.getMessage());
            }
        }

        return stringBuilder.toString();
    }

    public void formatJson(StringBuilder builder, int level, JSONObject jsonObject, boolean isArrayElement) throws JSONException {
        if (jsonObject != null && builder != null) {
            String levelZeroTab = tabs(level);
            String levelOneTab = tabs(level + 1);
            String newLine = getNewlineString();

            if (isArrayElement) {
                builder.append(levelZeroTab);
            }

            addNonQuotedElement(builder, "{", colorBraces);

            if (jsonObject.length() > 0) {
                Iterator<String> keys = jsonObject.keys();
                while (keys.hasNext()) {
                    builder.append(getNewlineString());

                    String key = keys.next();
                    Object value = jsonObject.get(key);

                    builder.append(levelOneTab);

                    addQuotedElement(builder, key, colorKey);

                    builder.append(" : ");

                    if (value instanceof JSONArray) {
                        formatJson(builder, level + 1, (JSONArray) value, false);
                    } else if (value instanceof JSONObject) {
                        formatJson(builder, level + 1, (JSONObject) value, false);
                    } else {
                        addValueElement(builder, value);
                    }

                    if (keys.hasNext()) {
                        addNonQuotedElement(builder, ",", colorComma);
                    }
                }

                builder
                        .append(newLine)
                        .append(levelZeroTab);
            }

            addNonQuotedElement(builder, "}", colorBraces);
        }
    }

    public void formatJson(StringBuilder builder, int level, JSONArray jsonArray, boolean isArrayElement) throws JSONException {
        if (jsonArray != null && builder != null) {
            int len = jsonArray.length();
            String levelZeroTab = tabs(level);
            String levelOneTab = tabs(level + 1);
            String newLine = getNewlineString();

            if (isArrayElement) {
                builder.append(levelZeroTab);
            }

            addNonQuotedElement(builder, "[", colorSqBracket);

            if (len > 0) {
                for (int i = 0; i < len; i++) {
                    builder.append(newLine);

                    Object element = jsonArray.get(i);
                    if (element instanceof JSONArray) {
                        formatJson(builder, level + 1, (JSONArray) element, true);
                    } else if (element instanceof JSONObject) {
                        formatJson(builder, level + 1, (JSONObject) element, true);
                    } else {
                        builder.append(levelOneTab);
                        addValueElement(builder, element);
                    }

                    if (len - i != 1) {
                        addNonQuotedElement(builder, ",", colorComma);
                    }
                }

                builder
                        .append(newLine)
                        .append(levelZeroTab);
            }

            addNonQuotedElement(builder, "]", colorSqBracket);
        }
    }

    private void addValueElement(StringBuilder builder, Object element) {
        if (element instanceof String) {
            addQuotedElement(builder, element, colorString);
        } else if (element instanceof Boolean) {
            addNonQuotedElement(builder, element, colorBoolean);
        } else {
            addNonQuotedElement(builder, element, colorNumber);
        }
    }

    private void addQuotedElement(StringBuilder builder, Object value, String colorHash) {
        if (outputFormat != null && outputFormat == OutputFormat.Html) {
            builder
                    .append(fontTagOpen(colorHash))
                    .append("\"")
                    .append(value)
                    .append("\"")
                    .append(fontTagClose());
        } else {
            builder.
{'fraction_non_alphanumeric': 0.09720921520884061, 'fraction_numerical': 0.0074920397078104516, 'mean_word_length': 1.6518500124161908, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '4267292', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2561, 'n_words': 625}