Java Code Examples for android.util.JsonReader#skipValue()

The following examples show how to use android.util.JsonReader#skipValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@NonNull
private static CrashlyticsReport.FilesPayload.File parseFile(@NonNull JsonReader jsonReader)
    throws IOException {
  final CrashlyticsReport.FilesPayload.File.Builder builder =
      CrashlyticsReport.FilesPayload.File.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "filename":
        builder.setFilename(jsonReader.nextString());
        break;
      case "contents":
        builder.setContents(Base64.decode(jsonReader.nextString(), Base64.NO_WRAP));
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();

  return builder.build();
}
 
Example 2
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 6 votes vote down vote up
OpenHours readOpeningHours(JsonReader reader) throws IOException {
    boolean openNow = false;
    List<OpenPeriod> periods = null;

    reader.beginObject();
    while (reader.hasNext()) {
        switch (reader.nextName()) {
            case "open_now":
                openNow = reader.nextBoolean();
                break;
            case "periods":
                periods = readOpenPeriodsArray(reader);
                break;
            default:
                reader.skipValue();
                break;
        }
    }
    reader.endObject();
    return new OpenHours(openNow, periods);
}
 
Example 3
Source File: BlocklistProcessor.java    From firefox-echo-show with Mozilla Public License 2.0 6 votes vote down vote up
public static Map<String, Trie> loadCategoryMap(final JsonReader reader, final Map<String, Trie> categoryMap, final ListType listType) throws IOException {
    reader.beginObject();

    while (reader.hasNext()) {
        final String name = reader.nextName();

        if (name.equals("categories")) {
            extractCategories(reader, categoryMap, listType);
        } else {
            reader.skipValue();
        }
    }

    reader.endObject();

    return categoryMap;
}
 
Example 4
Source File: GeoPoint.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void setCoordinatesFromJSONStream(JsonReader reader, int crs) throws IOException {
    setCRS(crs);
    reader.beginArray();
    int pos = 0;
    while (reader.hasNext()) {
        if(pos == 0)
            mX = reader.nextDouble();
        else if(pos == 1)
            mY = reader.nextDouble();
        else
            reader.skipValue();
        pos++;
    }
    reader.endArray();
}
 
Example 5
Source File: DecisionTree.java    From android-galaxyzoo with GNU General Public License v3.0 6 votes vote down vote up
private static void readJsonQuestion(final JsonReader reader, final Question question) throws IOException {
    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        switch (name) {
            case "text":
                question.setText(reader.nextString());
                break;
            case "title":
                question.setTitle(reader.nextString());
                break;
            case "help":
                question.setHelp(reader.nextString());
                break;
            case "answers":
                readJsonAnswers(reader, question);
                break;
            case "checkboxes":
                readJsonCheckboxes(reader, question);
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
}
 
Example 6
Source File: GalleryData.java    From NClientV2 with Apache License 2.0 6 votes vote down vote up
private void readImages(JsonReader jr) throws IOException {
    int actualPage=0;
    jr.beginObject();
    while (jr.peek()!=JsonToken.END_OBJECT){
        switch (jr.nextName()){
            case "cover":
                cover= new Page(ImageType.COVER,jr);
                break;
            case "pages":
                jr.beginArray();
                while(jr.hasNext())
                    pages.add(new Page(ImageType.PAGE,jr,actualPage++));
                jr.endArray();
                break;
            case "thumbnail":
                thumbnail= new Page(ImageType.THUMBNAIL,jr);
                break;
            default:
                jr.skipValue();
                break;
        }
    }
    jr.endObject();
    pages.trimToSize();
}
 
Example 7
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@NonNull
private static CrashlyticsReport.Session.Application parseApp(@NonNull JsonReader jsonReader)
    throws IOException {
  final CrashlyticsReport.Session.Application.Builder builder =
      CrashlyticsReport.Session.Application.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "identifier":
        builder.setIdentifier(jsonReader.nextString());
        break;
      case "version":
        builder.setVersion(jsonReader.nextString());
        break;
      case "displayVersion":
        builder.setDisplayVersion(jsonReader.nextString());
        break;
      case "installationUuid":
        builder.setInstallationUuid(jsonReader.nextString());
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();

  return builder.build();
}
 
Example 8
Source File: GeoJSONUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean readGeoJSONCRS(InputStream is, Context context) throws IOException, NGException {
    JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    boolean isWGS = true;
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if(name.equals(GeoConstants.GEOJSON_CRS)) {
            reader.beginObject();
            while (reader.hasNext()) {
                name = reader.nextName();
                if(name.equals(GeoConstants.GEOJSON_PROPERTIES)) {
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String subname = reader.nextName();
                        if(subname.equals(GeoConstants.GEOJSON_NAME)){
                            String val = reader.nextString();
                            isWGS = checkCRSSupportAndWGS(val, context);
                        }
                        else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    reader.close();
    return isWGS;
}
 
Example 9
Source File: MyViewModel.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private List<Earthquake> parseJson(InputStream in) throws IOException {
  // Create a new Json Reader to parse the input.
  JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

  try {
    // Create an empty list of earthquakes.
    List<Earthquake> earthquakes = null;

    // The root node of the Earthquake JSON feed is an object that
    // we must parse.
    reader.beginObject();
    while (reader.hasNext()) {
      String name = reader.nextName();

      // We are only interested in one sub-object: the array of
      // earthquakes labeled as features.
      if (name.equals("features")) {
        earthquakes = readEarthquakeArray(reader);
      } else {
        // We will ignore all other root level values and objects.
        reader.skipValue();
      }
    }
    reader.endObject();

    return earthquakes;

  } finally {
    reader.close();
  }
}
 
Example 10
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<AlternativePlaceId> readAltIdsArray(JsonReader reader) throws IOException {
    List<AlternativePlaceId> altIds = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        String placeId = null;
        PlaceScope scope = null;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "place_id":
                    placeId = reader.nextString();
                    break;
                case "scope":
                    scope = readScope(reader);
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        altIds.add(new AlternativePlaceId(placeId, scope));
    }
    reader.endArray();
    return altIds;
}
 
Example 11
Source File: DecisionTree.java    From android-galaxyzoo with GNU General Public License v3.0 5 votes vote down vote up
private void readJsonQuestions(final JsonReader reader) throws IOException {
    reader.beginObject();
    while (reader.hasNext()) {
        final String questionId = reader.nextName();

        final Question question = questionsMap.get(questionId);
        if (question != null) {
            readJsonQuestion(reader, question);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
}
 
Example 12
Source File: JsonUtils.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<AddressComponent> readAddressComponentsArray(JsonReader reader) throws IOException {
    List<AddressComponent> addressComponents = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        String longName = null;
        String shortName = null;
        List<AddressComponentType> types = null;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "long_name":
                    longName = reader.nextString();
                    break;
                case "short_name":
                    shortName = reader.nextString();
                    break;
                case "types":
                    types = readAddressComponentTypesArray(reader);
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        addressComponents.add(new AddressComponent(longName, shortName, types));
    }
    reader.endArray();
    return addressComponents;
}
 
Example 14
Source File: Page.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public Page(ImageType type, JsonReader reader,int page)throws IOException{
    this.imageType=type;
    this.page=page;
    reader.beginObject();
    while (reader.peek()!= JsonToken.END_OBJECT){
        switch (reader.nextName()){
            case "t":imageExt=stringToExt(reader.nextString()); break;
            case "w":size.setWidth(reader.nextInt());  break;
            case "h":size.setHeight(reader.nextInt()); break;
            default:reader.skipValue();break;
        }
    }
    reader.endObject();
}
 
Example 15
Source File: User.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public User(JsonReader reader) throws IOException {
    reader.beginObject();
    while(reader.peek()!= JsonToken.END_OBJECT){
        switch (reader.nextName()){
            case "id":id=reader.nextInt();break;
            case "post_date":username=reader.nextString();break;
            case "avatar_url":avatarUrl=reader.nextString();break;
            default:reader.skipValue();break;
        }
    }
    reader.endObject();
}
 
Example 16
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@NonNull
private static CrashlyticsReport.Session parseSession(@NonNull JsonReader jsonReader)
    throws IOException {
  final CrashlyticsReport.Session.Builder builder = CrashlyticsReport.Session.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "generator":
        builder.setGenerator(jsonReader.nextString());
        break;
      case "identifier":
        builder.setIdentifierFromUtf8Bytes(
            Base64.decode(jsonReader.nextString(), Base64.NO_WRAP));
        break;
      case "startedAt":
        builder.setStartedAt(jsonReader.nextLong());
        break;
      case "endedAt":
        builder.setEndedAt(jsonReader.nextLong());
        break;
      case "crashed":
        builder.setCrashed(jsonReader.nextBoolean());
        break;
      case "user":
        builder.setUser(parseUser(jsonReader));
        break;
      case "app":
        builder.setApp(parseApp(jsonReader));
        break;
      case "os":
        builder.setOs(parseOs(jsonReader));
        break;
      case "device":
        builder.setDevice(parseDevice(jsonReader));
        break;
      case "events":
        builder.setEvents(parseArray(jsonReader, CrashlyticsReportJsonTransform::parseEvent));
        break;
      case "generatorType":
        builder.setGeneratorType(jsonReader.nextInt());
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();

  return builder.build();
}
 
Example 17
Source File: BatchManagerTest.java    From background-geolocation-android with Apache License 2.0 4 votes vote down vote up
private List<BackgroundLocation> readLocationsArray(JsonReader reader) throws IOException {
    List<BackgroundLocation> locations = new ArrayList<BackgroundLocation>();
    reader.beginArray();
    while (reader.hasNext()) {
        BackgroundLocation l = new BackgroundLocation();
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("id")) {
                l.setLocationId(reader.nextLong());
            } else if (name.equals("time")) {
                l.setTime(reader.nextLong());
            } else if (name.equals("latitude")) {
                l.setLatitude(reader.nextDouble());
            } else if (name.equals("longitude")) {
                l.setLongitude(reader.nextDouble());
            } else if (name.equals("accuracy")) {
                l.setAccuracy((float)reader.nextDouble());
            } else if (name.equals("speed")) {
                l.setSpeed((float)reader.nextDouble());
            } else if (name.equals("bearing")) {
                l.setBearing((float)reader.nextDouble());
            } else if (name.equals("altitude")) {
                l.setAltitude(reader.nextDouble());
            } else if (name.equals("radius")) {
                JsonToken token = reader.peek();
                if (token != JsonToken.NULL) {
                    l.setRadius((float)reader.nextDouble());
                } else {
                    reader.skipValue();
                }
            } else if (name.equals("provider")) {
                l.setProvider(reader.nextString());
            } else if (name.equals("locationProvider")) {
                l.setLocationProvider(reader.nextInt());
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        locations.add(l);
    }
    reader.endArray();
    return locations;
}
 
Example 18
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
private static void skipJoinSplits(JsonReader reader) throws IOException {
  reader.skipValue();
}
 
Example 19
Source File: XulDataNode.java    From starcor.xul with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static XulDataNode buildFromJson(Reader reader) {
	JsonReader jsonReader = new JsonReader(reader);
	ArrayList<XulDataNode> parseStack = new ArrayList<XulDataNode>();
	XulDataNode lastDataNode = null;
	try {
		JsonToken peek;
		while ((peek = jsonReader.peek()) != JsonToken.END_DOCUMENT) {
			switch (peek) {
			case NAME:
				String nameStr = jsonReader.nextName();
				if (!jsonReader.hasNext()) {
					continue;
				}
				switch ((peek = jsonReader.peek())) {
				case BEGIN_OBJECT:
				case BEGIN_ARRAY:
					if (peek == JsonToken.BEGIN_OBJECT) {
						jsonReader.beginObject();
					} else {
						jsonReader.beginArray();
					}
					lastDataNode = lastDataNode.appendChild(nameStr);
					parseStack.add(lastDataNode);
					break;
				case STRING:
				case NUMBER:
					lastDataNode.setAttribute(nameStr, jsonReader.nextString());
					break;
				case BOOLEAN:
					lastDataNode.setAttribute(nameStr, String.valueOf(jsonReader.nextBoolean()));
					break;
				case NULL:
					jsonReader.nextNull();
					lastDataNode.setAttribute(nameStr, null);
					break;
				}
				break;
			case BEGIN_OBJECT:
			case BEGIN_ARRAY:
				if (peek == JsonToken.BEGIN_OBJECT) {
					jsonReader.beginObject();
				} else {
					jsonReader.beginArray();
				}
				if (lastDataNode != null) {
					lastDataNode = lastDataNode.appendChild(String.valueOf(lastDataNode.size()));
				} else {
					lastDataNode = XulDataNode.obtainDataNode(null);
				}
				parseStack.add(lastDataNode);
				break;
			case END_OBJECT:
			case END_ARRAY:
				if (peek == JsonToken.END_OBJECT) {
					jsonReader.endObject();
				} else {
					jsonReader.endArray();
				}
				parseStack.remove(parseStack.size() - 1);
				if (!parseStack.isEmpty()) {
					lastDataNode = parseStack.get(parseStack.size() - 1);
				}
				break;
			default:
				// array
				switch (peek) {
				case STRING:
				case NUMBER:
					lastDataNode.appendChild(String.valueOf(lastDataNode.size()), jsonReader.nextString());
					break;
				case BOOLEAN:
					lastDataNode.appendChild(String.valueOf(lastDataNode.size()), String.valueOf(jsonReader.nextBoolean()));
					break;
				case NULL:
					jsonReader.nextNull();
					lastDataNode.appendChild(String.valueOf(lastDataNode.size()));
					break;
				default:
					jsonReader.skipValue();
					break;
				}
				break;
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return lastDataNode;
}
 
Example 20
Source File: ContentFileParser.java    From flutter_whatsapp_stickers with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NonNull
private static StickerPack readStickerPack(@NonNull JsonReader reader) throws IOException, IllegalStateException {
    reader.beginObject();
    String identifier = null;
    String name = null;
    String publisher = null;
    String trayImageFile = null;
    String publisherEmail = null;
    String publisherWebsite = null;
    String privacyPolicyWebsite = null;
    String licenseAgreementWebsite = null;
    String imageDataVersion = "";
    boolean avoidCache = false;
    List<Sticker> stickerList = null;
    while (reader.hasNext()) {
        String key = reader.nextName();
        switch (key) {
            case "identifier":
                identifier = reader.nextString();
                break;
            case "name":
                name = reader.nextString();
                break;
            case "publisher":
                publisher = reader.nextString();
                break;
            case "tray_image_file":
                trayImageFile = reader.nextString();
                break;
            case "publisher_email":
                publisherEmail = reader.nextString();
                break;
            case "publisher_website":
                publisherWebsite = reader.nextString();
                break;
            case "privacy_policy_website":
                privacyPolicyWebsite = reader.nextString();
                break;
            case "license_agreement_website":
                licenseAgreementWebsite = reader.nextString();
                break;
            case "stickers":
                stickerList = readStickers(reader);
                break;
            case "image_data_version":
                imageDataVersion = reader.nextString();
                break;
            case "avoid_cache":
                avoidCache = reader.nextBoolean();
                break;
            default:
                reader.skipValue();
        }
    }
    if (TextUtils.isEmpty(identifier)) {
        throw new IllegalStateException("identifier cannot be empty");
    }
    if (TextUtils.isEmpty(name)) {
        throw new IllegalStateException("name cannot be empty");
    }
    if (TextUtils.isEmpty(publisher)) {
        throw new IllegalStateException("publisher cannot be empty");
    }
    if (TextUtils.isEmpty(trayImageFile)) {
        throw new IllegalStateException("tray_image_file cannot be empty");
    }
    if (stickerList == null || stickerList.size() == 0) {
        throw new IllegalStateException("sticker list is empty");
    }
    if (identifier.contains("..") || identifier.contains("/")) {
        throw new IllegalStateException("identifier should not contain .. or / to prevent directory traversal");
    }
    if (TextUtils.isEmpty(imageDataVersion)) {
        throw new IllegalStateException("image_data_version should not be empty");
    }
    reader.endObject();
    final StickerPack stickerPack = new StickerPack(identifier, name, publisher, trayImageFile, publisherEmail, publisherWebsite, privacyPolicyWebsite, licenseAgreementWebsite, imageDataVersion, avoidCache);
    stickerPack.setStickers(stickerList);
    return stickerPack;
}