<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.safehaus.kiskis.mgmt.shared.protocol;

import com.google.gson.Gson;
import org.safehaus.kiskis.mgmt.shared.protocol.api.CommandInterface;

/**
 * @author dilshat
 */
public class CommandJson {

    private static Gson gson = new Gson();

    public static Request getRequest(String json) {
        try {
            Command cmd = gson.fromJson(escape(json), Command.class);
            if (cmd.command != null) {
                return (Request) cmd.command;
            }
        } catch (Exception e) {
        }


        return null;
    }

    public static Response getResponse(String json) {
        try {
            Command cmd = gson.fromJson(escape(json), Command.class);
            if (cmd.response != null) {
                return (Response) cmd.response;
            }
        } catch (Exception e) {
        }

        return null;
    }

    public static String getJson(CommandInterface cmd) {
        try {

            return gson.toJson(cmd);
        } catch (Exception e) {
        }
        return null;
    }

    private static String escape(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case '"':
                    sb.append("\\\"");
                    break;
                case '\\':
                    sb.append("\\\\");
                    break;
                case '\b':
                    sb.append("\\b");
                    break;
                case '\f':
                    sb.append("\\f");
                    break;
                case '\n':
                    sb.append("\\n");
                    break;
                case '\r':
                    sb.append("\\r");
                    break;
                case '\t':
                    sb.append("\\t");
                    break;
                case '/':
                    sb.append("\\/");
                    break;
                default:
                    if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F') || (ch >= '\u2000' && ch <= '\u20FF')) {
                        String ss = Integer.toHexString(ch);
                        sb.append("\\u");
                        for (int k = 0; k < 4 - ss.length(); k++) {
                            sb.append('0');
                        }
                        sb.append(ss.toUpperCase());
                    } else {
                        sb.append(ch);
                    }
            }
        }
        return sb.toString().replace("\\\"", "\"");
    }
}
<commit_msg>fixed json issue<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.safehaus.kiskis.mgmt.shared.protocol;

import com.google.gson.Gson;
import org.safehaus.kiskis.mgmt.shared.protocol.api.CommandInterface;

/**
 * @author dilshat
 */
public class CommandJson {

    private static Gson gson = new Gson();

    public static Request getRequest(String json) {
        try {
            Command cmd = gson.fromJson(escape(json), Command.class);
            if (cmd.command != null) {
                return (Request) cmd.command;
            }
        } catch (Exception e) {
        }


        return null;
    }

    public static Response getResponse(String json) {
        try {
            Command cmd = gson.fromJson(escape(json), Command.class);
            if (cmd.response != null) {
                return (Response) cmd.response;
            }
        } catch (Exception e) {
        }

        return null;
    }

    public static String getJson(CommandInterface cmd) {
        try {

            return gson.toJson(cmd);
        } catch (Exception e) {
        }
        return null;
    }

    private static String escape(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case '"':
                    sb.append("\"");
                    break;
                case '\\':
                    sb.append("\\");
                    break;
                case '\b':
                    sb.append("\b");
                    break;
                case '\f':
                    sb.append("\f");
                    break;
                case '\n':
                    sb.append("\n");
                    break;
                case '\r':
                    sb.append("\r");
                    break;
                case '\t':
                    sb.append("\t");
                    break;
                case '/':
                    sb.append("\\/");
                    break;
                default:
                    if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F') || (ch >= '\u2000' && ch <= '\u20FF')) {
                        String ss = Integer.toHexString(ch);
                        sb.append("\\u");
                        for (int k = 0; k < 4 - ss.length(); k++) {
                            sb.append('0');
                        }
                        sb.append(ss.toUpperCase());
                    } else {
                        sb.append(ch);
                    }
            }
        }
        return sb.toString();
    }
}

{'fraction_non_alphanumeric': 0.13324780058651026, 'fraction_numerical': 0.00843108504398827, 'mean_word_length': 1.0196150999259808, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '5568502', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1378, 'n_words': 340}