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

The following examples show how to use android.util.JsonReader#nextName() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
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 2
/**
 * 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 3
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
@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 5
@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 6
@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 7
@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 8
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 9
Source Project: NClientV2   File: Comment.java    License: 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 10
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 11
Source Project: NClientV2   File: GalleryData.java    License: 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 12
/**
 * 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 13
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 14
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 15
Source Project: Android-Keyboard   File: MetadataParser.java    License: 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 16
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 17
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
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 19
private static String readFieldString(JsonReader reader) throws IOException {
  reader.nextName();
  return reader.nextString();
}
 
Example 20
Source Project: United4   File: United.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Plays the given song, by finding its R.raw id in the songs map, stored in properties
 * If notify is set set to true, will notify the webpage. For more information, see NotifierService's documentation
 *
 * @param song   the full name of the song to play
 * @param reload whether to notify the web page
 */
public static void playSong(String song, final boolean reload) {
    Log.i(TAG, "playSong called on " + song);
    // Find the filename in the map
    String songs = P.get("songs");
    JsonReader reader = new JsonReader(new StringReader(songs));
    String id = null;
    try {
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            String read = reader.nextString();
            //noinspection EqualsReplaceableByObjectsCall
            if (name.equals(song)) {
                id = read;
                Log.i(TAG, "Song id is " + id);
                break;
            }
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    // If we couldn't find it, fucking die
    if (id == null) {
        Log.e(TAG, "Song not found");
        throw new IllegalArgumentException("song not found");
    }
    Log.i(TAG, "Loading sound");
    // Stop any song in progress
    stop();
    // Set some properties in case the javascript forgot to set them
    P.set("is_playing", "true");
    P.set("current_song", song);
    if (player != null) {
        player.stop();
    }
    LoadControl loadControl = new DefaultLoadControl();
    player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
    player.addListener(new SongDoneListener());

    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "la/u/ncher"), bandwidthMeter);

    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

    MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("https://niles.xyz/valhalla_music/" + id + ".mp3"), dataSourceFactory, extractorsFactory, null, null);
    player.setPlayWhenReady(true);
    player.prepare(mediaSource);
    if (reload) {
        NotifierService.notify(NotifierService.NotificationType.RELOAD_MUSIC);
    }
}