Example usage for org.json JSONObject putOpt

List of usage examples for org.json JSONObject putOpt

Introduction

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

Prototype

public JSONObject putOpt(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.

Usage

From source file:biblivre3.circulation.lending.LendingInfoDTO.java

@Override
public JSONObject toJSONObject(Properties properties) {
    JSONObject json = new JSONObject();

    try {/*from www  .  j a  va  2s . c  om*/
        json.put("serial", this.getSerial());
        json.put("title", this.getTitle());
        json.put("author", this.getAuthor());

        json.put("holdingSerial", this.getHoldingSerial());
        json.put("assetHolding", this.getAssetHolding());

        json.put("lent", this.isLent());
        json.putOpt("lendDate", this.getLendDate());
        json.putOpt("returnDate", this.getReturnDate());

        json.put("reserved", this.isReserved());

        json.putOpt("userSerial", this.getUserSerial());
        json.putOpt("userName", this.getUserName());

        json.putOpt("daysLate", this.getDaysLate());
        json.putOpt("fineValue", this.getFineValue());

        if (this.getMessage() != null) {
            json.put("message", I18nUtils.getText(properties, this.getMessage()));
        }
    } catch (JSONException e) {
    }

    return json;
}

From source file:com.app.swaedes.swaedes.ConstructionSitePage.java

private void makeRequest() {

    //Creating JSON Object where data will be stored
    JSONObject feature = new JSONObject();
    JSONObject site = new JSONObject();

    try {/*from   w ww.j a v  a  2  s . com*/

        //if the user made an audio comment, it will be put with the rest of the data
        if (bool_audio) {
            feature.put(Config.KEY_AUDIO, encoded_string_audio);
            feature.put(Config.KEY_AUDIO_NAME, audio_name);
        }

        //Storing data in JSON Objects
        feature.put(Config.KEY_FLOORS, nb_floors);
        feature.put(Config.KEY_WATER, water);
        feature.put(Config.KEY_LARVAE, larvae);
        feature.put(Config.KEY_COMMENT, comment);
        feature.put(Config.KEY_CLOTHES, nb_clothes);
        feature.put(Config.KEY_BOXES, nb_boxes);
        feature.put(Config.KEY_WORKERS, workers);
        feature.put(Config.KEY_LATITUDE, latitude);
        feature.put(Config.KEY_LONGITUDE, longitude);
        feature.put(Config.KEY_IMAGE, encoded_string_image);
        feature.put(Config.KEY_USER, user);
        feature.put(Config.KEY_IMAGE_NAME, image_name);

        site.put(Config.KEY_ADDRESS, address);
        site.put(Config.KEY_SITE_NAME, name);
        site.put(Config.KEY_BOOL_CONSTRUCTION_SITE, true);
        site.put(Config.KEY_SITE_TYPE, keyword);
        site.put(Config.KEY_STATE, state);

        feature.putOpt(Config.KEY_SITE, site);

        Log.d("Swaedes", feature.toString());

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, Config.UPLOAD_URL, feature,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response.toString().contains(Config.UPLOAD_SUCCESS)) {

                            //If response contains success, go to Main Menu
                            Intent intent = new Intent(ConstructionSitePage.this, MainMenuPage.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(ConstructionSitePage.this, "Error, try again", Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            //Set the request headers
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };

        //Getting the request queue of the app, add the request to it
        Swaedes swaedes = (Swaedes) getApplication();
        RequestQueue mVolleyRequestQueue = swaedes.getVolleyRequestQueue();
        mVolleyRequestQueue.add(request);

    } catch (JSONException e) {
        Log.e("Swaedes", "Could not parse malformed JSON: \"" + feature + "\"");
    }
}

From source file:edu.umass.cs.protocoltask.json.ProtocolPacket.java

public JSONObject toJSONObject() throws JSONException {
    JSONObject json = toJSONObjectImpl();
    // Packet.putPacketType(json, this.getType());
    this.putPacketType(json, getType());
    json.putOpt(Keys.SENDER.toString(), this.sender);
    json.putOpt(Keys.INITIATOR.toString(), this.initiator);
    json.putOpt(Keys.KEY.toString(), this.key);
    return json;// w w  w .  j a v  a2s  .  c o  m
}

From source file:kr.ac.cau.mecs.cass.processor.SigninProcessor.java

@Override
public Signal process(Signal signal) {
    Signal resignal = new Signal();

    resignal.setReceiver(signal.getSender());
    resignal.setSender("CASS");
    resignal.setAction(new Action(Action.ACT_SIGNIN));

    if (signal.getPayload() != null && (signal.getPayload().getPayload() instanceof JSONObjectPayload)) {
        JSONObject jobj = (JSONObject) signal.getPayload().getPayload().getData();

        if (jobj.has("userid") && jobj.has("userpw")) {
            String userid = jobj.optString("userid");
            String userpw = jobj.optString("userpw");
            //valid payload

            DBUserEntity _user = UserEntityDAO.getByUserID(session, userid);

            if (_user != null && _user.getPassword().equals(userpw)) {
                //valid credential...
                JSONObject jres = new JSONObject();

                if (_user.getAccessToken() == null) {
                    System.out.println("new token");
                    _user.setAccessToken(new DBAccessTokenEntity());
                    _user.getAccessToken().setUser(_user);
                    session.save(_user.getAccessToken());
                }//from ww w .  j  a  v a  2  s.c  o  m

                KeyPair keypair = AccessTokenUtil.generateKeyPair(System.currentTimeMillis());

                String usertoken = BCrypt.hashpw(userid, BCrypt.gensalt(12));
                String authtoken = AccessTokenUtil.signData(usertoken, keypair.getPrivate());

                _user.getAccessToken().setPrivateKey(AccessTokenUtil.encodePrivateKey(keypair.getPrivate()));
                _user.getAccessToken().setPublicKey(AccessTokenUtil.encodePublicKey(keypair.getPublic()));
                _user.getAccessToken().setAccessToken(authtoken);
                _user.getAccessToken().setUserToken(usertoken);

                jres.putOpt("authToken", authtoken);
                jres.putOpt("userToken", usertoken);

                session.update(_user);

                resignal.setPayload(new Payload(new JSONObjectPayload(jres)));

            } else {
                setGenericMessage(resignal, "invalid credential");
            }

        } else {
            setGenericMessage(resignal, "invalid payload type");
        }
    } else {
        //inform user invalid payload type
        setGenericMessage(resignal, "invalid payload type");
    }

    return resignal;
}

From source file:kr.ac.cau.mecs.cass.processor.SignupProcessor.java

@Override
public Signal process(Signal signal) {
    Signal resignal = new Signal();

    resignal.setReceiver(signal.getSender());
    resignal.setSender("CASS");
    resignal.setAction(new Action(Action.ACT_SIGNUP));

    if (signal.getPayload() != null && (signal.getPayload().getPayload() instanceof JSONObjectPayload)) {
        JSONObject jobj = (JSONObject) signal.getPayload().getPayload().getData();

        if (jobj.has("userid") && jobj.has("userpw")) {
            String userid = jobj.optString("userid");
            String userpw = jobj.optString("userpw");
            //valid payload

            DBUserEntity _user = UserEntityDAO.getByUserID(session, userid);

            if (_user != null) {
                //user exists
                setGenericMessage(resignal, "user exists");
            } else {
                if (userid.length() > 4) {
                    if (userpw.length() > 4) {
                        //create here
                        _user = new DBUserEntity();
                        _user.setName(userid);
                        _user.setPassword(userpw);

                        if (_user.getAccessToken() == null) {
                            _user.setAccessToken(new DBAccessTokenEntity());
                            _user.getAccessToken().setUser(_user);
                        }/*  w  w  w .j  a v a2s.  co  m*/

                        KeyPair keypair = AccessTokenUtil.generateKeyPair(System.currentTimeMillis());

                        String usertoken = BCrypt.hashpw(userid, BCrypt.gensalt(12));
                        String authtoken = AccessTokenUtil.signData(usertoken, keypair.getPrivate());

                        _user.getAccessToken()
                                .setPrivateKey(AccessTokenUtil.encodePrivateKey(keypair.getPrivate()));
                        _user.getAccessToken()
                                .setPublicKey(AccessTokenUtil.encodePublicKey(keypair.getPublic()));
                        _user.getAccessToken().setAccessToken(authtoken);
                        _user.getAccessToken().setUserToken(usertoken);

                        session.save(_user);
                        session.saveOrUpdate(_user.getAccessToken());

                        JSONObject jres = new JSONObject();
                        jres.putOpt("authToken", authtoken);
                        jres.putOpt("userToken", usertoken);

                        resignal.setPayload(new Payload(new JSONObjectPayload(jres)));
                    } else {
                        setGenericMessage(resignal, "pw too short(min 5)");
                    }
                } else {
                    setGenericMessage(resignal, "id too short(min 5)");
                }
            }

        } else {
            setGenericMessage(resignal, "invalid payload type");
        }
    } else {
        //inform user invalid payload type
        setGenericMessage(resignal, "invalid payload type");
    }

    return resignal;
}