Search in sources :

Example 46 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class StatementParser method parseStatement.

/**
     * Parses a single JSON statement. This method guarantees that exactly one JSON object
     * will be consumed.
     */
static ParsedStatement parseStatement(JsonReader reader, AbstractAsset source) throws JSONException, AssociationServiceException, IOException {
    List<Statement> statements = new ArrayList<Statement>();
    List<String> delegates = new ArrayList<String>();
    JSONObject statement = JsonParser.parse(reader);
    if (statement.optString(Utils.DELEGATE_FIELD_DELEGATE, null) != null) {
        delegates.add(statement.optString(Utils.DELEGATE_FIELD_DELEGATE));
    } else {
        JSONObject targetObject = statement.optJSONObject(Utils.ASSET_DESCRIPTOR_FIELD_TARGET);
        if (targetObject == null) {
            throw new AssociationServiceException(String.format(FIELD_NOT_STRING_FORMAT_STRING, Utils.ASSET_DESCRIPTOR_FIELD_TARGET));
        }
        JSONArray relations = statement.optJSONArray(Utils.ASSET_DESCRIPTOR_FIELD_RELATION);
        if (relations == null) {
            throw new AssociationServiceException(String.format(FIELD_NOT_ARRAY_FORMAT_STRING, Utils.ASSET_DESCRIPTOR_FIELD_RELATION));
        }
        AbstractAsset target = AssetFactory.create(targetObject);
        for (int i = 0; i < relations.length(); i++) {
            statements.add(Statement.create(source, target, Relation.create(relations.getString(i))));
        }
    }
    return new ParsedStatement(statements, delegates);
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Example 47 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class ShortcutUser method dumpCheckin.

public JSONObject dumpCheckin(boolean clear) throws JSONException {
    final JSONObject result = new JSONObject();
    result.put(KEY_USER_ID, mUserId);
    {
        final JSONArray launchers = new JSONArray();
        for (int i = 0; i < mLaunchers.size(); i++) {
            launchers.put(mLaunchers.valueAt(i).dumpCheckin(clear));
        }
        result.put(KEY_LAUNCHERS, launchers);
    }
    {
        final JSONArray packages = new JSONArray();
        for (int i = 0; i < mPackages.size(); i++) {
            packages.put(mPackages.valueAt(i).dumpCheckin(clear));
        }
        result.put(KEY_PACKAGES, packages);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 48 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class ShortcutService method dumpCheckin.

/**
     * Dumpsys for checkin.
     *
     * @param clear if true, clear the history information.  Some other system services have this
     * behavior but shortcut service doesn't for now.
     */
private void dumpCheckin(PrintWriter pw, boolean clear) {
    synchronized (mLock) {
        try {
            final JSONArray users = new JSONArray();
            for (int i = 0; i < mUsers.size(); i++) {
                users.put(mUsers.valueAt(i).dumpCheckin(clear));
            }
            final JSONObject result = new JSONObject();
            result.put(KEY_SHORTCUT, users);
            result.put(KEY_LOW_RAM, injectIsLowRamDevice());
            result.put(KEY_ICON_SIZE, mMaxIconDimension);
            pw.println(result.toString(1));
        } catch (JSONException e) {
            Slog.e(TAG, "Unable to write in json", e);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 49 with JSONArray

use of org.json.JSONArray in project Conversations by siacs.

the class Contact method deleteOtrFingerprint.

public boolean deleteOtrFingerprint(String fingerprint) {
    synchronized (this.keys) {
        boolean success = false;
        try {
            if (this.keys.has("otr_fingerprints")) {
                JSONArray newPrints = new JSONArray();
                JSONArray oldPrints = this.keys.getJSONArray("otr_fingerprints");
                for (int i = 0; i < oldPrints.length(); ++i) {
                    if (!oldPrints.getString(i).equals(fingerprint)) {
                        newPrints.put(oldPrints.getString(i));
                    } else {
                        success = true;
                    }
                }
                this.keys.put("otr_fingerprints", newPrints);
            }
            return success;
        } catch (JSONException e) {
            return false;
        }
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 50 with JSONArray

use of org.json.JSONArray in project core by s4.

the class Handshake method clientConnectCreate.

private ClientConnection clientConnectCreate(byte[] v, ByteArrayIOChannel io, Socket sock, List<String> reason) throws IOException {
    try {
        JSONObject cInfo = new JSONObject(new String(v));
        String s = cInfo.optString("uuid", "");
        if (s.isEmpty()) {
            logger.error("missing client identifier during handshake.");
            reason.add("missing UUID");
            return null;
        }
        UUID u = UUID.fromString(s);
        logger.info("connecting to client " + u);
        s = cInfo.optString("readMode", "Private");
        ClientReadMode rmode = ClientReadMode.fromString(s);
        if (rmode == null) {
            logger.error(u + ": unknown readMode " + s);
            reason.add("unknown readMode " + s);
            return null;
        }
        s = cInfo.optString("writeMode", "Enabled");
        ClientWriteMode wmode = ClientWriteMode.fromString(s);
        if (wmode == null) {
            logger.error(u + ": unknown writeMode " + s);
            reason.add("unknown writeMode " + s);
            return null;
        }
        logger.info(u + " read=" + rmode + " write=" + wmode);
        if (rmode == ClientReadMode.None && wmode == ClientWriteMode.Disabled) {
            // client cannot disable read AND write...
            logger.error("client neither reads nor writes.");
            reason.add("read and write disabled");
            return null;
        }
        ClientConnection conn = new ClientConnection(clientStub, sock, u, rmode, wmode);
        if (rmode == ClientReadMode.Select) {
            JSONArray incl = cInfo.optJSONArray("readInclude");
            JSONArray excl = cInfo.optJSONArray("readExclude");
            if (incl == null && excl == null) {
                logger.error(u + ": missing stream selection information");
                reason.add("missing readInclude and readExclude");
                return null;
            }
            if (incl != null) {
                List<String> streams = new ArrayList<String>(incl.length());
                for (int i = 0; i < incl.length(); ++i) streams.add(incl.getString(i));
                conn.includeStreams(streams);
            }
            if (excl != null) {
                List<String> streams = new ArrayList<String>(excl.length());
                for (int i = 0; i < excl.length(); ++i) streams.add(excl.getString(i));
                conn.excludeStreams(streams);
            }
        }
        return conn;
    } catch (JSONException e) {
        logger.error("malformed JSON from client during handshake", e);
        reason.add("malformed JSON");
    } catch (NumberFormatException e) {
        logger.error("received malformed UUID", e);
        reason.add("malformed UUID");
    } catch (IllegalArgumentException e) {
        logger.error("received malformed UUID", e);
        reason.add("malformed UUID");
    }
    return null;
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) UUID(java.util.UUID)

Aggregations

JSONArray (org.json.JSONArray)1710 JSONObject (org.json.JSONObject)1191 JSONException (org.json.JSONException)738 ArrayList (java.util.ArrayList)323 IOException (java.io.IOException)243 Test (org.junit.Test)207 HashMap (java.util.HashMap)108 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)96 List (java.util.List)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 File (java.io.File)54 HashSet (java.util.HashSet)54 Date (java.util.Date)47 Query (org.b3log.latke.repository.Query)47 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 GraphObject (com.abewy.android.apps.klyph.core.graph.GraphObject)43 FileNotFoundException (java.io.FileNotFoundException)40 Map (java.util.Map)40 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)36 VolleyError (com.android.volley.VolleyError)35