Example usage for org.json JSONObject numberToString

List of usage examples for org.json JSONObject numberToString

Introduction

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

Prototype

static public String numberToString(Number n) throws JSONException 

Source Link

Document

Produce a string from a Number.

Usage

From source file:org.cosmo.common.util.JSONList.java

public static byte[] valueToBytes(Object value) throws JSONException, IOException {
    if (value == null || value.equals(null)) {
        return Constants.NullByteMarker;
    }//from w  ww. j a  v  a2s . c  om
    // return raw bytes is value is byte[] - else use normal json string conversion
    if (value instanceof byte[]) {
        return (byte[]) value;
    }

    if (value instanceof long[]) {
        return Util.UTF8(longArrayString((long[]) value));
    }

    if (value instanceof JSONString) {
        Object o;
        try {
            o = ((JSONString) value).toJSONString();
        } catch (Exception e) {
            throw new JSONException(e);
        }
        if (o instanceof String) {
            return Util.UTF8(o);
        }
        throw new JSONException("Bad value from toJSONString: " + o);
    }
    if (value instanceof Number) {
        return Util.UTF8(JSONObject.numberToString((Number) value));
    }
    if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) {
        return Util.UTF8(value);
    }
    if (value instanceof Map) {
        return Util.UTF8(new JSONObject((Map) value));
    }
    if (value instanceof JSONList) {
        return ((JSONList) value).getBytes().makeExact().bytes();
    }
    if (value instanceof List) {
        return new JSONList((List) value).getBytes().makeExact().bytes();
    }
    if (value instanceof Collection) {
        return Util.UTF8(new JSONArray((Collection) value));
    }
    if (value.getClass().isArray()) {
        return Util.UTF8(new JSONArray(value));
    }
    return Util.UTF8(JSONObject.quote(value.toString()));
}

From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java

private static void appendPluginResult(SQLitePLuginResult result, StringBuilder sb) throws JSONException {
    sb.append('[');
    if (result.error == null) {
        sb.append("null");
    } else {/*from   w  ww.j  av a 2  s.c om*/
        sb.append(JSONObject.quote(result.error.getMessage()));
    }
    sb.append(',').append(JSONObject.numberToString(result.insertId)).append(',')
            .append(JSONObject.numberToString(result.rowsAffected)).append(',');

    // column names
    sb.append('[');
    for (int i = 0; i < result.columns.length; i++) {
        if (i > 0) {
            sb.append(',');
        }
        sb.append(JSONObject.quote(result.columns[i]));
    }
    sb.append("],[");
    // rows
    for (int i = 0; i < result.rows.length; i++) {
        if (i > 0) {
            sb.append(',');
        }
        Object[] values = result.rows[i];
        // row content
        sb.append('[');
        for (int j = 0; j < values.length; j++) {
            if (j > 0) {
                sb.append(',');
            }
            Object value = values[j];
            if (value == null) {
                sb.append("null");
            } else if (value instanceof String) {
                sb.append(JSONObject.quote((String) value));
            } else if (value instanceof Boolean) {
                sb.append(value.toString());
            } else {
                sb.append(JSONObject.numberToString((Number) value));
            }
        }
        sb.append(']');
    }
    sb.append("]]");

    debug("returning json: %s", sb);
}