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

The following examples show how to use android.util.JsonReader#nextName() . 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 parseNdkPayload(@NonNull JsonReader jsonReader)
    throws IOException {
  final CrashlyticsReport.FilesPayload.Builder builder = CrashlyticsReport.FilesPayload.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "files":
        builder.setFiles(parseArray(jsonReader, CrashlyticsReportJsonTransform::parseFile));
        break;
      case "orgId":
        builder.setOrgId(jsonReader.nextString());
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();

  return builder.build();
}
 
Example 2
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 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: AboutJSONParser.java    From pivaa with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Paring object
 * @param reader
 * @return
 * @throws IOException
 */
private AboutRecord readMessage(JsonReader reader) throws IOException {
    String name = "";
    String description = "";

    reader.beginObject();
    while (reader.hasNext()) {
        String key = reader.nextName();

        if (key.equals("name")) {
            name = reader.nextString();

        } else if (key.equals("description")) {
            description = reader.nextString();

        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return new AboutRecord(name, description);
}
 
Example 5
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 6
Source File: GalleryData.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
private void readTitles(JsonReader jr) throws IOException {
    jr.beginObject();
    while(jr.peek()!=JsonToken.END_OBJECT){
        switch (jr.nextName()){
            case "japanese":setTitle(TitleType.JAPANESE,jr.peek()!=JsonToken.NULL?jr.nextString():"");break;
            case "english": setTitle(TitleType.ENGLISH ,jr.peek()!=JsonToken.NULL?jr.nextString():"");break;
            case "pretty":  setTitle(TitleType.PRETTY  ,jr.peek()!=JsonToken.NULL?jr.nextString():"");break;
            default:jr.skipValue();break;
        }
        if(jr.peek()==JsonToken.NULL)jr.skipValue();
    }
    jr.endObject();
}
 
Example 7
Source File: AndroidSmartyStreetsApiJsonParser.java    From Smarty-Streets-AutoCompleteTextView with Apache License 2.0 5 votes vote down vote up
Address readPlace(JsonReader reader) throws IOException {
    String text = null;
    String streetLine = null;
    String city = null;
    String state = null;
    reader.beginObject();
    while (reader.hasNext()) {
        switch (reader.nextName()) {
            case "text":
                text = reader.nextString();
                break;
            case "street_line":
                streetLine = reader.nextString();
                break;
            case "city":
                city = reader.nextString();
                break;
            case "state":
                state = reader.nextString();
                break;
            default:
                reader.skipValue();
                break;
        }
    }
    reader.endObject();
    return new Address(text, streetLine, city, state);
}
 
Example 8
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private static ZCashTransactionInput readTxSingleInput(JsonReader reader) throws IOException {
  ZCashTransactionInput input = new ZCashTransactionInput();
  reader.beginObject();
  while (reader.peek() != JsonToken.END_OBJECT) {
    String name = reader.nextName();
    switch (name) {
      case COINBASE:
        input.coinbase = reader.nextString();
        break;
      case SEQUENCE:
        input.sequence = reader.nextLong();
        break;
      case TXID:
        input.txid = reader.nextString();
        break;
      case VOUT:
        input.n = reader.nextLong();
        break;
      case SCRIPTSIG:
        reader.skipValue();
        break;
      case RETRIEVEDVOUT:
        input.copyDataFrom(readTxSingleOutput(reader));
        break;
      default:
        reader.skipValue();
    }
  }

  reader.endObject();
  return input;
}
 
Example 9
Source File: PropertiesSingleton.java    From United4 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reads in all the properties from the json file or some generic default properties if that failed
 */
private static void init() {
    resetForAppStart();
    if (getProperty("migrated").isEmpty()) {
        try {
            JsonReader reader = new JsonReader(new InputStreamReader(United.getContext().openFileInput(CONFIG)));
            reader.beginObject();
            int read = 0;
            while (reader.hasNext()) {
                read++;
                String key = reader.nextName();
                String value = reader.nextString();
                Log.d(TAG, "MIGRATION: Read prop " + key);
                setProperty(key, value);
            }
            resetForAppStart();
            if (read == 0) {
                setFirstRunProperties();
            }
            setProperty("migrated", "success");
        } catch (Exception e) {
            e.printStackTrace();
            setFirstRunProperties();
            setProperty("migrated", "errors");
        }
    } else if (getProperty("migrated").equalsIgnoreCase("reset")) {
        setFirstRunProperties();
        setProperty("migrated", "reset_complete");
    }
}
 
Example 10
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@NonNull
private static CrashlyticsReport.Session.OperatingSystem parseOs(@NonNull JsonReader jsonReader)
    throws IOException {
  final CrashlyticsReport.Session.OperatingSystem.Builder builder =
      CrashlyticsReport.Session.OperatingSystem.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "platform":
        builder.setPlatform(jsonReader.nextInt());
        break;
      case "version":
        builder.setVersion(jsonReader.nextString());
        break;
      case "buildVersion":
        builder.setBuildVersion(jsonReader.nextString());
        break;
      case "jailbroken":
        builder.setJailbroken(jsonReader.nextBoolean());
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();

  return builder.build();
}
 
Example 11
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<MatchedSubstring> readMatchedSubstringsArray(JsonReader reader) throws IOException {
    List<MatchedSubstring> matchedSubstrings = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        int length = -1;
        int offset = -1;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "length":
                    length = reader.nextInt();
                    break;
                case "offset":
                    offset = reader.nextInt();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        matchedSubstrings.add(new MatchedSubstring(length, offset));
    }
    reader.endArray();
    return matchedSubstrings;
}
 
Example 12
Source File: Comment.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public Comment(JsonReader reader) throws IOException {
    reader.beginObject();
    while(reader.peek()!= JsonToken.END_OBJECT){
        switch (reader.nextName()){
            case "id":id=reader.nextInt();break;
            case "post_date":postDate=new Date(reader.nextLong()*1000);break;
            case "body":body=reader.nextString();break;
            case "poster":poster=new User(reader);break;
            default:reader.skipValue();break;
        }
    }
    reader.endObject();
}
 
Example 13
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<PlacePhoto> readPhotosArray(JsonReader reader) throws IOException {
    List<PlacePhoto> photos = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        int height = -1;
        int width = -1;
        String photoReference = null;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "height":
                    height = reader.nextInt();
                    break;
                case "width":
                    width = reader.nextInt();
                    break;
                case "photo_reference":
                    photoReference = reader.nextString();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        photos.add(new PlacePhoto(height, width, photoReference));
    }
    reader.endArray();
    return photos;
}
 
Example 14
Source File: ContentFileParser.java    From flutter_whatsapp_stickers with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NonNull
private static List<Sticker> readStickers(@NonNull JsonReader reader) throws IOException, IllegalStateException {
    reader.beginArray();
    List<Sticker> stickerList = new ArrayList<>();

    while (reader.hasNext()) {
        reader.beginObject();
        String imageFile = null;
        List<String> emojis = new ArrayList<>(LIMIT_EMOJI_COUNT);
        while (reader.hasNext()) {
            final String key = reader.nextName();
            if ("image_file".equals(key)) {
                imageFile = reader.nextString();
            } else if ("emojis".equals(key)) {
                reader.beginArray();
                while (reader.hasNext()) {
                    String emoji = reader.nextString();
                    emojis.add(emoji);
                }
                reader.endArray();
            } else {
                throw new IllegalStateException("unknown field in json: " + key);
            }
        }
        reader.endObject();
        if (TextUtils.isEmpty(imageFile)) {
            throw new IllegalStateException("sticker image_file cannot be empty");
        }
        if (!imageFile.endsWith(".webp")) {
            throw new IllegalStateException("image file for stickers should be webp files, image file is: " + imageFile);
        }
        if (imageFile.contains("..") || imageFile.contains("/")) {
            throw new IllegalStateException("the file name should not contain .. or / to prevent directory traversal, image file is:" + imageFile);
        }
        stickerList.add(new Sticker(imageFile, emojis));
    }
    reader.endArray();
    return stickerList;
}
 
Example 15
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 16
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
private static ZCashTransactionOutput readTxSingleOutput(JsonReader reader) throws IOException {
  ZCashTransactionOutput output = new ZCashTransactionOutput();
  reader.beginObject(); //output
  while (reader.peek() != JsonToken.END_OBJECT) {
    String name = reader.nextName();
    switch (name) {
      case N:
        output.n = reader.nextLong();
        break;
      case SCRIPTPUBKEY:
        reader.beginObject();
        while (reader.peek() != JsonToken.END_OBJECT) {
          name = reader.nextName();
          switch (name) {
            case ADDRESSES:
              reader.beginArray();
              while (reader.hasNext()) {
                output.address = reader.nextString();
              }
              reader.endArray();
              break;
            case ASM:
              output.asm = reader.nextString();
              break;
            case HEX:
              output.hex = reader.nextString();
              break;
            case REQSIGS:
              output.regSigs = reader.nextLong();
              break;
            case TYPE:
              output.type = reader.nextString();
              break;
            default:
              reader.skipValue();
          }
        }
        reader.endObject();
        break;
      case VALUE:
        output.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case VALUEZAT:
        output.value = reader.nextLong();
        break;
      default:
        reader.skipValue();
    }
  }

  reader.endObject(); //output end
  return output;
}
 
Example 17
Source File: GeoGeometryFactory.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static GeoGeometry fromJsonStream(JsonReader reader, int crs) throws IOException {
    GeoGeometry geometry = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if(name.equalsIgnoreCase(GeoConstants.GEOJSON_TYPE)){
            int type = typeFromString(reader.nextString());
            switch (type) {
                case GTPoint:
                    geometry = new GeoPoint();
                    break;
                case GTLineString:
                    geometry = new GeoLineString();
                    break;
                case GTPolygon:
                    geometry = new GeoPolygon();
                    break;
                case GTMultiPoint:
                    geometry = new GeoMultiPoint();
                    break;
                case GTMultiLineString:
                    geometry = new GeoMultiLineString();
                    break;
                case GTMultiPolygon:
                    geometry = new GeoMultiPolygon();
                    break;
                case GTGeometryCollection:
                    geometry = new GeoGeometryCollection();
                case GTNone:
                default:
                    break;
            }
        } else if(name.equalsIgnoreCase(GeoConstants.GEOJSON_COORDINATES)) {
            if (geometry == null)
                reader.skipValue();
            else
                geometry.setCoordinatesFromJSONStream(reader, crs);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    if (geometry != null) {
        geometry.setCRS(crs);
        if (!geometry.isValid())
            return null;
    }

    return geometry;
}
 
Example 18
Source File: MetadataParser.java    From Android-Keyboard with Apache License 2.0 4 votes vote down vote up
/**
 * Parse one JSON-formatted word list metadata.
 * @param reader the reader containing the data.
 * @return a WordListMetadata object from the parsed data.
 * @throws IOException if the underlying reader throws IOException during reading.
 */
private static WordListMetadata parseOneWordList(final JsonReader reader)
        throws IOException, BadFormatException {
    final TreeMap<String, String> arguments = new TreeMap<>();
    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        if (!TextUtils.isEmpty(name)) {
            arguments.put(name, reader.nextString());
        }
    }
    reader.endObject();
    if (TextUtils.isEmpty(arguments.get(ID_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(LOCALE_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(DESCRIPTION_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(UPDATE_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(FILESIZE_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(CHECKSUM_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(REMOTE_FILENAME_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(VERSION_FIELD_NAME))
            || TextUtils.isEmpty(arguments.get(FORMATVERSION_FIELD_NAME))) {
        throw new BadFormatException(arguments.toString());
    }
    // TODO: need to find out whether it's bulk or update
    // The null argument is the local file name, which is not known at this time and will
    // be decided later.
    return new WordListMetadata(
            arguments.get(ID_FIELD_NAME),
            MetadataDbHelper.TYPE_BULK,
            arguments.get(DESCRIPTION_FIELD_NAME),
            Long.parseLong(arguments.get(UPDATE_FIELD_NAME)),
            Long.parseLong(arguments.get(FILESIZE_FIELD_NAME)),
            arguments.get(RAW_CHECKSUM_FIELD_NAME),
            arguments.get(CHECKSUM_FIELD_NAME),
            MetadataDbHelper.DICTIONARY_RETRY_THRESHOLD /* retryCount */,
            null,
            arguments.get(REMOTE_FILENAME_FIELD_NAME),
            Integer.parseInt(arguments.get(VERSION_FIELD_NAME)),
            Integer.parseInt(arguments.get(FORMATVERSION_FIELD_NAME)),
            0, arguments.get(LOCALE_FIELD_NAME));
}
 
Example 19
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
private static String readFieldString(JsonReader reader) throws IOException {
  reader.nextName();
  return reader.nextString();
}