Search in sources :

Example 61 with JsonValue

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

the class SrcDstResult method fromJSON.

public static SDRList fromJSON(String json) {
    SDRList sdrl = new SDRList();
    for (// $NON-NLS-1$ //$NON-NLS-2$
    JsonValue arrv : // $NON-NLS-1$ //$NON-NLS-2$
    Json.parse(json).asArray()) {
        SrcDstResult sdr = new SrcDstResult(arrv.asObject());
        if (sdr.id == null) {
            sdrl.needSave = true;
            sdr.id = UUID.randomUUID().toString();
        }
        sdrl.add(sdr);
    }
    return sdrl;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue)

Example 62 with JsonValue

use of com.eclipsesource.json.JsonValue in project zencash-swing-wallet-ui by ZencashOfficial.

the class MessagingPanel method importContactIdentity.

/**
 * Imports a contact's identity from file.
 */
public void importContactIdentity() {
    try {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Import contact's messaging identity from file...");
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int result = fileChooser.showOpenDialog(this.parentFrame);
        if (result != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File f = fileChooser.getSelectedFile();
        JsonObject topIdentityObject = null;
        Reader r = null;
        try {
            r = new InputStreamReader(new FileInputStream(f), "UTF-8");
            topIdentityObject = Util.parseJsonObject(r);
        } finally {
            if (r != null) {
                r.close();
            }
        }
        // Validate the fields inside the objects, make sure this is indeed an identity
        // verify mandatory etc.
        JsonValue innerValue = topIdentityObject.get("zenmessagingidentity");
        JsonObject innerIdentity = (innerValue != null) ? innerValue.asObject() : null;
        if ((innerValue == null) || (innerIdentity == null) || (innerIdentity.get("nickname") == null) || (innerIdentity.get("sendreceiveaddress") == null) || (innerIdentity.get("senderidaddress") == null)) {
            JOptionPane.showMessageDialog(this.parentFrame, "The selected JSON file has a wrong format or is not a messaging identity file!", "Messaging identity has wrong format!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        MessagingIdentity contactIdentity = new MessagingIdentity(innerIdentity);
        // Search through the existing contact identities, to make sure we are not adding it a second time
        for (MessagingIdentity mi : this.messagingStorage.getContactIdentities(false)) {
            if (mi.isIdenticalTo(contactIdentity)) {
                int choice = JOptionPane.showConfirmDialog(this.parentFrame, "There is already a contact in your contact list with the same identity. \n\n" + "Existing contact identity: " + mi.getDiplayString() + "\n" + "Contact identity being imported: " + contactIdentity.getDiplayString() + "\n\n" + "Two identities are considered the same if their T/Z addresses are the same. \n" + "Do you want to replace the details of the existing messaging identity, with\n" + "the one being imported?", "The same contact identity is already available", JOptionPane.YES_NO_OPTION);
                if (choice == JOptionPane.YES_OPTION) {
                    this.messagingStorage.updateContactIdentityForSenderIDAddress(contactIdentity.getSenderidaddress(), contactIdentity);
                    JOptionPane.showMessageDialog(this.parentFrame, "Your contact's messaging identity has been successfully updated.\n", "Messaging identity is successfully updated", JOptionPane.INFORMATION_MESSAGE);
                    this.contactList.reloadMessagingIdentities();
                }
                // In any case - not a new identity to add
                return;
            }
        }
        // Check for the existence of an "Unknown" type of identity already - that could be
        // updated. Search can be done by T address only.
        MessagingIdentity existingUnknownID = this.messagingStorage.getContactIdentityForSenderIDAddress(contactIdentity.getSenderidaddress());
        if (existingUnknownID != null) {
            int choice = JOptionPane.showConfirmDialog(this.parentFrame, "There is a contact in your contact list with the same sender identification address \n" + "but with yet unknown/not yet imported full identity:\n\n" + "Existing contact identity: " + existingUnknownID.getDiplayString() + "\n" + "Contact identity being imported: " + contactIdentity.getDiplayString() + "\n\n" + "Please confirm that you want to update the details of the existing contact identity\n" + "with the one being imported?", "Contact with the same sender identification address is already available.", JOptionPane.YES_NO_OPTION);
            if (choice == JOptionPane.YES_OPTION) {
                this.messagingStorage.updateContactIdentityForSenderIDAddress(contactIdentity.getSenderidaddress(), contactIdentity);
                JOptionPane.showMessageDialog(this.parentFrame, "Your contact's messaging identity has been successfully updated.\n", "Messaging identity is successfully updated", JOptionPane.INFORMATION_MESSAGE);
                this.contactList.reloadMessagingIdentities();
            }
            return;
        }
        // Add the new identity normally!
        this.messagingStorage.addContactIdentity(contactIdentity);
        int sendIDChoice = JOptionPane.showConfirmDialog(this.parentFrame, "Your contact's messaging identity has been successfully imported: \n" + contactIdentity.getDiplayString() + "\n" + "You can now send and receive messages from this contact. Do you wish\n" + "to send a limited sub-set of your contact details to this new contact\n" + "as a special message?\n\n" + "This will allow him/her to establish contact with you without manually\n" + "importing your messaging identity (the way you imported his identity).", "Successfully imported. Send your identity over?", JOptionPane.YES_NO_OPTION);
        this.contactList.reloadMessagingIdentities();
        if (sendIDChoice == JOptionPane.YES_OPTION) {
            this.sendIdentityMessageTo(contactIdentity);
        }
    } catch (Exception ex) {
        Log.error("Unexpected error in importing contact messaging identity from file!", ex);
        this.errorReporter.reportError(ex, false);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) InputStreamReader(java.io.InputStreamReader) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileInputStream(java.io.FileInputStream) URISyntaxException(java.net.URISyntaxException) WalletCallException(com.vaklinov.zcashui.ZCashClientCaller.WalletCallException) IOException(java.io.IOException)

Example 63 with JsonValue

use of com.eclipsesource.json.JsonValue in project zencash-swing-wallet-ui by ZencashOfficial.

the class ZCashClientCaller method executeCommandAndGetJsonValue.

private JsonValue executeCommandAndGetJsonValue(String command1, String command2, String command3) throws WalletCallException, IOException, InterruptedException {
    String strResponse = this.executeCommandAndGetSingleStringResponse(command1, command2, command3);
    JsonValue response = null;
    try {
        response = Json.parse(strResponse);
    } catch (ParseException pe) {
        throw new WalletCallException(strResponse + "\n" + pe.getMessage() + "\n", pe);
    }
    return response;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) ParseException(com.eclipsesource.json.ParseException)

Example 64 with JsonValue

use of com.eclipsesource.json.JsonValue in project WereWolfPlugin by Ph1Lou.

the class LanguageManager method buildLanguageFile.

private File buildLanguageFile(Plugin plugin, String defaultLang) {
    String lang = main.getConfig().getString("lang");
    File file = new File(plugin.getDataFolder() + File.separator + "languages" + File.separator, lang + ".json");
    if (!file.exists()) {
        FileUtils_.copy(plugin.getResource(lang + ".json"), plugin.getDataFolder() + File.separator + "languages" + File.separator + lang + ".json");
    }
    if (!file.exists()) {
        FileUtils_.copy(plugin.getResource(defaultLang + ".json"), plugin.getDataFolder() + File.separator + "languages" + File.separator + defaultLang + ".json");
        return new File(plugin.getDataFolder() + File.separator + "languages" + File.separator, defaultLang + ".json");
    } else {
        String defaultText = FileUtils_.convert(plugin.getResource(defaultLang + ".json"));
        Map<String, JsonValue> fr = loadTranslations(plugin, defaultText);
        Map<String, JsonValue> custom = loadTranslations(plugin, FileUtils_.loadContent(file));
        JsonObject jsonObject = Json.parse(FileUtils_.loadContent(file)).asObject();
        for (String string : fr.keySet()) {
            if (!custom.containsKey(string)) {
                JsonObject temp = jsonObject;
                String tempString = string;
                while (temp.get(tempString.split("\\.")[0]) != null) {
                    String temp2 = tempString.split("\\.")[0];
                    tempString = tempString.replaceFirst(temp2 + "\\.", "");
                    temp = temp.get(temp2).asObject();
                }
                String[] strings = tempString.split("\\.");
                for (int i = 0; i < strings.length - 1; i++) {
                    temp.set(strings[i], new JsonObject());
                    temp = temp.get(strings[i]).asObject();
                }
                temp.set(strings[strings.length - 1], fr.get(string));
            }
        }
        FileUtils_.saveJson(file, jsonObject);
    }
    return file;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) File(java.io.File)

Example 65 with JsonValue

use of com.eclipsesource.json.JsonValue in project selenium_tests by sergueik.

the class MinimalJSONTest method readSideData.

private String readSideData(String payload, Optional<Map<String, JsonValue>> parameters, String acceptedKeys) {
    if (debug) {
        System.err.println("Accepted keys: " + acceptedKeys);
    }
    Map<String, JsonValue> collector = (parameters.isPresent()) ? parameters.get() : new HashMap<>();
    String data = (payload == null) ? "{\"foo\":\"bar\", \"result\":true,\"id\":42 }" : payload;
    if (debug) {
        // System.err.println("Processing payload: " + data.replaceAll(",",
        // ",\n"));
        System.err.println("Processing payload: [ " + data + "]");
    }
    JsonObject jsonObject = JsonObject.readFrom(data);
    assertThat(jsonObject, notNullValue());
    assertThat(jsonObject.isEmpty(), is(false));
    Iterator<Member> jsonObjectIterator = jsonObject.iterator();
    while (jsonObjectIterator.hasNext()) {
        Member jsonObjectMember = jsonObjectIterator.next();
        System.err.println("Found member: " + jsonObjectMember.getName());
        String propertyKey = jsonObjectMember.getName();
        if (!propertyKey.matches(acceptedKeys)) {
            System.err.println("Ignoring key: " + propertyKey);
            continue;
        }
        if (debug) {
            System.err.println("Processing key: " + propertyKey);
        }
        Boolean found = false;
        try {
            JsonValue propertyVal = jsonObject.get(propertyKey);
            if (debug) {
                System.err.println("Loaded string: " + propertyKey + ": " + propertyVal);
            }
            collector.put(propertyKey, propertyVal);
            found = true;
        } catch (Exception e) {
            System.err.println("Exception (ignored, continue): " + e.toString());
        }
    }
    return Integer.toString(collector.get("id").asInt());
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) Member(com.eclipsesource.json.JsonObject.Member) 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