Search in sources :

Example 1 with JsonToken

use of android.util.JsonToken in project platform_frameworks_base by android.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Example 2 with JsonToken

use of android.util.JsonToken in project android_frameworks_base by ResurrectionRemix.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Example 3 with JsonToken

use of android.util.JsonToken in project android_frameworks_base by crdroidandroid.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Example 4 with JsonToken

use of android.util.JsonToken in project android_frameworks_base by AOSPA.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Example 5 with JsonToken

use of android.util.JsonToken in project android_frameworks_base by DirtyUnicorns.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Aggregations

JsonToken (android.util.JsonToken)5 JSONArray (org.json.JSONArray)5 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5