Search in sources :

Example 1 with JsonReader

use of android.util.JsonReader in project cw-omnibus by commonsguy.

the class EditHistory method getOpenEditors.

public List<Uri> getOpenEditors() {
    String editors = prefsRef.get().getString(PREF_OPEN_EDITORS, null);
    ArrayList<Uri> result = new ArrayList<Uri>();
    if (editors != null) {
        StringReader sr = new StringReader(editors);
        JsonReader json = new JsonReader(sr);
        try {
            json.beginArray();
            while (json.hasNext()) {
                result.add(Uri.parse(json.nextString()));
            }
            json.endArray();
            json.close();
        } catch (IOException e) {
            Log.e(getClass().getSimpleName(), "Exception reading JSON", e);
        }
    }
    return (result);
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(android.util.JsonReader) IOException(java.io.IOException) Uri(android.net.Uri)

Example 2 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 3 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 4 with JsonReader

use of android.util.JsonReader in project robolectric by robolectric.

the class ShadowJsonReaderTest method shouldWork.

@Test
public void shouldWork() throws Exception {
    JsonReader jsonReader = new JsonReader(new StringReader("{\"abc\": \"def\"}"));
    jsonReader.beginObject();
    assertThat(jsonReader.nextName()).isEqualTo("abc");
    assertThat(jsonReader.nextString()).isEqualTo("def");
    jsonReader.endObject();
}
Also used : StringReader(java.io.StringReader) JsonReader(android.util.JsonReader) Test(org.junit.Test)

Example 5 with JsonReader

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

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