<commit_before>package com.jasonette.seed.Core;

import android.content.Context;
import android.util.Log;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.jasonette.seed.Helper.JasonHelper;

import org.json.JSONObject;

import java.util.concurrent.LinkedBlockingQueue;

import timber.log.Timber;

public class JasonParser {
    static JSONObject res;
    private static Context context;

    private static JasonParser instance = null;

    private JasonParser(){
        this.listener = null;
    }

    public interface JasonParserListener {
        public void onFinished(JSONObject json);
    }
    private JasonParserListener listener;
    private V8 juice;
    public void setParserListener(JasonParserListener listener){
        this.listener = listener;
    }


    private class Task {
        public String data_type;
        public JSONObject data;
        public Object template;
        public Task(String data_type, JSONObject data, Object template) {
            this.data_type = data_type;
            this.data = data;
            this.template = template;
        }
    }
    private LinkedBlockingQueue<Task> taskQueue = new LinkedBlockingQueue<Task>();
    private void processTask(Task task) {
        try {
            Object template = task.template;
            JSONObject data = task.data;
            String data_type = task.data_type;

            // thread handling - acquire handle
            juice.getLocker().acquire();
            Console console = new Console();
            V8Object v8Console = new V8Object(juice);
            juice.add("console", v8Console);
            v8Console.registerJavaMethod(console, "log", "log", new Class<?>[] { String.class });
            v8Console.registerJavaMethod(console, "error", "error", new Class<?>[] { String.class });
            v8Console.registerJavaMethod(console, "trace", "trace", new Class<?>[] {});

            String templateJson = template.toString();
            String dataJson = data.toString();
            String val = "{}";

            V8Object parser = juice.getObject("ST");
            // Get global variables (excluding natively injected variables which will never be used in the template)
            String globals = juice.executeStringScript("JSON.stringify(Object.keys(this).filter(function(key){return ['ST', 'to_json', 'setImmediate', 'clearImmediate', 'console'].indexOf(key) === -1;}));");
            if(data_type.equalsIgnoreCase("json")) {
                V8Array parameters = new V8Array(juice);
                parameters.push(templateJson);
                parameters.push(dataJson);
                parameters.push(globals);
                parameters.push(true);
                val = parser.executeStringFunction("transform", parameters);
                parameters.release();
            } else {
                String raw_data = data.getString("$jason");
                V8Array parameters = new V8Array(juice).push("html");
                parameters.push(templateJson);
                parameters.push(raw_data);
                parameters.push(globals);
                parameters.push(true);
                val = juice.executeStringFunction("to_json", parameters);
                parameters.release();
            }
            parser.release();
            v8Console.release();

            res = new JSONObject(val);

            listener.onFinished(res);
<commit_msg>Parser fix => when the result is null, it should just ignore instead of fail<commit_after>package com.jasonette.seed.Core;

import android.content.Context;
import android.util.Log;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.jasonette.seed.Helper.JasonHelper;

import org.json.JSONObject;

import java.util.concurrent.LinkedBlockingQueue;

import timber.log.Timber;

public class JasonParser {
    static JSONObject res;
    private static Context context;

    private static JasonParser instance = null;

    private JasonParser(){
        this.listener = null;
    }

    public interface JasonParserListener {
        public void onFinished(JSONObject json);
    }
    private JasonParserListener listener;
    private V8 juice;
    public void setParserListener(JasonParserListener listener){
        this.listener = listener;
    }


    private class Task {
        public String data_type;
        public JSONObject data;
        public Object template;
        public Task(String data_type, JSONObject data, Object template) {
            this.data_type = data_type;
            this.data = data;
            this.template = template;
        }
    }
    private LinkedBlockingQueue<Task> taskQueue = new LinkedBlockingQueue<Task>();
    private void processTask(Task task) {
        try {
            Object template = task.template;
            JSONObject data = task.data;
            String data_type = task.data_type;

            // thread handling - acquire handle
            juice.getLocker().acquire();
            Console console = new Console();
            V8Object v8Console = new V8Object(juice);
            juice.add("console", v8Console);
            v8Console.registerJavaMethod(console, "log", "log", new Class<?>[] { String.class });
            v8Console.registerJavaMethod(console, "error", "error", new Class<?>[] { String.class });
            v8Console.registerJavaMethod(console, "trace", "trace", new Class<?>[] {});

            String templateJson = template.toString();
            String dataJson = data.toString();
            String val = "{}";

            V8Object parser = juice.getObject("ST");
            // Get global variables (excluding natively injected variables which will never be used in the template)
            String globals = juice.executeStringScript("JSON.stringify(Object.keys(this).filter(function(key){return ['ST', 'to_json', 'setImmediate', 'clearImmediate', 'console'].indexOf(key) === -1;}));");
            if(data_type.equalsIgnoreCase("json")) {
                V8Array parameters = new V8Array(juice);
                parameters.push(templateJson);
                parameters.push(dataJson);
                parameters.push(globals);
                parameters.push(true);
                val = parser.executeStringFunction("transform", parameters);
                parameters.release();
            } else {
                String raw_data = data.getString("$jason");
                V8Array parameters = new V8Array(juice).push("html");
                parameters.push(templateJson);
                parameters.push(raw_data);
                parameters.push(globals);
                parameters.push(true);
                val = juice.executeStringFunction("to_json", parameters);
                parameters.release();
            }
            parser.release();
            v8Console.release();

            if (val.equalsIgnoreCase("null")) {
                res = new JSONObject();
            } else {
                res = new JSONObject(val);
            }

            listener.onFinished(res);

{'fraction_non_alphanumeric': 0.1047752808988764, 'fraction_numerical': 0.005898876404494382, 'mean_word_length': 2.295233688107358, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '3432954', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1792, 'n_words': 427}