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:com.microsoft.live.unittest.PostTest.java

@Override
protected void checkValidResponseBody(LiveOperation operation) throws JSONException {
    JSONObject result = operation.getResult();
    String id = result.getString(JsonKeys.ID);
    Object description = result.get(JsonKeys.DESCRIPTION);
    String name = result.getString(JsonKeys.NAME);
    String permissions = result.getString(JsonKeys.PERMISSIONS);
    boolean isDefault = result.getBoolean(JsonKeys.IS_DEFAULT);

    JSONObject from = result.getJSONObject(JsonKeys.FROM);
    String fromId = from.getString(JsonKeys.ID);
    String fromName = from.getString(JsonKeys.NAME);

    Object subscriptionLocation = result.get(JsonKeys.SUBSCRIPTION_LOCATION);
    String createdTime = result.getString(JsonKeys.CREATED_TIME);
    String updatedTime = result.getString(JsonKeys.UPDATED_TIME);

    assertEquals("calendar_id", id);
    assertEquals(JSONObject.NULL, description);
    assertEquals("name", name);
    assertEquals("owner", permissions);
    assertEquals(false, isDefault);/*from w w w .jav a 2  s.  c o m*/
    assertEquals("from_id", fromId);
    assertEquals("from_name", fromName);
    assertEquals(JSONObject.NULL, subscriptionLocation);
    assertEquals("2011-12-10T02:48:33+0000", createdTime);
    assertEquals("2011-12-10T02:48:33+0000", updatedTime);
}

From source file:com.microsoft.live.unittest.PostTest.java

@Override
protected void loadValidResponseBody() throws JSONException {
    JSONObject calendar = new JSONObject();
    calendar.put(JsonKeys.ID, "calendar_id");
    calendar.put(JsonKeys.DESCRIPTION, JSONObject.NULL);
    calendar.put(JsonKeys.NAME, "name");
    calendar.put(JsonKeys.PERMISSIONS, "owner");
    calendar.put(JsonKeys.IS_DEFAULT, false);

    JSONObject from = new JSONObject();
    from.put(JsonKeys.ID, "from_id");
    from.put(JsonKeys.NAME, "from_name");

    calendar.put(JsonKeys.FROM, from);/*w  w  w . j  av a  2s . c om*/
    calendar.put(JsonKeys.SUBSCRIPTION_LOCATION, JSONObject.NULL);
    calendar.put(JsonKeys.CREATED_TIME, "2011-12-10T02:48:33+0000");
    calendar.put(JsonKeys.UPDATED_TIME, "2011-12-10T02:48:33+0000");

    byte[] bytes = calendar.toString().getBytes();
    this.mockEntity.setInputStream(new ByteArrayInputStream(bytes));
}

From source file:org.uiautomation.ios.server.servlet.IOSServlet.java

private JSONObject serializeException(Throwable e) throws JSONException {
    JSONObject res = new JSONObject();
    res.put("message", e.getMessage());
    res.put("class", e.getClass().getCanonicalName());
    res.put("screen", JSONObject.NULL);
    res.put("stackTrace", serializeStackTrace(e.getStackTrace()));
    if (e.getCause() != null) {
        res.put("cause", serializeException(e.getCause()));
    }/*w ww .  ja  va  2s . c om*/
    return res;

}

From source file:com.whizzosoftware.hobson.dto.property.PropertyContainerDTO.java

private PropertyContainerDTO(JSONObject json) {
    if (json.has(JSONAttributes.CCLASS)) {
        containerClass = new PropertyContainerClassDTO.Builder(json.getJSONObject(JSONAttributes.CCLASS))
                .build();//from   w  w w  .ja v  a 2 s.c o m
    }
    if (json.has(JSONAttributes.VALUES)) {
        values = new HashMap<>();
        JSONObject jp = json.getJSONObject(JSONAttributes.VALUES);
        for (Object o : jp.keySet()) {
            String key = o.toString();
            Object v = jp.get(key);
            if (JSONObject.NULL.equals(v)) {
                values.put(key, null);
            } else if (v instanceof Serializable || v instanceof JSONArray || v instanceof JSONObject) {
                values.put(key, v);
            } else {
                throw new HobsonRuntimeException("Invalid property value for " + key + ": " + v);
            }
        }
    }
}

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

public Object serialize(Object o) {

    // null is null
    if (o == null) {
        return JSONObject.NULL;
    }//from   w  ww.  j  a va 2s. c  om

    // collections
    if (Collection.class.isAssignableFrom(o.getClass())) {
        Iterator<?> it = ((Collection<?>) o).iterator();

        JSONArray ja = new JSONArray();
        while (it.hasNext()) {

            Object i = serialize(it.next());
            ja.put(i);
        }
        return ja;
    }

    // maps
    if (Map.class.isAssignableFrom(o.getClass())) {
        JSONObject jo = new JSONObject();
        Map<?, ?> m = ((Map<?, ?>) o);
        Iterator<?> ki = m.keySet().iterator();
        while (ki.hasNext()) {
            Object key = ki.next();
            Object value = serialize(m.get(key));
            try {
                jo.put(key.toString(), value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jo;
    }

    // primitives
    if (o instanceof Double || o instanceof Number || o instanceof Integer || o instanceof String
            || o instanceof Enum<?> || o instanceof Boolean) {
        return o;
    }

    if (o instanceof Date) {
        return ((Date) o).getTime();
    }

    // convert arrays to collections
    boolean b = o.getClass().isArray();

    if (b) {
        try {
            Object[] objs = (Object[]) o;
            List<Object> l = Arrays.asList(objs);
            return serialize(l);
        } catch (ClassCastException e) {
            return JSONObject.NULL;
        }
    }
    // serialize using bean like methods
    return serializePOJO(o, true);

}

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

public Object serializePOJO(Object pojo, boolean includeSuper) {

    if ("java.lang.Class".equals(pojo.getClass().getName())) {
        return null;
    }/*from w w w.  java 2s. c  om*/
    if (pojo.getClass().getClassLoader() == null) {
        includeSuper = false;
    }
    Object[] args = {};
    HashMap<String, Object> map = new HashMap<String, Object>();

    Method[] methods = null;
    if (includeSuper) {
        methods = pojo.getClass().getMethods();
    } else {
        methods = pojo.getClass().getDeclaredMethods();
    }

    for (int i = 0; i < methods.length; i++) {

        Method m = methods[i];
        try {

            // skip if there is a skip annotation
            if (!skipSerialization(m)) {
                // change the case of the property from camelCase
                String key = "";
                if (m.getName().startsWith("is") && m.getName().length() > 3) {
                    key += m.getName().substring(2, 3).toLowerCase();
                    // get the rest of the name;
                    key += m.getName().substring(3);
                } else if (m.getName().startsWith("get") && m.getName().length() >= 4) {
                    key += m.getName().substring(3, 4).toLowerCase();
                    // get the rest of the name;
                    key += m.getName().substring(4);
                }
                Object value = m.invoke(pojo, args);
                map.put(key, value);
            }
        } catch (IllegalArgumentException e) {
            getLogger().warning("Unable to serialize " + pojo + " : " + e);
        } catch (IllegalAccessException e) {
            getLogger().warning("Unable to serialize " + pojo + " : " + e);
        } catch (InvocationTargetException e) {
            getLogger().warning("Unable to serialize " + pojo + " : " + e);
        }
    }
    // use the serializer itself to serialize a map of properties we created
    if (map.keySet().size() > 0) {
        return serialize(map);
    }

    return JSONObject.NULL;
}

From source file:org.araqne.confdb.file.Importer.java

private Map<String, Object> parse(JSONObject jsonObject) throws IOException {
    Map<String, Object> m = new HashMap<String, Object>();
    String[] names = JSONObject.getNames(jsonObject);
    if (names == null)
        return m;

    for (String key : names) {
        try {//from   ww  w.j a va2 s.  c o m
            Object value = jsonObject.get(key);
            if (value == JSONObject.NULL)
                value = null;
            else if (value instanceof JSONArray)
                value = parse((JSONArray) value);
            else if (value instanceof JSONObject)
                value = parse((JSONObject) value);

            m.put(key, value);
        } catch (JSONException e) {
            logger.error("araqne confdb: cannot parse json", e);
            throw new IOException(e);
        }
    }

    return m;
}

From source file:org.araqne.confdb.file.Importer.java

private Object parse(JSONArray jsonarray) throws IOException {
    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < jsonarray.length(); i++) {
        try {/*from  w ww .j a va  2  s.  c o m*/
            Object o = jsonarray.get(i);
            if (o == JSONObject.NULL)
                list.add(null);
            else if (o instanceof JSONArray)
                list.add(parse((JSONArray) o));
            else if (o instanceof JSONObject)
                list.add(parse((JSONObject) o));
            else
                list.add(o);
        } catch (JSONException e) {
            logger.error("araqne confdb: cannot parse json", e);
            throw new IOException(e);
        }
    }
    return list;
}

From source file:ru.jkff.antro.ProfileListener.java

private JSONObject toJSON(Call e) throws JSONException {
    JSONObject res = new JSONObject();
    res.put("kind", e.kind.toString());
    res.put("location", toJSON(e.location));
    res.put("name", e.name == null ? JSONObject.NULL : e.name);

    return res;// w  ww  . j  a v  a2  s  . co  m
}

From source file:ru.jkff.antro.ProfileListener.java

private JSONObject toJSON(OurLocation loc) throws JSONException {
    JSONObject res = new JSONObject();
    res.put("file", loc.fileName == null ? JSONObject.NULL : loc.fileName);
    res.put("line", loc.line);
    return res;//  www .j av  a2s  . co  m
}