Search in sources :

Example 56 with JsonValue

use of com.eclipsesource.json.JsonValue in project Recaf by Col-E.

the class Configurable method load.

/**
 * @param path
 * 		Path to json file of config.
 *
 * @throws IOException
 * 		When the file cannot be read.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
default void load(Path path) throws IOException {
    JsonObject json = Json.parse(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8)).asObject();
    for (FieldWrapper field : getConfigFields()) {
        String name = field.key();
        if (name == null)
            continue;
        final JsonValue value = json.get(name);
        if (value != null) {
            try {
                Class<?> type = field.type();
                if (type.equals(Boolean.TYPE))
                    field.set(value.asBoolean());
                else if (type.equals(Integer.TYPE))
                    field.set(value.asInt());
                else if (type.equals(Long.TYPE))
                    field.set(value.asLong());
                else if (type.equals(Float.TYPE))
                    field.set(value.asFloat());
                else if (type.equals(Double.TYPE))
                    field.set(value.asDouble());
                else if (type.equals(String.class))
                    field.set(value.asString());
                else if (type.isEnum())
                    field.set(Enum.valueOf((Class<? extends Enum>) (Class<?>) field.type(), value.asString()));
                else if (type.equals(Resource.class)) {
                    JsonObject object = value.asObject();
                    String resPath = object.getString("path", null);
                    if (object.getBoolean("internal", true))
                        field.set(Resource.internal(resPath));
                    else
                        field.set(Resource.external(resPath));
                } else if (type.equals(List.class)) {
                    List<Object> list = new ArrayList<>();
                    JsonArray array = value.asArray();
                    // We're gonna assume our lists just hold strings
                    array.forEach(v -> {
                        if (v.isString())
                            list.add(v.asString());
                        else
                            warn("Didn't properly load config for {}, expected all string arguments", name);
                    });
                    field.set(list);
                } else if (supported(type))
                    loadType(field, type, value);
                else
                    warn("Didn't load config for {}, unsure how to serialize.", name);
            } catch (Exception ex) {
                error(ex, "Skipping bad option: {} - {}", path.getFileName(), name);
            }
        }
    }
    onLoad();
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonValue(com.eclipsesource.json.JsonValue) Resource(me.coley.recaf.util.Resource) ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject) JsonObject(com.eclipsesource.json.JsonObject) IOException(java.io.IOException)

Example 57 with JsonValue

use of com.eclipsesource.json.JsonValue in project Recaf by Col-E.

the class SelfUpdater method fetchLatestInfo.

/**
 * Fetch the {@link #latestVersion latest version}
 * and {@link #latestArtifact latest artifact url}.
 *
 * @throws IOException
 * 		When opening a connection to the API url fails.
 */
private static void fetchLatestInfo() throws IOException {
    URL updateURL = new URL(API);
    String content = IOUtils.toString(updateURL.openStream(), StandardCharsets.UTF_8);
    JsonObject updateJson = Json.parse(content).asObject();
    // compare versions
    latestVersion = updateJson.getString("tag_name", "2.0.0");
    latestPatchnotes = updateJson.getString("body", "#Error\nCould not fetch update notes.");
    if (isOutdated()) {
        Log.info(LangUtil.translate("update.outdated"));
        JsonArray assets = updateJson.get("assets").asArray();
        for (JsonValue assetValue : assets.values()) {
            JsonObject assetObj = assetValue.asObject();
            String file = assetObj.getString("name", "invalid");
            // Skip non-jars
            if (!file.endsWith(".jar")) {
                continue;
            }
            // Find the largest jar
            int size = assetObj.getInt("size", 0);
            if (size > latestArtifactSize) {
                latestArtifactSize = size;
                String fileURL = assetObj.getString("browser_download_url", null);
                if (fileURL != null)
                    latestArtifact = fileURL;
            }
        }
        try {
            String date = updateJson.getString("published_at", null);
            if (date != null)
                latestVersionDate = Instant.parse(date);
        } catch (DateTimeParseException ex) {
            Log.warn("Failed to parse timestamp for latest release");
        }
        if (latestArtifact == null)
            Log.warn(LangUtil.translate("update.fail.nodownload"));
    }
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) DateTimeParseException(java.time.format.DateTimeParseException) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) URL(java.net.URL)

Example 58 with JsonValue

use of com.eclipsesource.json.JsonValue in project JRomManager by optyfr.

the class ProfileActions method doImport.

/**
 * @param session
 * @param jsobj
 * @param sl
 * @param imprt
 * @throws SecurityException
 * @throws IOException
 */
private void doImport(WebSession session, JsonObject jsobj, final boolean sl, final Import imprt) throws SecurityException, IOException {
    final var parent = getAbsolutePath(Optional.ofNullable(jsobj.get(PARENT)).filter(JsonValue::isString).map(JsonValue::asString).orElse(session.getUser().getSettings().getWorkPath().toString())).toFile();
    final var file = new File(parent, imprt.getFile().getName());
    FileUtils.copyFile(imprt.getFile(), file);
    final var pnfo = ProfileNFO.load(session, file);
    pnfo.getMame().set(imprt.getOrgFile(), sl);
    if (imprt.getRomsFile() != null) {
        FileUtils.copyFileToDirectory(imprt.getRomsFile(), parent);
        pnfo.getMame().setFileroms(new File(parent, imprt.getRomsFile().getName()));
        if (sl) {
            if (imprt.getSlFile() != null) {
                FileUtils.copyFileToDirectory(imprt.getSlFile(), parent);
                pnfo.getMame().setFilesl(new File(parent, imprt.getSlFile().getName()));
            } else
                new GlobalActions(ws).warn("Could not import softwares list");
        }
        pnfo.save(session);
        imported(pnfo.getFile());
    } else {
        new GlobalActions(ws).warn("Could not import roms list");
        Files.delete(file.toPath());
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) File(java.io.File)

Example 59 with JsonValue

use of com.eclipsesource.json.JsonValue in project JRomManager by optyfr.

the class GlobalActions method setProperty.

@SuppressWarnings("exports")
public void setProperty(JsonObject jso) {
    JsonObject pjso = jso.get(PARAMS).asObject();
    for (Member m : pjso) {
        JsonValue value = m.getValue();
        if (value.isBoolean())
            ws.getSession().getUser().getSettings().setProperty(m.getName(), value.asBoolean());
        else if (value.isString())
            ws.getSession().getUser().getSettings().setProperty(m.getName(), value.asString());
        else
            ws.getSession().getUser().getSettings().setProperty(m.getName(), value.toString());
    }
    try {
        if (ws.isOpen()) {
            ws.getSession().getUser().getSettings().saveSettings();
            final var rjso = new JsonObject();
            rjso.add("cmd", "Global.updateProperty");
            rjso.add(PARAMS, pjso);
            ws.send(rjso.toString());
        }
    } catch (IOException e) {
        Log.err(e.getMessage(), e);
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) IOException(java.io.IOException) Member(com.eclipsesource.json.JsonObject.Member)

Example 60 with JsonValue

use of com.eclipsesource.json.JsonValue in project JRomManager by optyfr.

the class Settings method asJSO.

public JsonObject asJSO() {
    final var jso = new JsonObject();
    properties.forEach((k, v) -> {
        try {
            JsonValue value = Json.parse((String) v);
            if (value.isObject() || value.isArray() || value.isBoolean())
                jso.add((String) k, value);
            else
                jso.add((String) k, (String) v);
        } catch (Exception e) {
            jso.add((String) k, (String) v);
        }
    });
    return jso;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) IOException(java.io.IOException)

Aggregations

JsonValue (com.eclipsesource.json.JsonValue)147 JsonObject (com.eclipsesource.json.JsonObject)74 JsonArray (com.eclipsesource.json.JsonArray)43 Test (org.junit.Test)38 ArrayList (java.util.ArrayList)26 URL (java.net.URL)21 HashMap (java.util.HashMap)10 IOException (java.io.IOException)9 Member (com.eclipsesource.json.JsonObject.Member)6 ParseException (com.eclipsesource.json.ParseException)4 File (java.io.File)4 MalformedURLException (java.net.MalformedURLException)4 InetSocketAddress (java.net.InetSocketAddress)3 Collection (java.util.Collection)3 Map (java.util.Map)3 WebTarget (javax.ws.rs.client.WebTarget)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)2 WalletCallException (com.vaklinov.zcashui.ZCashClientCaller.WalletCallException)2 FileInputStream (java.io.FileInputStream)2