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);
}
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();
}
}
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;
}
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();
}
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);
}
Aggregations