Example usage for org.json JSONObject NULL

List of usage examples for org.json JSONObject NULL

Introduction

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

Prototype

Object NULL

To view the source code for org.json JSONObject NULL.

Click Source Link

Document

It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value.

Usage

From source file:cz.vse.fis.keg.entityclassifier.exporter.JSONExporter.java

public String toJSON(List<Entity> entities) {
    String jsonResult = "";
    try {/*from w ww . j  a  v  a  2  s  . c om*/

        JSONArray jsonEntities = new JSONArray();

        for (Entity e : entities) {

            JSONObject jsonE = new JSONObject();
            jsonE.put("entityType", e.getEntityType());
            jsonE.put("underlyingString", e.getUnderlyingString());
            jsonE.put("startOffset", e.getStartOffset());
            jsonE.put("endOffset", e.getEndOffset());
            ArrayList<Type> types = e.getTypes();

            JSONArray typesJ = new JSONArray();

            if (types != null) {
                for (Type t : types) {

                    JSONObject typeJ = new JSONObject();

                    String tLabel = t.getTypeLabel();
                    if (tLabel != null) {
                        typeJ.put("typeLabel", t.getTypeLabel());
                    } else {
                        String tmp = null;
                        typeJ.put("typeLabel", JSONObject.NULL);
                    }

                    String tURI = t.getTypeURI();
                    if (tURI != null) {
                        typeJ.put("typeURI", t.getTypeURI());
                    } else {
                        String tmp = null;
                        typeJ.put("typeURI", JSONObject.NULL);
                    }

                    typeJ.put("entityLabel", t.getEntityLabel());
                    typeJ.put("entityURI", t.getEntityURI());

                    Confidence classificationConf = t.getClassificationConfidence();

                    if (classificationConf != null) {

                        JSONObject confValueJ = new JSONObject();
                        confValueJ.put("value", classificationConf.getValue());

                        if (classificationConf.getType() != null) {
                            confValueJ.put("type", classificationConf.getType());
                        } else {
                            confValueJ.put("type", "classification");
                        }
                        typeJ.put("classificationConfidence", confValueJ);
                    } else {
                        JSONObject confValueJ = new JSONObject();
                        confValueJ.put("value", -1);
                        confValueJ.put("type", "classification");
                        typeJ.put("classificationConfidence", confValueJ);
                    }

                    // create element linking confidence
                    Confidence linkingConf = t.getLinkingConfidence();
                    if (linkingConf != null) {
                        JSONObject linkValueJ = new JSONObject();
                        linkValueJ.put("value", linkingConf.getValue());
                        if (linkingConf.getType() != null) {
                            linkValueJ.put("type", linkingConf.getType());
                        }
                        typeJ.put("linkingConfidence", linkValueJ);
                    } else {
                        JSONObject linkValueJ = new JSONObject();
                        linkValueJ.put("value", -1);
                        linkValueJ.put("type", "linking");
                        typeJ.put("linkingConfidence", linkValueJ);
                    }

                    Salience s = t.getSalience();
                    if (s != null) {
                        JSONObject salienceJ = new JSONObject();
                        salienceJ.put("score", s.getScore());
                        salienceJ.put("confidence", s.getConfidence());
                        salienceJ.put("classLabel", s.getClassLabel());
                        typeJ.put("salience", salienceJ);
                    }

                    typeJ.put("provenance", t.getProvenance());
                    typesJ.put(typeJ);
                }
                jsonE.put("types", typesJ);
            }
            jsonEntities.put(jsonE);
        }

        jsonResult = jsonEntities.toString();
        return jsonResult;

    } catch (Exception ex) {
        Logger.getLogger(JSONExporter.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
    }
    return "problem";
}

From source file:io.selendroid.SelendroidCapabilities.java

public String getSerial() {
    if (getRawCapabilities().get(SERIAL) == null || getRawCapabilities().get(SERIAL).equals(JSONObject.NULL))
        return null;
    return (String) getRawCapabilities().get(SERIAL);
}

From source file:io.selendroid.SelendroidCapabilities.java

public Boolean getEmulator() {
    if (getRawCapabilities().get(EMULATOR) == null
            || getRawCapabilities().get(EMULATOR).equals(JSONObject.NULL))
        return null;
    return (Boolean) getRawCapabilities().get(EMULATOR);
}

From source file:run.ace.Utils.java

public static Object[] convertJSONArrayToArray(JSONArray array) throws JSONException {
    int length = array.length();
    Object[] args = new Object[length];
    for (int i = 0; i < length; i++) {
        Object arg = array.get(i);
        // Convert non-array non-primitives
        if (arg instanceof JSONObject) {
            arg = Utils.deserializeObjectOrStruct((JSONObject) arg);
        }/*  w  w w  .  j  a v  a 2  s.  c  o  m*/
        // null appears as a special object
        else if (arg == JSONObject.NULL) {
            arg = null;
        }
        // Arrays appear as JSONArray
        else if (arg instanceof JSONArray) {

            JSONArray a = (JSONArray) arg;
            Object[] newArray = new Object[a.length()];
            for (int j = 0; j < newArray.length; j++) {
                Object item = a.get(j);
                if (item instanceof JSONObject) {
                    newArray[j] = Utils.deserializeObjectOrStruct((JSONObject) item);
                } else if (item == JSONObject.NULL) {
                    newArray[j] = null;
                } else {
                    newArray[j] = item;
                }
            }

            // Make sure it's the right type of array
            if (true) { // TODO: parameterTypes[i] == (long[]).getClass()) {
                long[] typedArray = new long[newArray.length];
                for (int j = 0; j < newArray.length; j++) {
                    typedArray[j] = (long) (int) (Integer) newArray[j];
                }
                arg = typedArray;
            } else {
                arg = newArray;
            }
        }
        args[i] = arg;
    }
    return args;
}

From source file:org.protorabbit.json.JSONUtil.java

@SuppressWarnings("unchecked")
public static Object cloneJSON(Object target) {

    if (target == null)
        return JSONObject.NULL;

    if (target instanceof JSONObject) {
        Object o = null;// ww w .java 2s .co  m
        o = new JSONObject();
        JSONObject jo = (JSONObject) target;
        Iterator<String> it = jo.keys();
        while (it.hasNext()) {
            String key = it.next();
            try {
                ((JSONObject) o).put(key, cloneJSON(jo.get(key)));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return o;
    } else if (target instanceof JSONArray) {
        Object o = new JSONArray();
        JSONArray ja = (JSONArray) target;
        int len = ja.length();
        for (int i = 0; i < len; i++) {
            try {
                ((JSONArray) o).put(cloneJSON(ja.get(i)));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    } else if (target instanceof Long) {
        return new Long(((Long) target).longValue());
    } else if (target instanceof Double) {
        return new Double(((Double) target).doubleValue());
    } else if (target instanceof Integer) {
        return new Integer(((Integer) target).intValue());
    } else if (target instanceof Boolean) {
        return new Boolean(((Boolean) target).booleanValue());
    }
    return target;
}

From source file:com.yoloci.fileupload.BundleJSONConverter.java

public static Bundle convertToBundle(JSONObject jsonObject) throws JSONException {
    Bundle bundle = new Bundle();
    @SuppressWarnings("unchecked")
    Iterator<String> jsonIterator = jsonObject.keys();
    while (jsonIterator.hasNext()) {
        String key = jsonIterator.next();
        Object value = jsonObject.get(key);
        if (value == null || value == JSONObject.NULL) {
            // Null is not supported.
            continue;
        }// ww w  .  ja va  2s .  c o  m

        // Special case JSONObject as it's one way, on the return it would be Bundle.
        if (value instanceof JSONObject) {
            bundle.putBundle(key, convertToBundle((JSONObject) value));
            continue;
        }

        Setter setter = SETTERS.get(value.getClass());
        if (setter == null) {
            throw new IllegalArgumentException("Unsupported type: " + value.getClass());
        }
        setter.setOnBundle(bundle, key, value);
    }

    return bundle;
}

From source file:com.vk.sdk.util.VKJsonHelper.java

/**
 * Converts object from json to java object
 *
 * @param json object from jsonobject or jsonarray
 * @return converted object//www .java  2 s .  c o m
 * @throws JSONException
 */
private static Object fromJson(Object json) throws JSONException {
    if (json == JSONObject.NULL) {
        return null;
    } else if (json instanceof JSONObject) {
        return toMap((JSONObject) json);
    } else if (json instanceof JSONArray) {
        return toList((JSONArray) json);
    } else {
        return json;
    }
}

From source file:com.tenduke.example.webadmin.graph.GraphOperationSpec.java

/**
 * Sets up operation spec JSON./*from   ww w.ja va 2s  .  c  o m*/
 * @return This operation specification as a JSON object.
 */
public JSONObject toJson() {
    //
    final JSONObject retValue = new JSONObject();
    retValue.put("operation", getOperation());
    retValue.put("method", getMethod() == null ? "GET" : getMethod());
    retValue.put("selectorTemplate", getSelectorTemplate() == null ? JSONObject.NULL : getSelectorTemplate());
    //
    JSONArray params = new JSONArray();
    parameters.entrySet().stream().forEach(
            p -> params.put(new JSONObject().put("name", p.getKey()).put("description", p.getValue())));
    retValue.put("parameters", params);
    //
    JSONArray templates = new JSONArray();
    graphTemplates.entrySet().stream().forEach(
            t -> templates.put(new JSONObject().put("name", t.getKey()).put("template", t.getValue())));
    retValue.put("graphTemplates", templates);
    //
    return retValue;
}

From source file:com.clover.sdk.v3.JsonParcelHelper.java

private static void writeValue(Parcel out, int flags, Object v) {
    if (v == null || v == JSONObject.NULL) {
        out.writeInt(VAL_NULL);//from   w w  w .j a  va  2  s . c om
    } else {
        Class<?> c = v.getClass();
        if (c == String.class) {
            out.writeInt(VAL_STRING);
            out.writeString((String) v);
        } else if (c == Long.class) {
            out.writeInt(VAL_LONG);
            out.writeLong((Long) v);
        } else if (c == Boolean.class) {
            out.writeInt(VAL_BOOLEAN);
            out.writeInt((Boolean) v ? 1 : 0);
        } else if (c == JSONObject.class) {
            out.writeInt(VAL_MAP);
            wrap((JSONObject) v).writeToParcel(out, flags);
        } else if (c == JSONArray.class) {
            out.writeInt(VAL_OBJECTARRAY);
            wrap((JSONArray) v).writeToParcel(out, flags);
        } else if (c == Double.class) {
            out.writeInt(VAL_DOUBLE);
            out.writeDouble((Double) v);
        } else if (c == Integer.class) {
            out.writeInt(VAL_INTEGER);
            out.writeInt((Integer) v);
        } else if (c == Float.class) {
            out.writeInt(VAL_FLOAT);
            out.writeFloat((Float) v);
        } else {
            throw new RuntimeException("Json: unable to marshal value " + v);
        }
    }
}

From source file:com.clover.sdk.v3.JsonParcelHelper.java

private static Object readValue(Parcel in) {
    int type = in.readInt();

    switch (type) {
    case VAL_NULL:
        return JSONObject.NULL;

    case VAL_STRING:
        return in.readString();

    case VAL_INTEGER:
        return in.readInt();

    case VAL_MAP:
        return ObjectWrapper.CREATOR.createFromParcel(in).unwrap();

    case VAL_LONG:
        return in.readLong();

    case VAL_FLOAT:
        return in.readFloat();

    case VAL_DOUBLE:
        return in.readDouble();

    case VAL_BOOLEAN:
        return in.readInt() != 0;

    case VAL_OBJECTARRAY:
        return ArrayWrapper.CREATOR.createFromParcel(in).unwrap();

    default:/*from  w ww .  j a v  a2  s  .  c  o  m*/
        int off = in.dataPosition() - 4;
        throw new IllegalArgumentException(
                "Json: unmarshalling unknown type code " + type + " at offset " + off);
    }
}