/** * Reads a JSON object for a specific density and populates variables if the density matches * {@code bitmapDensity}. * * @param reader The JsonReader reading the JSON metadata. * @param bitmapDensity The density of the unscaled {@link Bitmap} that was loaded. * @return True if the JSON object being parsed corresponds to bitmapDensity. * @throws IOException */ private boolean parseMetadataForDensity(JsonReader reader, int bitmapDensity) throws IOException { String name = reader.nextName(); assert name.equals("density"); int density = reader.nextInt(); // If this is metadata for a density other than bitmapDensity, skip parsing the rest of the // object. if (density != bitmapDensity) { reader.skipValue(); // Skip width name. reader.skipValue(); // Skip width value. reader.skipValue(); // Skip height name. reader.skipValue(); // Skip height value. reader.skipValue(); // Skip rectangles name. reader.skipValue(); // Skip rectangles array. return false; } name = reader.nextName(); assert name.equals("width"); mSpriteWidth = reader.nextInt(); name = reader.nextName(); assert name.equals("height"); mSpriteHeight = reader.nextInt(); name = reader.nextName(); assert name.equals("rectangles"); parseFrameRectangles(reader); return true; }
/** * Parses the raw JSON resource specified by {@code metadataResId}. The JSON is expected to be in * this format: { "apiVersion": <version number (string)>, "densities": [ { "density": <density * (int)>, "width": <unscaled sprite width (int)>, "height": <unscaled sprite height (int)>, * "rectangles": [ [<list of ints for frame 0>], [<list of ints for frame 1>], ...... ] }, { * "density": <density (int)>, "width": <unscaled sprite width (int)>, "height": <unscaled sprite * height (int)>, "rectangles": [ [<list of ints for frame 0>], [<list of ints for frame 1>], * ...... ] }, ...... ] } * * @param metadataResId The id of the raw resource containing JSON to parse. * @param bitmapDensity The density of the unscaled {@link Bitmap} that was loaded. * @param resources A {@link Resources} instance to load assets from. * @throws IOException */ @VisibleForTesting void parseMetadata(int metadataResId, int bitmapDensity, Resources resources) throws IOException { InputStream inputStream = resources.openRawResource(metadataResId); JsonReader reader = new JsonReader(new InputStreamReader(inputStream)); try { reader.beginObject(); // Start reading top-level object. // Check apiVersion. String name = reader.nextName(); assert name.equals("apiVersion"); String version = reader.nextString(); assert version.equals("1.0"); // Parse array of densities. name = reader.nextName(); assert name.equals("densities"); reader.beginArray(); // Start reading array of densities. while (reader.hasNext()) { reader.beginObject(); // Start reading object for this density. boolean foundDensity = parseMetadataForDensity(reader, bitmapDensity); reader.endObject(); // Stop reading object for this density. if (foundDensity) break; } } finally { reader.close(); inputStream.close(); } }
@Override public void deSerializeRepresentation(JsonReader sreader) throws IOException { sreader.beginObject(); Vector<StrokeData> strokes = new Vector<StrokeData>(); while (sreader.hasNext()) { sreader.nextName(); sreader.beginObject(); StrokeData stroke = new StrokeData(); while (sreader.hasNext()) { String name = sreader.nextName(); if (name.equals(SERIAL_COLOR)) { stroke.mColor = sreader.nextInt(); } else if (name.equals(SERIAL_RADIUS)) { stroke.mRadius = (float) sreader.nextDouble(); } else if (name.equals(SERIAL_TYPE)) { stroke.mType = (byte) sreader.nextInt(); } else if (name.equals(SERIAL_POINTS_COUNT)) { stroke.noPoints = sreader.nextInt(); } else if (name.equals(SERIAL_POINTS)) { int count = 0; sreader.beginArray(); while (sreader.hasNext()) { if ((count + 1) > stroke.mPoints.length) { stroke.mPoints = Arrays.copyOf(stroke.mPoints, count * 2); } stroke.mPoints[count++] = (float) sreader.nextDouble(); } stroke.mPath = new Path(); stroke.mPath.moveTo(stroke.mPoints[0], stroke.mPoints[1]); for (int i = 0; i < count; i += 2) { stroke.mPath.lineTo(stroke.mPoints[i], stroke.mPoints[i + 1]); } sreader.endArray(); strokes.add(stroke); } else { sreader.skipValue(); } } sreader.endObject(); } mDrawing = strokes; sreader.endObject(); }
private List<Post> loadCatalog(JsonReader reader) throws Exception { ArrayList<Post> list = new ArrayList<>(); reader.beginArray(); // Array of pages while (reader.hasNext()) { reader.beginObject(); // Page object while (reader.hasNext()) { if (reader.nextName().equals("threads")) { reader.beginArray(); // Threads array while (reader.hasNext()) { list.add(readPostObject(reader)); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); } reader.endArray(); return list; }
@Override public void onHttpRequestComplete(Intent intent) { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } if (intent.getIntExtra(Constants.IntentExtras.REQUEST_ID, -1) == Constants.ApiRequestId.GET_EMOJIS_DATA) { try { String trendingShows = intent.getStringExtra(Constants.IntentExtras.MESSAGE); JsonReader jsonReader = new JsonReader(new StringReader(trendingShows)); jsonReader.beginObject(); while (jsonReader.hasNext()) { String emojisName = jsonReader.nextName(); String emojisImageUrl = jsonReader.nextString(); Log.d(getTag(), emojisName + ":" + emojisImageUrl); EmojiData emojiData = new EmojiData(); emojiData.setEmojisName(emojisName); emojiData.setEmojisImageUrl(emojisImageUrl); emojiDataList.add(emojiData); } jsonReader.endObject(); this.emojisListAdapter.notifyDataSetChanged(); } catch (IOException e) { e.printStackTrace(); Log.e(getTag(), e.getMessage()); } } }
private void parseWeatherFormWeather(JsonReader reader) throws IOException { Log.d(TAG, "Entering parseWeatherFormWeather"); reader.beginArray(); reader.beginObject(); try { while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case JsonWeatherTokens.DESCRIPTION: weatherData.setWeather(reader.nextString()); break; case JsonWeatherTokens.ICON: weatherData.setIcon(reader.nextString()); break; default: reader.skipValue(); break; } } } finally { reader.endObject(); reader.endArray(); } }
private void readErthquake(JsonReader reader) throws IOException { String s; reader.beginObject(); while (reader.hasNext()) { s = reader.nextName(); if (s.equals("properties")) { readProperties(reader); } else if (s.equals("geometry")) { readGeometry(reader); } else { reader.skipValue(); } } reader.endObject(); final Quake quake = new Quake(date, details, location, magnitude, link); // Process a newly found earthquake handler.post( new Runnable() { public void run() { addNewQuake(quake); } }); }
private void parseWeatherServiceResults(JsonReader reader) throws IOException { Log.d(TAG, "Entering parse Weather Service Results"); reader.beginObject(); try { while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case JsonWeatherTokens.SYS: if (reader.peek() == JsonToken.BEGIN_OBJECT) { parseWeatherFormSys(reader); break; } case JsonWeatherTokens.WEATHER: parseWeatherFormWeather(reader); break; case JsonWeatherTokens.MAIN: parseWeatherFormMain(reader); break; case JsonWeatherTokens.WIND: parseWeatherFormWind(reader); break; case JsonWeatherTokens.NAME: weatherData.setCity(reader.nextString()); break; default: reader.skipValue(); break; } } } finally { reader.endObject(); } }
public ArrayList<Promotion> parsePromotionList(JsonReader jsonReader) { ArrayList<Promotion> promoList = new ArrayList<Promotion>(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_PROMOTIONS) && jsonReader.peek() != JsonToken.NULL) { jsonReader.beginArray(); while (jsonReader.hasNext()) { Promotion promo = parsePromotion(jsonReader); if (promo != null) { promoList.add(promo); } } jsonReader.endArray(); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return promoList; }
private void parseWeatherFormSys(JsonReader reader) throws IOException { Log.d(TAG, "Entering parseWeatherFormSys"); reader.beginObject(); try { while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case JsonWeatherTokens.COUNTRY: weatherData.setCountry(reader.nextString()); break; case JsonWeatherTokens.SUNRISE: weatherData.setSunrise(reader.nextLong()); break; case JsonWeatherTokens.SUNSET: weatherData.setSunset(reader.nextLong()); break; default: reader.skipValue(); break; } } } finally { reader.endObject(); } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public ArrayList<Song> parseSongList(JsonReader jsonReader) { ArrayList<Song> songList = new ArrayList<Song>(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_RESULTS) && jsonReader.peek() != JsonToken.NULL) { jsonReader.beginArray(); while (jsonReader.hasNext()) { Song song = parseSong(jsonReader); if (song != null) { songList.add(song); } } jsonReader.endArray(); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return songList; }
public Repo readMessage(JsonReader reader) throws IOException { repo = new Repo(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { repo.setName(reader.nextString()); } else if (name.equals("description")) { repo.setDescription(reader.nextString()); } else if (name.equals("owner")) { ReadOwnerMessage(reader); } else if (name.equals("html_url")) { repo.setRepo_url(reader.nextString()); } else if (name.equals("fork")) { repo.setFork(reader.nextBoolean()); } else { reader.skipValue(); } } reader.endObject(); return repo; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public Song parseSong(JsonReader jsonReader) { Song song = new Song(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_TRACKID)) { song.setId(jsonReader.nextLong()); } else if (name.equals(JSON_KEY_ARTISTNAME)) { song.setArtist(jsonReader.nextString()); } else if (name.equals(JSON_KEY_TRACKNAME)) { song.setTitle(jsonReader.nextString()); } else if (name.equals(JSON_KEY_TRACKPRICE)) { song.setPrice(jsonReader.nextDouble()); } else if (name.equals(JSON_KEY_TRACKCOVER)) { song.setCoverURL(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return song; }
@SuppressWarnings("cast") public static some.test.Simple createUsingJsonStream(Realm realm, JsonReader reader) throws IOException { some.test.Simple obj = realm.createObject(some.test.Simple.class); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { if (reader.peek() == JsonToken.NULL) { reader.skipValue(); ((SimpleRealmProxyInterface) obj).realmSet$name(null); } else { ((SimpleRealmProxyInterface) obj).realmSet$name((String) reader.nextString()); } } else if (name.equals("age")) { if (reader.peek() == JsonToken.NULL) { reader.skipValue(); throw new IllegalArgumentException("Trying to set non-nullable field 'age' to null."); } else { ((SimpleRealmProxyInterface) obj).realmSet$age((int) reader.nextInt()); } } else { reader.skipValue(); } } reader.endObject(); return obj; }
private FoodResult readHitObject(JsonReader reader) throws IOException { FoodResult food = new FoodResult(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("_index")) { String index = reader.nextString(); // we currently do nothing with this value } else if (name.equals("_type")) { String type = reader.nextString(); // we currently do nothing with this value } else if (name.equals("_id")) { food.id = reader.nextString(); } else if (name.equals("_score")) { double score = reader.nextDouble(); // we currently do nothing with this value } else if (name.equals("fields")) { readHitFields(reader, food); } else { reader.skipValue(); } } reader.endObject(); return food; }
private SnippetArticle readArticleDetails(JsonReader reader) throws IOException { String headline = ""; String publisher = ""; String snippets = ""; String url = ""; String thumbnail = ""; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("headline")) { headline = reader.nextString(); } else if (name.equals("publisher")) { publisher = reader.nextString(); } else if (name.equals("snippet")) { snippets = reader.nextString(); } else if (name.equals("url")) { url = reader.nextString(); } else if (name.equals("thumbnail")) { thumbnail = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); return new SnippetsManager.SnippetArticle( headline, publisher, snippets, url, thumbnail, mNumArticles++); }
public Promotion parsePromotion(JsonReader jsonReader) { Promotion promo = new Promotion(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_BUTTON)) { promo.setButton(parseButton(jsonReader)); } else if (name.equals(JSON_KEY_DESCRIPTION)) { promo.setDescription(jsonReader.nextString()); } else if (name.equals(JSON_KEY_FOOTER)) { promo.setFooter(jsonReader.nextString()); } else if (name.equals(JSON_KEY_TITLE)) { promo.setTitle(jsonReader.nextString()); } else if (name.equals(JSON_KEY_IMAGE)) { promo.setImage(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return promo; }
@Override public void onMessage(int instanceId, String message) { StringReader contents = new StringReader(message); JsonReader reader = new JsonReader(contents); int requestId = -1; String cmd = null; String url = null; try { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals(TAG_CMD)) { cmd = reader.nextString(); } else if (name.equals(TAG_REQUEST_ID)) { requestId = reader.nextInt(); } else if (name.equals("url")) { url = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); if (cmd != null && cmd.equals(CMD_REQUEST_SHOW) && requestId >= 0) { handleRequestShow(instanceId, url, requestId); } } catch (IOException e) { Log.d(TAG, "Error: " + e); } }
private void jsonToUsuario(JsonReader reader) throws JSONException, IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "id": this.id = reader.nextInt(); break; case "token": this.token = reader.nextString(); break; case "latitud": this.latitud = reader.nextDouble(); break; case "longitud": this.longitud = reader.nextDouble(); break; case "activo": this.activo = reader.nextBoolean(); break; case "ofreceTransito": this.ofreceTransito = reader.nextBoolean(); break; case "autopublicar": this.autopublicar = reader.nextBoolean(); break; case "email": this.email = reader.nextString(); break; case "username": this.username = reader.nextString(); break; case "password": this.password = reader.nextString(); break; case "telefono": this.telefono = reader.nextString(); break; case "direccion": this.direccion = reader.nextString(); break; case "foto": if (reader.peek() == JsonToken.NULL) reader.nextNull(); else { this.foto = null; reader.nextString(); // TODO: Completar cuando el post del usuario admita json // this.foto.setImg(Imagen.bytesFromBase64DEFAULT(reader.nextString())); } break; default: reader.skipValue(); break; } } reader.endObject(); }
public Button parseButton(JsonReader jsonReader) { Button button = new Button(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_BUT_TARGET)) { button.setTarget(jsonReader.nextString()); } else if (name.equals(JSON_KEY_BUT_TITLE)) { button.setTitle(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } catch (Exception e) { try { jsonReader.beginArray(); jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_BUT_TARGET)) { button.setTarget(jsonReader.nextString()); } else if (name.equals(JSON_KEY_BUT_TITLE)) { button.setTitle(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); jsonReader.endArray(); } catch (IOException e2) { } } return button; }
/** Parse single metric body */ void parseBody() throws IOException { // reset values String metricId = null; int lastRowId = 0; String instanceId = null; String serverName = null; String name = null; JSONObject parameters = null; _reader.beginObject(); while (_reader.hasNext()) { String property = _reader.nextName(); switch (property) { case "uid": metricId = nextStringOrNull(); break; case "last_rowid": lastRowId = _reader.nextInt(); break; case "name": name = nextStringOrNull(); break; case "server": instanceId = nextStringOrNull(); break; case "tag_name": serverName = nextStringOrNull(); break; case "parameters": parameters = nextObjectOrNull(); break; default: _reader.skipValue(); break; } } _reader.endObject(); serverName = serverName == null ? instanceId : serverName; Metric metric = HTMITApplication.getDatabase() .getDataFactory() .createMetric(metricId, name, instanceId, serverName, lastRowId, parameters); String unit = metric.getUnit(); if (unit == null) { // Guess metric unit based on name if not given by server unit = HTMITApplication.getMetricUnit(name); metric.setUnit(unit); } _results.add(metric); }
private void readEarthquakes(JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String s = reader.nextName(); if (s.equals("features")) { readFeaturesArray(reader); } else { reader.skipValue(); } } reader.endObject(); }
public void deSerializeRepresentation(JsonReader reader) throws IOException { ArrayList<String[]> al = new ArrayList<String[]>(); reader.beginObject(); while (reader.hasNext()) { String[] kv = {reader.nextName(), reader.nextString()}; al.add(kv); } reader.endObject(); String[][] oldFormat = al.toArray(new String[al.size()][]); deSerializeRepresentation(oldFormat); }
private void readMessage(JsonReader reader) throws IOException { String name = null; String zone = null; String apn = null; String address = null; reader.beginObject(); while (reader.hasNext()) { tag = reader.nextName(); if (tag.equals("name")) { name = reader.nextString(); } else if (tag.equals("zone")) { zone = reader.nextString(); } else if (tag.equals("apn")) { apn = reader.nextString(); } else if (tag.equals("address")) { address = reader.nextString(); } else if (tag.equals("feature") && reader.peek() != JsonToken.NULL) { polygonPath = readLatLngArray(reader); } else { reader.skipValue(); } } // while reader.endObject(); // create new marker and polygon if (address != null && !address.isEmpty()) { address = address + ", COSTA MESA, CA"; } polyopts = new PolygonOptions().addAll(polygonPath); polyopts.fillColor(randomColor()); polyopts.strokeColor(Color.YELLOW); polyopts.strokeWidth(1); polygon = map.addPolygon(polyopts); marker = map.addMarker( new MarkerOptions() .position(polygon.getPoints().get(0)) .title(address) .snippet("APN :[" + apn + "] Zone : (" + zone + ") ") .anchor(0.5f, 0.5f) .infoWindowAnchor(0.5f, 0.5f) .icon(BitmapDescriptorFactory.fromResource(R.drawable.house2))); marker.setVisible(false); }
private void readSnippetGroup(List<SnippetListItem> listSnippetItems, JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("recommendation_basis")) { listSnippetItems.add(new SnippetHeader(reader.nextString())); } else if (name.equals("articles")) { readArticlesArray(listSnippetItems, reader); } } reader.endObject(); }
public Response(JsonReader jsonReader) { results = new HashMap<String, String>(); try { jsonReader.beginObject(); while (jsonReader.peek() != JsonToken.END_OBJECT) { String label = jsonReader.nextName(); results.put(label, jsonReader.nextString()); } jsonReader.endObject(); } catch (IOException e) { Log.i("RESPONSE :", e.getMessage()); } }
public List<Image> readImages(JsonReader reader) throws IOException { List<Image> images = new ArrayList<>(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("images".equals(name)) { images.addAll(readImagesArray(reader)); } else { reader.skipValue(); } } reader.endObject(); return images; }
private List<Post> loadBoard(JsonReader reader) throws Exception { ArrayList<Post> list = new ArrayList<>(); reader.beginObject(); // Threads array if (reader.nextName().equals("threads")) { reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); // Thread object if (reader.nextName().equals("posts")) { reader.beginArray(); list.add(readPostObject(reader)); // Only consume one post while (reader.hasNext()) reader.skipValue(); reader.endArray(); } else { reader.skipValue(); } reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } reader.endObject(); return list; }
private ArrayList<Category> parseCategoriesList(JsonReader reader) throws IOException { ArrayList<Category> result = new ArrayList<>(); String name = reader.nextName(); reader.beginArray(); while (reader.hasNext()) { Category category = parseCategory(reader); if (category != null) { result.add(category); } } reader.endArray(); return result; }
/** Peeks at the next token and returns a {@link org.json.JSONObject} or {@code null} */ JSONObject nextObjectOrNull() throws IOException { if (_reader.peek() == JsonToken.NULL) { _reader.nextNull(); return null; } JSONObject json = new JSONObject(); String name; Object value; _reader.beginObject(); while (_reader.hasNext()) { name = _reader.nextName(); switch (_reader.peek()) { case STRING: value = nextStringOrNull(); break; case BEGIN_ARRAY: value = nextArrayOrNull(); break; case BEGIN_OBJECT: value = nextObjectOrNull(); break; case BOOLEAN: value = nextBooleanOrNull(); break; case NUMBER: value = nextNumberOrNull(); break; case NULL: _reader.nextNull(); value = null; break; default: _reader.skipValue(); value = null; } try { json.put(name, value); } catch (JSONException e) { Log.e( getClass().getSimpleName(), "Failed to parse JSON object: " + name + ", value = " + value); } } _reader.endObject(); return json; }