<commit_before>package me.vickychijwani.spectre.network;

import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;

import java.lang.reflect.Type;
import java.util.Map;

import me.vickychijwani.spectre.model.entity.ConfigurationParam;
import me.vickychijwani.spectre.network.entity.ConfigurationList;

/* package */ class ConfigurationListDeserializer implements JsonDeserializer<ConfigurationList> {

    @Override
    public ConfigurationList deserialize(JsonElement element, Type type,
                                         JsonDeserializationContext context)
            throws JsonParseException {
        try {
            JsonArray configJsons = element.getAsJsonObject().getAsJsonArray("configuration");
            if (configJsons.size() > 0 && !configJsons.get(0).getAsJsonObject().has("key")) {
                // new configuration format - dictionary style
                // { configuration: [{ fileStorage: {value: true, type: "bool"}, ... }] }
                return parseDictionaryStyleConfig(configJsons.get(0).getAsJsonObject());
            } else {
                // old configuration format - array of entries
                // { configuration: [ {key: fileStorage, value: true}, ... ] }
                return parseArrayOfEntriesConfig(configJsons);
            }
        } catch (Exception e) {
            // FIXME temp log to help debug Crashlytics issue #87
            Crashlytics.log(Log.DEBUG, "ParseException", "Exception thrown while trying to parse JSON: " + element.toString());
            throw e;
        }
    }

    private ConfigurationList parseDictionaryStyleConfig(JsonObject configJson) {
        ConfigurationList config = new ConfigurationList();
        for (Map.Entry<String, JsonElement> entryJson : configJson.entrySet()) {
            String key = entryJson.getKey();
            JsonPrimitive valueJson = entryJson.getValue().getAsJsonObject()
                    .get("value").getAsJsonPrimitive();
            config.configuration.add(makeConfigParam(key, valueJson));
        }
        return config;
    }

    private ConfigurationList parseArrayOfEntriesConfig(JsonArray configJsons) {
        ConfigurationList config = new ConfigurationList();
        for (JsonElement itemJsonEl : configJsons) {
            JsonObject itemJson = itemJsonEl.getAsJsonObject();
            String key = itemJson.get("key").getAsString();
            JsonPrimitive valueJson = itemJson.getAsJsonPrimitive("value");
            config.configuration.add(makeConfigParam(key, valueJson));
        }
        return config;
    }

    private ConfigurationParam makeConfigParam(String key, JsonPrimitive value)
            throws JsonParseException {
        ConfigurationParam param = new ConfigurationParam();
        String valueStr;
        if (value == null && "mail".equals(key)) {
            valueStr = "";  // Ghost 0.8.0 gives an empty string instead of null, when no mail transport is set
                            // we don't care about the mail transport anyhow
        } else if (value == null) {
            // FIXME temp log to help debug Crashlytics issue #87
            throw new NullPointerException("value for key '" + key + "' is null!");
        } else if (value.isString()) {
            valueStr = value.getAsString();
        } else if (value.isBoolean()) {
            valueStr = String.valueOf(value.getAsBoolean());
        } else if (value.isNumber()) {
            valueStr = String.valueOf(value.getAsDouble());
        } else {
            throw new JsonParseException("unknown value type in Ghost configuration list");
        }
        param.setKey(key);
        param.setValue(valueStr);
        return param;
    }

}<commit_msg>[Ghost 1.0] Handle new configuration endpoint schema.<commit_after>package me.vickychijwani.spectre.network;

import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.Map;

import me.vickychijwani.spectre.model.entity.ConfigurationParam;
import me.vickychijwani.spectre.network.entity.ConfigurationList;

/* package */ class ConfigurationListDeserializer implements JsonDeserializer<ConfigurationList> {

    @Override
    public ConfigurationList deserialize(JsonElement element, Type type,
                                         JsonDeserializationContext context)
            throws JsonParseException {
        try {
            JsonArray configJsons = element.getAsJsonObject().getAsJsonArray("configuration");
            // { "configuration": [{
            //     "fileStorage": true,
            //     "blogTitle": "My Blog",
            //     "routeKeywords": { ... },
            //     ...
            // }]}
            return parseConfig(configJsons.get(0).getAsJsonObject());
        } catch (Exception e) {
            Crashlytics.log(Log.DEBUG, "ParseException", "Exception thrown while trying to parse JSON: " + element.toString());
            throw e;
        }
    }

    private ConfigurationList parseConfig(JsonObject configJson) {
        ConfigurationList config = new ConfigurationList();
        for (Map.Entry<String, JsonElement> entryJson : configJson.entrySet()) {
            String key = entryJson.getKey();
            JsonElement value = entryJson.getValue();
            config.configuration.add(makeConfigParam(key, value));
        }
        return config;
    }

    private ConfigurationParam makeConfigParam(String key, JsonElement value) {
        if (value.isJsonNull()) {
            throw new NullPointerException("value for key '" + key + "' is null!");
        }
        ConfigurationParam param = new ConfigurationParam();
        param.setKey(key);
        String valueStr;
        if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
            // toString would return the string with quotes around it which we don't want
            valueStr = value.getAsString();
        } else {
            valueStr = value.toString();
        }
        param.setValue(valueStr);
        return param;
    }

}
{'fraction_non_alphanumeric': 0.1014294403892944, 'fraction_numerical': 0.0019768856447688566, 'mean_word_length': 2.5436422413793105, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7063438', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1670, 'n_words': 390}