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:synapticloop.b2.response.BaseB2Response.java

/**
 * Read and remove String with key from JSON object
 * //from  w  w w .  jav a 2 s  .  com
 * @param response The JSON object to read from
 * @param key the key to read as a string and remove
 * 
 * @return the read key (or null if it doesn't exist)
 */
protected String readString(JSONObject response, String key) {
    final Object value = response.remove(key);
    if (null == value || JSONObject.NULL == value) {
        getLogger().warn("No field for key {}", key);
        return null;
    }
    return value.toString();
}

From source file:synapticloop.b2.response.BaseB2Response.java

/**
 * Read and remove int with key from JSON object
 * /*from  w w w. j  a  va2 s .com*/
 * @param key the key to read as an int and remove
 * 
 * @return the read key (or null if it doesn't exist)
 */
protected Integer readInt(String key) {
    final Object value = response.remove(key);
    if (null == value || JSONObject.NULL == value) {
        getLogger().warn("No field for key {}", key);
        return null;
    }
    return value instanceof Number ? ((Number) value).intValue() : Integer.parseInt(value.toString());
}

From source file:synapticloop.b2.response.BaseB2Response.java

/**
 * Read and remove long with key from JSON object
 * /*from www. j  a  va 2s .c o m*/
 * @param key the key to read as a long and remove
 * 
 * @return the read key (or null if it doesn't exist)
 */
protected Long readLong(String key) {
    final Object value = response.remove(key);
    if (null == value || JSONObject.NULL == value) {
        getLogger().warn("No field for key {}", key);
        return null;
    }
    return value instanceof Number ? ((Number) value).longValue() : Long.parseLong(value.toString());
}

From source file:synapticloop.b2.response.BaseB2Response.java

/**
 * Read and remove JSONObject with key from JSON object
 * //from   ww w . j  a va 2  s . com
 * @param response The JSON object to read from
 * @param key the key to read as a JSONObject and remove
 * 
 * @return the read key (or null if it doesn't exist)
 */
protected JSONObject readObject(JSONObject response, String key) {
    final Object value = response.remove(key);
    if (null == value || JSONObject.NULL == value) {
        getLogger().warn("No field for key {}", key);
        return null;
    }
    return value instanceof JSONObject ? (JSONObject) value : null;
}

From source file:synapticloop.b2.response.BaseB2Response.java

/**
 * Read and remove JSONArray with key from JSON object
 * //from   ww w. ja v a 2  s . c  om
 * @param key the key to read as a JSONArray and remove
 * 
 * @return the read key (or null if it doesn't exist)
 */
protected JSONArray readObjects(String key) {
    final Object value = response.remove(key);
    if (null == value || JSONObject.NULL == value) {
        getLogger().warn("No field for key {}", key);
        return null;
    }
    return value instanceof JSONArray ? (JSONArray) value : null;
}

From source file:synapticloop.b2.response.BaseB2Response.java

/**
 * Read and remove JSONObject with key from JSON object
 *
 * @param key the key to read as a JSONObject and put keys and values into map
 *
 * @return the read keys and values (or null if it doesn't exist)
 *///from  w  w w  .j a va  2  s . c o  m
protected Map<String, String> readMap(String key) {
    final Map<String, String> map = new HashMap<String, String>();
    JSONObject value = this.readObject(key);
    if (null == value || JSONObject.NULL == value) {
        getLogger().warn("No field for key {}", key);
        return null;
    }
    for (String k : value.keySet().toArray(new String[value.keySet().size()])) {
        map.put(k, this.readString(value, k));
    }
    return map;
}

From source file:org.qi4j.entitystore.sql.SQLEntityStoreMixin.java

protected DefaultEntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState)
        throws EntityStoreException {
    try {/*from   w w  w .j  a  v a2s. co m*/
        ModuleSPI module = unitOfWork.module();
        JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
        EntityStatus status = EntityStatus.LOADED;

        String version = jsonObject.getString("version");
        long modified = jsonObject.getLong("modified");
        String identity = jsonObject.getString("identity");

        // Check if version is correct
        String currentAppVersion = jsonObject.optString(MapEntityStore.JSONKeys.application_version.name(),
                "0.0");
        if (!currentAppVersion.equals(application.version())) {
            if (migration != null) {
                migration.migrate(jsonObject, application.version(), this);
            } else {
                // Do nothing - set version to be correct
                jsonObject.put(MapEntityStore.JSONKeys.application_version.name(), application.version());
            }

            LOGGER.trace("Updated version nr on {} from {} to {}",
                    new Object[] { identity, currentAppVersion, application.version() });

            // State changed
            status = EntityStatus.UPDATED;
        }

        String type = jsonObject.getString("type");

        EntityDescriptor entityDescriptor = module.entityDescriptor(type);
        if (entityDescriptor == null) {
            throw new EntityTypeNotFoundException(type);
        }

        Map<QualifiedName, Object> properties = new HashMap<QualifiedName, Object>();
        JSONObject props = jsonObject.getJSONObject("properties");
        for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
            Object jsonValue;
            try {
                jsonValue = props.get(propertyDescriptor.qualifiedName().name());
            } catch (JSONException e) {
                // Value not found, default it
                Object initialValue = propertyDescriptor.initialValue();
                properties.put(propertyDescriptor.qualifiedName(), initialValue);
                status = EntityStatus.UPDATED;
                continue;
            }
            if (jsonValue == JSONObject.NULL) {
                properties.put(propertyDescriptor.qualifiedName(), null);
            } else {
                Object value = ((PropertyTypeDescriptor) propertyDescriptor).propertyType().type()
                        .fromJSON(jsonValue, module);
                properties.put(propertyDescriptor.qualifiedName(), value);
            }
        }

        Map<QualifiedName, EntityReference> associations = new HashMap<QualifiedName, EntityReference>();
        JSONObject assocs = jsonObject.getJSONObject("associations");
        for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
            try {
                Object jsonValue = assocs.get(associationType.qualifiedName().name());
                EntityReference value = jsonValue == JSONObject.NULL ? null
                        : EntityReference.parseEntityReference((String) jsonValue);
                associations.put(associationType.qualifiedName(), value);
            } catch (JSONException e) {
                // Association not found, default it to null
                associations.put(associationType.qualifiedName(), null);
                status = EntityStatus.UPDATED;
            }
        }

        Map<QualifiedName, List<EntityReference>> manyAssociations = createManyAssociations(jsonObject,
                entityDescriptor);
        Map<QualifiedName, Map<String, EntityReference>> namedAssociations = createNamedAssociations(jsonObject,
                entityDescriptor);

        return new DefaultEntityState(unitOfWork, version, modified,
                EntityReference.parseEntityReference(identity), status, entityDescriptor, properties,
                associations, manyAssociations, namedAssociations);
    } catch (JSONException e) {
        throw new EntityStoreException(e);
    }
}

From source file:org.qi4j.entitystore.sql.SQLEntityStoreMixin.java

private Map<QualifiedName, List<EntityReference>> createManyAssociations(JSONObject jsonObject,
        EntityDescriptor entityDescriptor) throws JSONException {
    JSONObject manyAssocs = jsonObject.getJSONObject("manyassociations");
    Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<QualifiedName, List<EntityReference>>();
    for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
        List<EntityReference> references = new ArrayList<EntityReference>();
        try {//www. j av a  2 s  .  c o  m
            JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
            for (int i = 0; i < jsonValues.length(); i++) {
                Object jsonValue = jsonValues.getString(i);
                EntityReference value = jsonValue == JSONObject.NULL ? null
                        : EntityReference.parseEntityReference((String) jsonValue);
                references.add(value);
            }
            manyAssociations.put(manyAssociationType.qualifiedName(), references);
        } catch (JSONException e) {
            // ManyAssociation not found, default to empty one
            manyAssociations.put(manyAssociationType.qualifiedName(), references);
        }
    }
    return manyAssociations;
}

From source file:org.qi4j.entitystore.sql.SQLEntityStoreMixin.java

private Map<QualifiedName, Map<String, EntityReference>> createNamedAssociations(JSONObject jsonObject,
        EntityDescriptor entityDescriptor) throws JSONException {
    JSONObject namedAssocs = jsonObject.getJSONObject("namedassociations");
    Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<QualifiedName, Map<String, EntityReference>>();
    for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
        Map<String, EntityReference> references = new HashMap<String, EntityReference>();
        try {//  ww  w.  ja v  a 2  s  .co  m
            JSONObject jsonValues = namedAssocs.getJSONObject(namedAssociationType.qualifiedName().name());
            for (String name : jsonValues) {
                Object jsonValue = jsonValues.getString(name);
                EntityReference value;
                if (jsonValue == JSONObject.NULL) {
                    value = null;
                } else {
                    value = EntityReference.parseEntityReference((String) jsonValue);
                }
                references.put(name, value);
            }
            namedAssociations.put(namedAssociationType.qualifiedName(), references);
        } catch (JSONException e) {
            // NamedAssociation not found, default to empty one
            namedAssociations.put(namedAssociationType.qualifiedName(), references);
        }
    }
    return namedAssociations;
}

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

public String toJSONOneEntity(List<Entity> entities) {
    String jsonResult = "";
    try {/*from   www.  j  a  va2 s  .c  o  m*/

        JSONObject jsonE = new JSONObject();
        if (!entities.isEmpty()) {

            Entity e = entities.get(0);

            jsonE.put("underlyingString", e.getUnderlyingString());
            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 {
                        typeJ.put("typeLabel", JSONObject.NULL);
                    }

                    String tURI = t.getTypeURI();
                    if (tURI != null) {
                        typeJ.put("typeURI", t.getTypeURI());
                    } else {
                        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);
                    }

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

        jsonResult = jsonE.toString();
        return jsonResult;

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