Example usage for org.json JSONObject opt

List of usage examples for org.json JSONObject opt

Introduction

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

Prototype

public Object opt(String key) 

Source Link

Document

Get an optional value associated with a key.

Usage

From source file:com.google.blockly.model.Input.java

/**
 * Gets a list of connection checks from JSON. If json does not contain a 'check' field
 * null will be returned instead./*from   w  w w . j a v a 2s  .c om*/
 *
 * @param json The JSON to extract the connection checks from.
 * @param checksKey The key for the checks.
 *
 * @return The set of checks or null.
 */
@Nullable
public static String[] getChecksFromJson(JSONObject json, String checksKey) {
    Object checkObj = json.opt(checksKey);
    String[] checks = null;
    if (checkObj == null) {
        // Do nothing, ignore other checks
    } else if (checkObj instanceof JSONArray) {
        JSONArray jsonChecks = (JSONArray) checkObj;
        if (jsonChecks != null) {
            int count = jsonChecks.length();
            checks = new String[count];
            for (int i = 0; i < count; i++) {
                checks[i] = jsonChecks.optString(i);
                if (checks[i] == null) {
                    throw new IllegalArgumentException("Malformatted check array in Input.");
                }
            }
        }
    } else if (checkObj instanceof String) {
        checks = new String[] { (String) checkObj };
    }
    return checks;
}

From source file:com.rapid.actions.Validation.java

@Override
public String getJavaScript(RapidRequest rapidRequest, Application application, Page page, Control control,
        JSONObject jsonDetails) throws Exception {

    String js = "";

    // check we have some controls
    if (_controls != null) {
        // the validations is a JavaScript array
        JSONArray jsonValidations = new JSONArray();
        // loop the controls (which are actually controlIds)
        for (String controlId : _controls) {
            // find the control proper
            Control validateControl = page.getControl(controlId);
            // check we have a control
            if (validateControl != null) {
                // get the control validation
                com.rapid.core.Validation controlValidation = validateControl.getValidation();
                // check there is control validation
                if (controlValidation != null) {

                    // find the control class
                    JSONObject jsonControl = rapidRequest.getRapidServlet()
                            .getJsonControl(validateControl.getType());

                    // check a type has been specified and we could find it
                    if (jsonControl != null && !"".equals(controlValidation.getType())) {
                        // this method can't throw so catch here               
                        try {

                            // build the validations object
                            JSONObject jsonValidation = new JSONObject();

                            // add the properties                  
                            jsonValidation.put("controlId", validateControl.getId());
                            jsonValidation.put("controlType", validateControl.getType());
                            jsonValidation.put("validationType", controlValidation.getType());
                            jsonValidation.put("message", controlValidation.getMessage());
                            jsonValidation.put("validationProperty", jsonControl.opt("validationProperty"));
                            if (!"".equals(controlValidation.getRegEx()))
                                jsonValidation.put("regEx", controlValidation.getRegEx());
                            if (!"".equals(controlValidation.getJavaScript()))
                                jsonValidation.put("javaScript", controlValidation.getJavaScript());
                            if (controlValidation.getPassHidden())
                                jsonValidation.put("passHidden", true);
                            if (controlValidation.getAllowNulls())
                                jsonValidation.put("allowNulls", true);

                            // add optional properties
                            if (validateControl.getDetails() != null)
                                jsonValidation.put("details",
                                        validateControl.getDetailsJavaScript(application, page));

                            // add it to the array
                            jsonValidations.put(jsonValidation);

                        } catch (JSONException ex) {
                        }//from w  w  w  .j ava 2s  .c  o m
                    }
                }
            }
        }
        js = "if (Action_validation(ev, " + jsonValidations.toString() + "," + getProperty("showMessages")
                + ")) {\n";

        // insert pass actions
        if (_passActions != null) {
            for (Action action : _passActions)
                js += "  " + action.getJavaScript(rapidRequest, application, page, control, jsonDetails) + "\n";
        }

        js += "} else {\n";

        // insert fail actions
        if (_failActions != null) {
            for (Action action : _failActions)
                js += "  " + action.getJavaScript(rapidRequest, application, page, control, jsonDetails) + "\n";
        }

        // check whether to stop further actions
        boolean stopActions = Boolean.parseBoolean(getProperty("stopActions"));
        if (stopActions)
            js += "  return false;\n";

        js += "}\n";
    }

    return js;

}

From source file:com.liferay.mobile.android.http.client.OkHttpClientImpl.java

protected RequestBody getUploadBody(Request request) {
    JSONObject body = (JSONObject) request.getBody();
    Object tag = request.getTag();

    MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);

    Iterator<String> it = body.keys();

    while (it.hasNext()) {
        String key = it.next();//  www .  ja  va2s  .  c  om
        Object value = body.opt(key);

        if (value instanceof UploadData) {
            UploadData data = (UploadData) value;
            RequestBody requestBody = new InputStreamBody(data, tag);
            builder.addFormDataPart(key, data.getFileName(), requestBody);
        } else {
            builder.addFormDataPart(key, value.toString());
        }
    }

    return builder.build();
}

From source file:com.facebook.stetho.json.ObjectMapper.java

private <T> T _convertFromJSONObject(JSONObject jsonObject, Class<T> type) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException, InstantiationException, JSONException {
    Constructor<T> constructor = type.getDeclaredConstructor((Class[]) null);
    constructor.setAccessible(true);/*w ww . j ava  2 s.  com*/
    T instance = constructor.newInstance();
    Field[] fields = type.getFields();
    for (int i = 0; i < fields.length; ++i) {
        Field field = fields[i];
        Object value = jsonObject.opt(field.getName());
        Object setValue = getValueForField(field, value);
        try {
            field.set(instance, getValueForField(field, value));
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Class: " + type.getSimpleName() + " " + "Field: "
                    + field.getName() + " type " + setValue.getClass().getName(), e);
        }
    }
    return instance;
}

From source file:com.zotoh.core.util.JSONUte.java

/**
 * @param j//from   www  .java  2s  .c o m
 * @param fld
 * @return
 * @throws JSONException
 */
public static JSONArray getAndSetArray(JSONObject j, String fld) throws JSONException {
    JSONArray r = null;
    Object o;

    if (j != null && fld != null) {
        o = j.opt(fld);
        if (o instanceof JSONArray) {
            r = (JSONArray) o;
        } else if (o != null) {
            j.remove(fld);
        }

        if (r == null) {
            r = new JSONArray();
            j.put(fld, r);
        }
    }

    return r;
}

From source file:com.zotoh.core.util.JSONUte.java

/**
 * @param j/*from   w  ww .  j a v a  2s . c o  m*/
 * @param fld
 * @return
 * @throws JSONException
 */
public static JSONObject getAndSetObject(JSONObject j, String fld) throws JSONException {
    JSONObject r = null;
    Object o;

    if (j != null && fld != null) {
        o = j.opt(fld);
        if (o instanceof JSONObject) {
            r = (JSONObject) o;
        } else if (o != null) {
            j.remove(fld);
        }

        if (r == null) {
            r = new JSONObject();
            j.put(fld, r);
        }
    }

    return r;
}