Search in sources :

Example 6 with JsonReader

use of android.util.JsonReader in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class JsonUtils method jsonStrToList.

public static List<Object> jsonStrToList(final String s) {
    final ArrayList<Object> list = new ArrayList<>();
    final JsonReader reader = new JsonReader(new StringReader(s));
    try {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            while (reader.hasNext()) {
                final String name = reader.nextName();
                if (name.equals(INTEGER_CLASS_NAME)) {
                    list.add(reader.nextInt());
                } else if (name.equals(STRING_CLASS_NAME)) {
                    list.add(reader.nextString());
                } else {
                    Log.w(TAG, "Invalid name: " + name);
                    reader.skipValue();
                }
            }
            reader.endObject();
        }
        reader.endArray();
        return list;
    } catch (final IOException e) {
    } finally {
        close(reader);
    }
    return Collections.<Object>emptyList();
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(android.util.JsonReader) IOException(java.io.IOException)

Example 7 with JsonReader

use of android.util.JsonReader in project DrupalCloud by INsReady.

the class RESTServerClient method userRegister.

/***
	 * Register a user in your site.
	 * @param username 
	 * @param password
	 * @param email 
	 * @return
	 */
public JsonReader userRegister(String username, String password, String email) throws ServiceNotAvailableException, IOException {
    String uri = mENDPOIN + "user/register";
    BasicNameValuePair[] parameters = new BasicNameValuePair[3];
    parameters[0] = new BasicNameValuePair("name", username);
    parameters[1] = new BasicNameValuePair("pass", password);
    parameters[2] = new BasicNameValuePair("mail", email);
    JsonReader jsr = new JsonReader(callPost(uri, parameters));
    return jsr;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) JsonReader(android.util.JsonReader)

Example 8 with JsonReader

use of android.util.JsonReader in project realm-java by realm.

the class Realm method createAllFromJson.

/**
     * Creates a Realm object for each object in a JSON array. This must be done within a transaction.
     * JSON properties with unknown properties will be ignored. If a {@link RealmObject} field is not present in the
     * JSON object the {@link RealmObject} field will be set to the default value for that type.
     * <p>
     * This API is only available in API level 11 or later.
     *
     * @param clazz type of Realm objects created.
     * @param inputStream the JSON array as a InputStream. All objects in the array must be of the specified class.
     * @throws RealmException if mapping from JSON fails.
     * @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
     * {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
     * @throws IOException if something was wrong with the input stream.
     */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public <E extends RealmModel> void createAllFromJson(Class<E> clazz, InputStream inputStream) throws IOException {
    if (clazz == null || inputStream == null) {
        return;
    }
    checkIfValid();
    JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
    try {
        reader.beginArray();
        while (reader.hasNext()) {
            configuration.getSchemaMediator().createUsingJsonStream(clazz, this, reader);
        }
        reader.endArray();
    } finally {
        reader.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonReader(android.util.JsonReader) TargetApi(android.annotation.TargetApi)

Example 9 with JsonReader

use of android.util.JsonReader in project realm-java by realm.

the class Realm method createObjectFromJson.

/**
     * Creates a Realm object pre-filled with data from a JSON object. This must be done inside a transaction. JSON
     * properties with unknown properties will be ignored. If a {@link RealmObject} field is not present in the JSON
     * object the {@link RealmObject} field will be set to the default value for that type.
     * <p>
     * This API is only available in API level 11 or later.
     *
     * @param clazz type of Realm object to create.
     * @param inputStream the JSON object data as a InputStream.
     * @return created object or {@code null} if JSON string was empty or null.
     * @throws RealmException if the mapping from JSON failed.
     * @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
     * {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
     * @throws IOException if something went wrong with the input stream.
     */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public <E extends RealmModel> E createObjectFromJson(Class<E> clazz, InputStream inputStream) throws IOException {
    if (clazz == null || inputStream == null) {
        return null;
    }
    checkIfValid();
    E realmObject;
    Table table = schema.getTable(clazz);
    if (table.hasPrimaryKey()) {
        // As we need the primary key value we have to first parse the entire input stream as in the general
        // case that value might be the last property. :(
        Scanner scanner = null;
        try {
            scanner = getFullStringScanner(inputStream);
            JSONObject json = new JSONObject(scanner.next());
            realmObject = configuration.getSchemaMediator().createOrUpdateUsingJsonObject(clazz, this, json, false);
        } catch (JSONException e) {
            throw new RealmException("Failed to read JSON", e);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    } else {
        JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
        try {
            realmObject = configuration.getSchemaMediator().createUsingJsonStream(clazz, this, reader);
        } finally {
            reader.close();
        }
    }
    return realmObject;
}
Also used : Scanner(java.util.Scanner) Table(io.realm.internal.Table) JSONObject(org.json.JSONObject) InputStreamReader(java.io.InputStreamReader) JSONException(org.json.JSONException) JsonReader(android.util.JsonReader) RealmException(io.realm.exceptions.RealmException) TargetApi(android.annotation.TargetApi)

Example 10 with JsonReader

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

the class StatementParser method parseStatementList.

/**
     * Parses a JSON array of statements.
     */
static ParsedStatement parseStatementList(String statementList, AbstractAsset source) throws JSONException, IOException {
    List<Statement> statements = new ArrayList<Statement>();
    List<String> delegates = new ArrayList<String>();
    JsonReader reader = new JsonReader(new StringReader(statementList));
    reader.setLenient(false);
    reader.beginArray();
    while (reader.hasNext()) {
        ParsedStatement result;
        try {
            result = parseStatement(reader, source);
        } catch (AssociationServiceException e) {
            // The element in the array is well formatted Json but not a well-formed Statement.
            continue;
        }
        statements.addAll(result.getStatements());
        delegates.addAll(result.getDelegates());
    }
    reader.endArray();
    return new ParsedStatement(statements, delegates);
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(android.util.JsonReader)

Aggregations

JsonReader (android.util.JsonReader)26 StringReader (java.io.StringReader)18 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 JSONException (org.json.JSONException)6 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)3 TargetApi (android.annotation.TargetApi)2 InputStreamReader (java.io.InputStreamReader)2 SuppressLint (android.annotation.SuppressLint)1 Uri (android.net.Uri)1 RealmException (io.realm.exceptions.RealmException)1 Table (io.realm.internal.Table)1 Scanner (java.util.Scanner)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1