<commit_before>package it.near.sdk.utils;



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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import it.near.sdk.logging.NearLog;
import it.near.sdk.morpheusnear.JsonApiObject;
import it.near.sdk.morpheusnear.Morpheus;
import it.near.sdk.morpheusnear.Resource;

/**
 * Serializing utils mothods for building json api coded strings.
 * Created by cattaneostefano on 18/10/2016.
 */

public class NearJsonAPIUtils {
    private static final String TAG = "NearJsonAPIUtils";

    /**
     * Turns an hashmap of values to a jsonapi resource string.
     *
     * @param type the type of the jsonapi resource.
     * @param map  the attribute map.
     * @return codified string.
     * @throws JSONException if map can be transformed into JSONObject
     */
    public static String toJsonAPI(String type, HashMap<String, Object> map) throws JSONException {
        return toJsonAPI(type, null, map);
    }

    /**
     * Turns a list of hashmaps into a json api array of resources of the same type.
     *
     * @param type the type of the jsonapi resource.
     * @param maps the maps of attributes.
     * @return codified string.
     * @throws JSONException if map can be transformed into JSONObject
     */
    public static String toJsonAPI(String type, List<HashMap<String, Object>> maps) throws JSONException {
        JSONArray array = new JSONArray();
        for (HashMap<String, Object> map : maps) {
            array.put(getResObj(type, null, map));
        }
        JSONObject outerObj = new JSONObject();
        outerObj.put("data", array);
        return outerObj.toString();
    }

    /**
     * Turns an hasmap of values to a jsonapi resource string. Also sets the id.
     *
     * @param type jsonapi resource type.
     * @param id   id of the resource.
     * @param map  values map.
     * @return codified string.
     * @throws JSONException if map can be transformed into JSONObject
     */
    public static String toJsonAPI(String type, String id, HashMap<String, Object> map) throws JSONException {
        JSONObject dataObject = getResObj(type, id, map);
        JSONObject outerObj = new JSONObject();
        outerObj.put("data", dataObject);
        return outerObj.toString();
    }

    /**
     * Build the data object of the jsonapi resource.
     *
     * @param type jsonapi resource type.
     * @param id   id of the resource.
     * @param map  values map
     * @return JSONObject representation of map object.
     * @throws JSONException if map can be transformed into JSONObject
     */
    private static JSONObject getResObj(String type, String id, HashMap<String, Object> map) throws JSONException {
        JSONObject attributesObj = new JSONObject();

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() instanceof HashMap) {
                attributesObj.put(entry.getKey(), new JSONObject((Map) entry.getValue()));
            } else {
                attributesObj.put(entry.getKey(), entry.getValue() != null ? entry.getValue() : JSONObject.NULL);
            }
        }

        JSONObject dataObject = new JSONObject();
        if (id != null) {
            dataObject.put("id", id);
        }
        dataObject.put("type", type);
        dataObject.put("attributes", attributesObj);
        return dataObject;
    }

    /**
     * Parse a list.
     *
     * @param morpheus the morpheus object. Its serializer must have been set to decode the Class of the objects of the list.
     * @param json     json to parse.
     * @param clazz    class of the list object.
     * @param <T>      generic type.
     * @return list of objects.
     */
    public static <T> List<T> parseList(Morpheus morpheus, JSONObject json, Class<T> clazz) {
        JsonApiObject jsonApiObject = null;
        try {
            jsonApiObject = morpheus.parse(json);
        } catch (Exception e) {
            NearLog.d(TAG, "Parsing error");
        }

        List<T> returnList = new ArrayList<T>();
        if (jsonApiObject == null ||
                jsonApiObject.getResources() == null) return returnList;

        for (Resource r : jsonApiObject.getResources()) {
            returnList.add((T) r);
        }
        return returnList;
    }

    /**
     * Parse an object.
     *
     * @param morpheus the morpheus object. Its serializer must have been set to decode the Class of the objects of the list.
     * @param json     json to parse.
     * @param clazz    class of the object.
     * @param <T>      generic type.
     * @return casted object.
     */
    public static <T> T parseElement(Morpheus morpheus, JSONObject json, Class<T> clazz) {
        JsonApiObject jsonApiObject = null;
        try {
            jsonApiObject = morpheus.parse(json);
        } catch (Exception e) {
            NearLog.d(TAG, "Parsing error");
        }
        return (T) jsonApiObject.getResource();
    }
}
<commit_msg>refactor near utils for jsonapi serializing<commit_after>package it.near.sdk.utils;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import it.near.sdk.logging.NearLog;
import it.near.sdk.morpheusnear.JsonApiObject;
import it.near.sdk.morpheusnear.Morpheus;
import it.near.sdk.morpheusnear.Resource;

import static it.near.sdk.utils.NearUtils.safe;

/**
 * Serializing utils methods for building json api coded strings.
 */

public class NearJsonAPIUtils {
    private static final String TAG = "NearJsonAPIUtils";
    private static final String KEY_DATA_ELEMENT = "data";
    private static final String KEY_ID = "id";
    private static final String KEY_TYPE = "type";
    private static final String KEY_ATTRIBUTES = "attributes";

    /**
     * Turns an hashmap of values to a jsonapi resource string.
     *
     * @param type the type of the jsonapi resource.
     * @param map  the attribute map.
     * @return codified string.
     * @throws JSONException if map can be transformed into JSONObject
     */
    public static String toJsonAPI(String type, HashMap<String, Object> map) throws JSONException {
        return toJsonAPI(type, null, map);
    }

    /**
     * Turns a list of hashmaps into a json api array of resources of the same type.
     *
     * @param type the type of the jsonapi resource.
     * @param maps the maps of attributes.
     * @return codified string.
     * @throws JSONException if map can be transformed into JSONObject
     */
    public static String toJsonAPI(String type, List<HashMap<String, Object>> maps) throws JSONException {
        JSONArray resources = new JSONArray();
        for (HashMap<String, Object> map : safe(maps)) {
            resources.put(buildResourceObject(type, null, map));
        }
        JSONObject jsonApiObject = new JSONObject();
        jsonApiObject.put(KEY_DATA_ELEMENT, resources);
        return jsonApiObject.toString();
    }

    /**
     * Turns an hashmap of values to a jsonapi resource string. Also sets the id.
     *
     * @param type jsonapi resource type.
     * @param id   id of the resource.
     * @param map  values map.
     * @return codified string.
     * @throws JSONException if map can be transformed into JSONObject
     */
    public static String toJsonAPI(String type, String id, HashMap<String, Object> map) throws JSONException {
        JSONObject resource = buildResourceObject(type, id, map);
        JSONObject jsonApiObject = new JSONObject();
        jsonApiObject.put(KEY_DATA_ELEMENT, resource);
        return jsonApiObject.toString();
    }

    /**
     * Build the data object of the jsonapi resource.
     *
     * @param type jsonapi resource type.
     * @param id   id of the resource.
     * @param map  values map
     * @return JSONObject representation of map object.
     * @throws JSONException if map can be transformed into JSONObject
     */
    private static JSONObject buildResourceObject(String type, String id, HashMap<String, Object> map) throws JSONException {
        if (map == null)
            throw new JSONException("Attribute map can't be null");

        JSONObject attributes = new JSONObject();

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() instanceof HashMap) {
                attributes.put(entry.getKey(), new JSONObject((Map) entry.getValue()));
            } else {
                attributes.put(entry.getKey(), entry.getValue() != null ? entry.getValue() : JSONObject.NULL);
            }
        }

        JSONObject resource = new JSONObject();
        if (id != null) {
            resource.put(KEY_ID, id);
        }
        resource.put(KEY_TYPE, type);
        resource.put(KEY_ATTRIBUTES, attributes);
        return resource;
    }

    /**
     * Parse a list.
     *
     * @param morpheus the morpheus object. Its serializer must have been set to decode the Class of the objects of the list.
     * @param json     json to parse.
     * @param clazz    class of the list object.
     * @param <T>      generic type.
     * @return list of objects.
     */
    public static <T> List<T> parseList(Morpheus morpheus, JSONObject json, Class<T> clazz) {
        JsonApiObject jsonApiObject = null;
        try {
            jsonApiObject = morpheus.parse(json);
        } catch (Exception e) {
            NearLog.d(TAG, "Parsing error");
        }

        List<T> returnList = new ArrayList<T>();
        if (jsonApiObject == null ||
                jsonApiObject.getResources() == null) return returnList;

        for (Resource r : jsonApiObject.getResources()) {
            returnList.add((T) r);
        }
        return returnList;
    }

    /**
     * Parse an object.
     *
     * @param morpheus the morpheus object. Its serializer must have been set to decode the Class of the objects of the list.
     * @param json     json to parse.
     * @param clazz    class of the object.
     * @param <T>      generic type.
     * @return casted object.
     */
    public static <T> T parseElement(Morpheus morpheus, JSONObject json, Class<T> clazz) {
        JsonApiObject jsonApiObject = null;
        try {
            jsonApiObject = morpheus.parse(json);
        } catch (Exception e) {
            NearLog.d(TAG, "Parsing error");
        }
        return (T) jsonApiObject.getResource();
    }
}

{'fraction_non_alphanumeric': 0.0949397132820659, 'fraction_numerical': 0.0007595177062565271, 'mean_word_length': 2.436867862969005, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '2558896', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2914, 'n_words': 985}