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.facebook.stetho.json.ObjectMapper.java

private JSONObject _convertToJSONObject(Object fromValue)
        throws JSONException, InvocationTargetException, IllegalAccessException {
    JSONObject jsonObject = new JSONObject();
    Field[] fields = fromValue.getClass().getFields();
    for (int i = 0; i < fields.length; ++i) {
        JsonProperty property = fields[i].getAnnotation(JsonProperty.class);
        if (property != null) {
            // AutoBox here ...
            Object value = fields[i].get(fromValue);
            Class clazz = fields[i].getType();
            if (value != null) {
                clazz = value.getClass();
            }//from   w  w  w .  j a va2 s. com
            String name = fields[i].getName();
            if (property.required() && value == null) {
                value = JSONObject.NULL;
            } else if (value == JSONObject.NULL) {
                // Leave it as null in this case.
            } else {
                value = getJsonValue(value, clazz, fields[i]);
            }
            jsonObject.put(name, value);
        }
    }
    return jsonObject;
}

From source file:com.jsonstore.jackson.JsonOrgJSONObjectDeserializer.java

@Override
public JSONObject deserialize(JsonParser parser, DeserializationContext context)
        throws IOException, JsonProcessingException {
    JSONObject result = new JacksonSerializedJSONObject();
    JsonToken token = parser.getCurrentToken();

    if (token == JsonToken.START_OBJECT) {
        token = parser.nextToken();/*from  ww  w.  j av  a  2  s  . c o m*/
    }

    try {
        while (token != JsonToken.END_OBJECT) {
            String name;

            if (token != JsonToken.FIELD_NAME) {
                throw context.wrongTokenException(parser, JsonToken.FIELD_NAME, ""); //$NON-NLS-1$
            }

            name = parser.getCurrentName();
            token = parser.nextToken();

            switch (token) {
            case START_ARRAY: {
                result.put(name, JsonOrgJSONArrayDeserializer.instance.deserialize(parser, context));

                break;
            }

            case START_OBJECT: {
                result.put(name, deserialize(parser, context));

                break;
            }

            case VALUE_EMBEDDED_OBJECT: {
                result.put(name, parser.getEmbeddedObject());

                break;
            }

            case VALUE_FALSE: {
                result.put(name, Boolean.FALSE);

                break;
            }

            case VALUE_NULL: {
                result.put(name, JSONObject.NULL);

                break;
            }

            case VALUE_NUMBER_FLOAT: {
                result.put(name, parser.getNumberValue());

                break;
            }

            case VALUE_NUMBER_INT: {
                result.put(name, parser.getNumberValue());

                break;
            }

            case VALUE_STRING: {
                result.put(name, parser.getText());

                break;
            }

            case VALUE_TRUE: {
                result.put(name, Boolean.TRUE);

                break;
            }

            case END_ARRAY:
            case END_OBJECT:
            case FIELD_NAME:
            case NOT_AVAILABLE: {
                break;
            }
            }

            token = parser.nextToken();
        }
    }

    catch (JSONException e) {
        throw context.mappingException(e.getMessage());
    }

    return result;
}