Example usage for org.json JSONObject equals

List of usage examples for org.json JSONObject equals

Introduction

In this page you can find the example usage for org.json JSONObject equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:config.Manejador.java

public boolean update(JSONObject criteria, JSONObject changes) throws JSONException, IOException {
    JSONObject js_obj = new JSONObject(this.src_archivo);
    JSONArray js_array = (JSONArray) js_obj.get(this.nom_coleccion);

    boolean retorno = false;

    JSONObject busqueda = this.Select(criteria);

    if (!busqueda.equals(this.dont_exists)) {
        Iterator it_changes = changes.keys();
        while (it_changes.hasNext()) {
            String key_c = it_changes.next().toString();
            busqueda.put(key_c, changes.get(key_c));
        }//from w  w w  .j  av a 2s .  co m

        int idx = busqueda.getInt("idx");
        busqueda.remove("idx");

        js_obj.put(this.nom_coleccion, js_array.put(idx, busqueda));
        retorno = true;
    }

    this.src_archivo = js_obj.toString(4);
    this.saveFile();
    return retorno;
}

From source file:config.Manejador.java

public boolean delete(JSONObject criteria) throws JSONException, IOException {
    boolean retorno = false;

    JSONObject js_obj = new JSONObject(this.src_archivo);
    JSONArray js_array = (JSONArray) js_obj.get(this.nom_coleccion);

    JSONObject busqueda = this.Select(criteria);

    if (!busqueda.equals(this.dont_exists)) {
        System.out.println(busqueda.toString(4));

        int indice = busqueda.getInt("idx");
        System.out.println(indice);

        js_array.remove(indice);/*from   www .j  a va2  s.c o  m*/
        js_obj.put(this.nom_coleccion, js_array);

        retorno = true;

        this.src_archivo = js_obj.toString(4);
        this.saveFile();
    }

    return retorno;
}

From source file:com.example.testplayer.NetworkManager.java

/**
 * Updates the JavaScript side whenever the connection changes
 *
 * @param info the current active network info
 * @return//  w ww .j a  v a  2 s.  c  o  m
 */
private void updateConnectionInfo(NetworkInfo info) {
    // send update to javascript "navigator.network.connection"
    // Jellybean sends its own info
    JSONObject thisInfo = this.getConnectionInfo(info);
    if (!thisInfo.equals(lastInfo)) {
        String connectionType = "";
        try {
            connectionType = thisInfo.get("type").toString();
        } catch (JSONException e) {
        }

        sendUpdate(connectionType);
        lastInfo = thisInfo;
    }
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

public void remField(JSONObject m, JSONObject field) {
    mCol.modSchema();/*  ww  w  .  ja va 2s. co m*/
    try {
        JSONArray ja = m.getJSONArray("flds");
        JSONArray ja2 = new JSONArray();
        int idx = -1;
        for (int i = 0; i < ja.length(); ++i) {
            if (field.equals(ja.getJSONObject(i))) {
                idx = i;
                continue;
            }
            ja2.put(ja.get(i));
        }
        m.put("flds", ja2);
        int sortf = m.getInt("sortf");
        if (sortf >= m.getJSONArray("flds").length()) {
            m.put("sortf", sortf - 1);
        }
        _updateFieldOrds(m);
        _transformFields(m, new TransformFieldDelete(idx));
        if (idx == sortIdx(m)) {
            // need to rebuild
            mCol.updateFieldCache(Utils.toPrimitive(nids(m)));
        }
        renameField(m, field, null);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.hichinaschool.flashcards.libanki.Models.java

public void moveField(JSONObject m, JSONObject field, int idx) {
    mCol.modSchema();//from w ww  .j  av  a2s . c o  m
    try {
        JSONArray ja = m.getJSONArray("flds");
        ArrayList<JSONObject> l = new ArrayList<JSONObject>();
        int oldidx = -1;
        for (int i = 0; i < ja.length(); ++i) {
            l.add(ja.getJSONObject(i));
            if (field.equals(ja.getJSONObject(i))) {
                oldidx = i;
                if (idx == oldidx) {
                    return;
                }
            }
        }
        // remember old sort field
        String sortf = Utils.jsonToString(m.getJSONArray("flds").getJSONObject(m.getInt("sortf")));
        // move
        l.remove(oldidx);
        l.add(idx, field);
        m.put("flds", new JSONArray(l));
        // restore sort field
        ja = m.getJSONArray("flds");
        for (int i = 0; i < ja.length(); ++i) {
            if (Utils.jsonToString(ja.getJSONObject(i)).equals(sortf)) {
                m.put("sortf", i);
                break;
            }
        }
        _updateFieldOrds(m);
        save(m);
        _transformFields(m, new TransformFieldMove(idx, oldidx));
        renameField(m, field, null);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/**
 * Removing a template// w  w  w. jav  a  2 s . c  om
 * 
 * @return False if removing template would leave orphan notes.
 */
public boolean remTemplate(JSONObject m, JSONObject template) {
    try {
        assert (m.getJSONArray("tmpls").length() > 1);
        // find cards using this template
        JSONArray ja = m.getJSONArray("tmpls");
        int ord = -1;
        for (int i = 0; i < ja.length(); ++i) {
            if (ja.get(i).equals(template)) {
                ord = i;
                break;
            }
        }
        String sql = "select c.id from cards c, notes f where c.nid=f.id and mid = " + m.getLong("id")
                + " and ord = " + ord;
        long[] cids = Utils.toPrimitive(mCol.getDb().queryColumn(Long.class, sql, 0));
        // all notes with this template must have at least two cards, or we could end up creating orphaned notes
        sql = "select nid, count() from cards where nid in (select nid from cards where id in "
                + Utils.ids2str(cids) + ") group by nid having count() < 2 limit 1";
        if (mCol.getDb().queryScalar(sql, false) != 0) {
            return false;
        }
        // ok to proceed; remove cards
        mCol.modSchema();
        mCol.remCards(cids);
        // shift ordinals
        mCol.getDb().execute(
                "update cards set ord = ord - 1, usn = ?, mod = ? where nid in (select id from notes where mid = ?) and ord > ?",
                new Object[] { mCol.usn(), Utils.intNow(), m.getLong("id"), ord });
        JSONArray tmpls = m.getJSONArray("tmpls");
        JSONArray ja2 = new JSONArray();
        for (int i = 0; i < tmpls.length(); ++i) {
            if (template.equals(tmpls.getJSONObject(i))) {
                continue;
            }
            ja2.put(tmpls.get(i));
        }
        m.put("tmpls", ja2);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    _updateTemplOrds(m);
    save(m);
    return true;
}