Here are the examples of the java api class android.util.JsonReader taken from open source projects.
1. Realm#createAllFromJson()
Project: realm-java
File: Realm.java
File: Realm.java
/** * Creates a Realm object for each object in a JSON array. This must be done within a transaction. * JSON properties with {@code null} value will map to the default value for the data type in Realm and 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. * * @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 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; } 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(); } }
2. AndroidPlacesApiJsonParser#readHistoryJson()
Project: android-PlacesAutocompleteTextView
File: AndroidPlacesApiJsonParser.java
File: AndroidPlacesApiJsonParser.java
@Override public List<Place> readHistoryJson(final InputStream in) throws JsonParsingException { JsonReader reader = null; try { reader = new JsonReader(new InputStreamReader(in, "UTF-8")); List<Place> places = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { places.add(readPlace(reader)); } reader.endArray(); reader.close(); return places; } catch (Exception e) { throw new JsonParsingException(e); } finally { ResourceUtils.closeResourceQuietly(reader); } }
3. BuiltInSerializers#deserializeStringCollection()
Project: Android-Orma
File: BuiltInSerializers.java
File: BuiltInSerializers.java
@NonNull public static <C extends Collection<String>> C deserializeStringCollection(@NonNull String serialized, C collection) { StringReader reader = new StringReader(serialized); JsonReader jsonReader = new JsonReader(reader); try { jsonReader.beginArray(); while (jsonReader.hasNext()) { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); collection.add(null); } else { collection.add(jsonReader.nextString()); } } jsonReader.endArray(); jsonReader.close(); return collection; } catch (IOException e) { return collection; } }
4. JsonReaderRequest#parseNetworkResponse()
Project: Clover
File: JsonReaderRequest.java
File: JsonReaderRequest.java
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { ByteArrayInputStream baos = new ByteArrayInputStream(response.data); JsonReader reader = new JsonReader(new InputStreamReader(baos, UTF8)); Exception exception = null; T read = null; try { read = readJson(reader); } catch (Exception e) { exception = e; } IOUtils.closeQuietly(reader); if (read == null) { if (exception != null) { return Response.error(new VolleyError(exception)); } else { return Response.error(new VolleyError("Unknown error")); } } else { return Response.success(read, HttpHeaderParser.parseCacheHeaders(response)); } }
5. DOMUtils#getNodeField()
Project: chromium_webview
File: DOMUtils.java
File: DOMUtils.java
private static String getNodeField(String fieldName, final ContentView view, TestCallbackHelperContainer viewClient, String nodeId) throws InterruptedException, TimeoutException { StringBuilder sb = new StringBuilder(); sb.append("(function() {"); sb.append(" var node = document.getElementById('" + nodeId + "');"); sb.append(" if (!node) return null;"); sb.append(" if (!node." + fieldName + ") return null;"); sb.append(" return [ node." + fieldName + " ];"); sb.append("})();"); String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult(view, viewClient, sb.toString()); Assert.assertFalse("Failed to retrieve contents for " + nodeId, jsonText.trim().equalsIgnoreCase("null")); JsonReader jsonReader = new JsonReader(new StringReader(jsonText)); String value = null; try { jsonReader.beginArray(); if (jsonReader.hasNext()) value = jsonReader.nextString(); jsonReader.endArray(); Assert.assertNotNull("Invalid contents returned.", value); jsonReader.close(); } catch (IOException exception) { Assert.fail("Failed to evaluate JavaScript: " + jsonText + "\n" + exception); } return value; }
6. DOMUtils#getNodeBounds()
Project: chromium_webview
File: DOMUtils.java
File: DOMUtils.java
/** * Returns the rect boundaries for a node by its id. */ public static Rect getNodeBounds(final ContentView view, TestCallbackHelperContainer viewClient, String nodeId) throws InterruptedException, TimeoutException { StringBuilder sb = new StringBuilder(); sb.append("(function() {"); sb.append(" var node = document.getElementById('" + nodeId + "');"); sb.append(" if (!node) return null;"); sb.append(" var width = node.offsetWidth;"); sb.append(" var height = node.offsetHeight;"); sb.append(" var x = -window.scrollX;"); sb.append(" var y = -window.scrollY;"); sb.append(" do {"); sb.append(" x += node.offsetLeft;"); sb.append(" y += node.offsetTop;"); sb.append(" } while (node = node.offsetParent);"); sb.append(" return [ x, y, width, height ];"); sb.append("})();"); String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult(view, viewClient, sb.toString()); Assert.assertFalse("Failed to retrieve bounds for " + nodeId, jsonText.trim().equalsIgnoreCase("null")); JsonReader jsonReader = new JsonReader(new StringReader(jsonText)); int[] bounds = new int[4]; try { jsonReader.beginArray(); int i = 0; while (jsonReader.hasNext()) { bounds[i++] = jsonReader.nextInt(); } jsonReader.endArray(); Assert.assertEquals("Invalid bounds returned.", 4, i); jsonReader.close(); } catch (IOException exception) { Assert.fail("Failed to evaluate JavaScript: " + jsonText + "\n" + exception); } return new Rect(bounds[0], bounds[1], bounds[0] + bounds[2], bounds[1] + bounds[3]); }
7. AndroidPlacesApiJsonParser#detailsFromStream()
Project: android-PlacesAutocompleteTextView
File: AndroidPlacesApiJsonParser.java
File: AndroidPlacesApiJsonParser.java
@Override public PlacesDetailsResponse detailsFromStream(final InputStream is) throws JsonParsingException { JsonReader reader = null; try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); reader = new JsonReader(bufferedReader); PlaceDetails result = null; Status status = null; String errorMessage = null; reader.beginObject(); while (reader.hasNext()) { switch(reader.nextName()) { case "result": result = readPlaceDetails(reader); break; case "status": status = readStatus(reader); break; case "error_message": errorMessage = reader.nextString(); break; default: reader.skipValue(); break; } } reader.endObject(); return new PlacesDetailsResponse(status, errorMessage, result); } catch (Exception e) { throw new JsonParsingException(e); } finally { ResourceUtils.closeResourceQuietly(reader); } }
8. AndroidPlacesApiJsonParser#autocompleteFromStream()
Project: android-PlacesAutocompleteTextView
File: AndroidPlacesApiJsonParser.java
File: AndroidPlacesApiJsonParser.java
@Override public PlacesAutocompleteResponse autocompleteFromStream(final InputStream is) throws JsonParsingException { JsonReader reader = null; try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); reader = new JsonReader(bufferedReader); List<Place> predictions = null; Status status = null; String errorMessage = null; reader.beginObject(); while (reader.hasNext()) { switch(reader.nextName()) { case "predictions": predictions = readPredictionsArray(reader); break; case "status": status = readStatus(reader); break; case "error_message": errorMessage = reader.nextString(); break; default: reader.skipValue(); break; } } reader.endObject(); return new PlacesAutocompleteResponse(status, errorMessage, predictions); } catch (Exception e) { throw new JsonParsingException(e); } finally { ResourceUtils.closeResourceQuietly(reader); } }
9. Cartographer#fromJson()
Project: analytics-android
File: Cartographer.java
File: Cartographer.java
/** * Deserializes the json read from the specified {@link Reader} into a {@link Map}. If you have * the Json in a String form instead of a {@link Reader}, use {@link #fromJson(String)} instead. */ Map<String, Object> fromJson(Reader reader) throws IOException { JsonReader jsonReader = new JsonReader(reader); try { return readerToMap(jsonReader); } finally { reader.close(); } }