<commit_before>package org.hcjf.encoding;

import com.google.gson.*;
import org.hcjf.errors.Errors;
import org.hcjf.layers.query.Evaluator;
import org.hcjf.layers.query.Query;
import org.hcjf.properties.SystemProperties;
import org.hcjf.utils.Introspection;
import org.hcjf.utils.Strings;

import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author javaito
 * @mail <EMAIL>
 */
public class JsonEncoding extends EncodingImpl {

    private static final String HCJF_JSON_IMPLEMENTATION = "hcjf";

    public JsonEncoding() {
        super(MimeType.APPLICATION_JSON, HCJF_JSON_IMPLEMENTATION);
    }

    /**
     * @param decodedPackage
     * @return
     */
    @Override
    public byte[] encode(DecodedPackage decodedPackage) {
        Gson gson = new Gson();
        JsonObject jsonObject = new JsonObject();

        if(decodedPackage instanceof CrudDecodedPackage) {
            CrudDecodedPackage crudDecodedPackage = (CrudDecodedPackage) decodedPackage;
            if (crudDecodedPackage.getParameters() != null) {
                JsonObject parameterObject = new JsonObject();
                for (String key : crudDecodedPackage.getParameters().keySet()) {
                    parameterObject.add(key, createTypedObject(crudDecodedPackage.getParameters().get(key)));
                }
                jsonObject.add(PARAMETERS_JSON_FIELD, parameterObject);
            }

            if (crudDecodedPackage.getQuery() != null) {
                JsonObject queryObject = new JsonObject();
                queryObject.add(QUERY_ID_FIELD, new JsonPrimitive(crudDecodedPackage.getQuery().getId().toString()));
                queryObject.add(QUERY_LIMIT_FIELD, new JsonPrimitive(crudDecodedPackage.getQuery().getLimit()));
                queryObject.add(QUERY_DESC_FIELD, new JsonPrimitive(crudDecodedPackage.getQuery().isDesc()));
                queryObject.add(QUERY_PAGE_START_FIELD, createTypedObject(crudDecodedPackage.getQuery().getPageStart()));
                JsonArray orderArray = new JsonArray();
                crudDecodedPackage.getQuery().getOrderFields().forEach(orderArray::add);
                queryObject.add(QUERY_ORDER_FIELDS_FIELD, orderArray);
                JsonArray evaluatorArray = new JsonArray();
                for (Evaluator evaluator : crudDecodedPackage.getQuery().getEvaluators()) {
                    JsonObject evaluatorJsonObject = new JsonObject();
                    evaluatorJsonObject.add(EVALUATOR_ACTION_FIELD,
                            new JsonPrimitive(Strings.uncapitalize(evaluator.getClass().getSimpleName())));
                    evaluatorJsonObject.add(EVALUATOR_FIELD_FIELD, new JsonPrimitive(evaluator.getFieldName()));
                    evaluatorJsonObject.add(EVALUATOR_VALUE_FIELD, createTypedObject(evaluator.getValue()));
                    evaluatorArray.add(evaluatorJsonObject);
                }
                jsonObject.add(QUERY_JSON_FIELD, queryObject);
            }

            if (crudDecodedPackage.getObject() != null) {
                if (crudDecodedPackage.getObject() instanceof Collection) {
                    JsonArray array = new JsonArray();
                    for (Object arrayObject : (Collection) crudDecodedPackage.getObject()) {
                        array.add(getBodyObject(arrayObject));
                    }
                    jsonObject.add(BODY_JSON_FIELD, array);
                } else {
                    jsonObject.add(BODY_JSON_FIELD, getBodyObject(crudDecodedPackage.getObject()));
                }
            }
        } else {
            throw new UnsupportedOperationException(Errors.getMessage(Errors.ORG_HCJF_ENCODING_3));
        }

        return gson.toJson(jsonObject).getBytes();<commit_msg>Fixed json encoding to support evaluator collections<commit_after>package org.hcjf.encoding;

import com.google.gson.*;
import org.hcjf.errors.Errors;
import org.hcjf.layers.query.Evaluator;
import org.hcjf.layers.query.FieldEvaluator;
import org.hcjf.layers.query.Query;
import org.hcjf.properties.SystemProperties;
import org.hcjf.utils.Introspection;
import org.hcjf.utils.Strings;

import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author javaito
 * @mail <EMAIL>
 */
public class JsonEncoding extends EncodingImpl {

    private static final String HCJF_JSON_IMPLEMENTATION = "hcjf";

    public JsonEncoding() {
        super(MimeType.APPLICATION_JSON, HCJF_JSON_IMPLEMENTATION);
    }

    /**
     * @param decodedPackage
     * @return
     */
    @Override
    public byte[] encode(DecodedPackage decodedPackage) {
        Gson gson = new Gson();
        JsonObject jsonObject = new JsonObject();

        if(decodedPackage instanceof CrudDecodedPackage) {
            CrudDecodedPackage crudDecodedPackage = (CrudDecodedPackage) decodedPackage;
            if (crudDecodedPackage.getParameters() != null) {
                JsonObject parameterObject = new JsonObject();
                for (String key : crudDecodedPackage.getParameters().keySet()) {
                    parameterObject.add(key, createTypedObject(crudDecodedPackage.getParameters().get(key)));
                }
                jsonObject.add(PARAMETERS_JSON_FIELD, parameterObject);
            }

            if (crudDecodedPackage.getQuery() != null) {
                JsonObject queryObject = new JsonObject();
                queryObject.add(QUERY_ID_FIELD, new JsonPrimitive(crudDecodedPackage.getQuery().getId().toString()));
                queryObject.add(QUERY_LIMIT_FIELD, new JsonPrimitive(crudDecodedPackage.getQuery().getLimit()));
                queryObject.add(QUERY_DESC_FIELD, new JsonPrimitive(crudDecodedPackage.getQuery().isDesc()));
                queryObject.add(QUERY_PAGE_START_FIELD, createTypedObject(crudDecodedPackage.getQuery().getPageStart()));
                JsonArray orderArray = new JsonArray();
                crudDecodedPackage.getQuery().getOrderFields().forEach(orderArray::add);
                queryObject.add(QUERY_ORDER_FIELDS_FIELD, orderArray);
                JsonArray evaluatorArray = new JsonArray();
                for (Evaluator evaluator : crudDecodedPackage.getQuery().getEvaluators()) {
                    if(evaluator instanceof FieldEvaluator) {
                        FieldEvaluator fieldEvaluator = (FieldEvaluator) evaluator;
                        JsonObject evaluatorJsonObject = new JsonObject();
                        evaluatorJsonObject.add(EVALUATOR_ACTION_FIELD,
                                new JsonPrimitive(Strings.uncapitalize(evaluator.getClass().getSimpleName())));
                        evaluatorJsonObject.add(EVALUATOR_FIELD_FIELD, new JsonPrimitive(fieldEvaluator.getFieldName()));
                        evaluatorJsonObject.add(EVALUATOR_VALUE_FIELD, createTypedObject(fieldEvaluator.getValue()));
                        evaluatorArray.add(evaluatorJsonObject);
                    }
                }
                jsonObject.add(QUERY_JSON_FIELD, queryObject);
            }

            if (crudDecodedPackage.getObject() != null) {
                if (crudDecodedPackage.getObject() instanceof Collection) {
                    JsonArray array = new JsonArray();
                    for (Object arrayObject : (Collection) crudDecodedPackage.getObject()) {
                        array.add(getBodyObject(arrayObject));
                    }
                    jsonObject.add(BODY_JSON_FIELD, array);
                } else {
                    jsonObject.add(BODY_JSON_FIELD, getBodyObject(crudDecodedPackage.getObject()));
                }
            }
        } else {
            throw new UnsupportedOperationException(Errors.getMessage(Errors.ORG_HCJF_ENCODING_3));
        }

        return gson.toJson(jsonObject).getBytes();
{'fraction_non_alphanumeric': 0.09591315453384419, 'fraction_numerical': 0.0002554278416347382, 'mean_word_length': 2.485091232754784, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7575517', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2083, 'n_words': 324}