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

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

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "key":
        builder.setKey(jsonReader.nextString());
        break;
      case "value":
        builder.setValue(jsonReader.nextString());
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();
  return builder.build();
}
 
Example 2
Source File: BlocklistProcessor.java    From focus-android 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 3
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 4
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 6 votes vote down vote up
DateTimePair readDateTimePair(JsonReader reader) throws IOException {
    String day = null;
    String time = null;

    reader.beginObject();
    while (reader.hasNext()) {
        switch (reader.nextName()) {
            case "day":
                day = reader.nextString();
                break;
            case "time":
                time = reader.nextString();
                break;
            default:
                reader.skipValue();
                break;
        }
    }
    reader.endObject();
    return new DateTimePair(day, time);
}
 
Example 5
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void readNGWFeatureAttachments(Feature feature, JsonReader reader)
        throws IOException
{
    //add extensions
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("attachment") && reader.peek() != JsonToken.NULL) {
            reader.beginArray();
            while (reader.hasNext()) {
                readNGWFeatureAttachment(feature, reader);
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }

    reader.endObject();
}
 
Example 6
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@NonNull
private static Event.Application parseEventApp(@NonNull JsonReader jsonReader)
    throws IOException {
  final Event.Application.Builder builder = Event.Application.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "background":
        builder.setBackground(jsonReader.nextBoolean());
        break;
      case "uiOrientation":
        builder.setUiOrientation(jsonReader.nextInt());
        break;
      case "execution":
        builder.setExecution(parseEventExecution(jsonReader));
        break;
      case "customAttributes":
        builder.setCustomAttributes(
            parseArray(jsonReader, CrashlyticsReportJsonTransform::parseCustomAttribute));
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();
  return builder.build();
}
 
Example 7
Source File: SampleChooserActivity.java    From ExoPlayer-Offline with Apache License 2.0 5 votes vote down vote up
private void readSampleGroup(JsonReader reader, List<SampleGroup> groups) throws IOException {
  String groupName = "";
  ArrayList<Sample> samples = new ArrayList<>();

  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    switch (name) {
      case "name":
        groupName = reader.nextString();
        break;
      case "samples":
        reader.beginArray();
        while (reader.hasNext()) {
          samples.add(readEntry(reader, false));
        }
        reader.endArray();
        break;
      case "offline_samples":
        reader.beginArray();
        while (reader.hasNext()){
          samples.add(readOfflineEntry(reader));
        }
        reader.endArray();
        break;
      case "_comment":
        reader.nextString(); // Ignore.
        break;
      default:
        throw new ParserException("Unsupported name: " + name);
    }
  }
  reader.endObject();

  SampleGroup group = getGroup(groupName, groups);
  group.samples.addAll(samples);
}
 
Example 8
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void readNGWDate(Feature feature, JsonReader reader, String fieldName)
        throws IOException
{
    reader.beginObject();
    int nYear = 1900;
    int nMonth = 1;
    int nDay = 1;
    int nHour = 0;
    int nMinute = 0;
    int nSecond = 0;
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(NGWUtil.NGWKEY_YEAR)) {
            nYear = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_MONTH)) {
            nMonth = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_DAY)) {
            nDay = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_HOUR)) {
            nHour = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_MINUTE)) {
            nMinute = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_SECOND)) {
            nSecond = reader.nextInt();
        } else {
            reader.skipValue();
        }
    }

    TimeZone timeZone = TimeZone.getDefault();
    timeZone.setRawOffset(0); // set to UTC
    Calendar calendar = new GregorianCalendar(timeZone);
    calendar.set(nYear, nMonth - 1, nDay, nHour, nMinute, nSecond);
    calendar.set(Calendar.MILLISECOND, 0); // we must to reset millis
    feature.setFieldValue(fieldName, calendar.getTimeInMillis());

    reader.endObject();
}
 
Example 9
Source File: MyViewModel.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public Earthquake readEarthquake(JsonReader reader) throws IOException {
  String id = null;
  Location location = null;
  Earthquake earthquakeProperties = null;

  reader.beginObject();

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

    if (name.equals("id")) {
      // The ID is stored as a value.
      id = reader.nextString();
    } else if (name.equals("geometry")) {
      // The location is stored as a geometry object
      // that must be parsed.
      location = readLocation(reader);
    } else if (name.equals("properties")) {
      // Most of the earthquake details are stored as a
      // properties object that must be parsed.
      earthquakeProperties = readEarthquakeProperties(reader);
    } else {
      reader.skipValue();
    }
  }

  reader.endObject();

  // Construct a new Earthquake based on the parsed details.
  return new Earthquake(id,
    earthquakeProperties.getDate(),
    earthquakeProperties.getDetails(),
    location,
    earthquakeProperties.getMagnitude(),
    earthquakeProperties.getLink());
}
 
Example 10
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@NonNull
private static Event.Application.Execution.BinaryImage parseEventBinaryImage(
    @NonNull JsonReader jsonReader) throws IOException {
  final Event.Application.Execution.BinaryImage.Builder builder =
      Event.Application.Execution.BinaryImage.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "name":
        builder.setName(jsonReader.nextString());
        break;
      case "baseAddress":
        builder.setBaseAddress(jsonReader.nextLong());
        break;
      case "size":
        builder.setSize(jsonReader.nextLong());
        break;
      case "uuid":
        builder.setUuidFromUtf8Bytes(Base64.decode(jsonReader.nextString(), Base64.NO_WRAP));
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();
  return builder.build();
}
 
Example 11
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 12
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 13
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 14
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 15
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 16
Source File: CrashlyticsReportJsonTransform.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@NonNull
private static Event.Application.Execution parseEventExecution(@NonNull JsonReader jsonReader)
    throws IOException {
  final Event.Application.Execution.Builder builder = Event.Application.Execution.builder();

  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String name = jsonReader.nextName();
    switch (name) {
      case "threads":
        builder.setThreads(
            parseArray(jsonReader, CrashlyticsReportJsonTransform::parseEventThread));
        break;
      case "exception":
        builder.setException(parseEventExecutionException(jsonReader));
        break;
      case "signal":
        builder.setSignal(parseEventSignal(jsonReader));
        break;
      case "binaries":
        builder.setBinaries(
            parseArray(jsonReader, CrashlyticsReportJsonTransform::parseEventBinaryImage));
        break;
      default:
        jsonReader.skipValue();
        break;
    }
  }
  jsonReader.endObject();
  return builder.build();
}
 
Example 17
Source File: SampleChooserActivity.java    From ExoPlayer-Offline with Apache License 2.0 4 votes vote down vote up
private Sample readEntry(JsonReader reader, boolean insidePlaylist) throws IOException {
  String sampleName = null;
  String uri = null;
  String extension = null;
  UUID drmUuid = null;
  String drmLicenseUrl = null;
  String[] drmKeyRequestProperties = null;
  boolean preferExtensionDecoders = false;
  ArrayList<UriSample> playlistSamples = null;

  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    switch (name) {
      case "name":
        sampleName = reader.nextString();
        break;
      case "uri":
        uri = reader.nextString();
        break;
      case "extension":
        extension = reader.nextString();
        break;
      case "drm_scheme":
        Assertions.checkState(!insidePlaylist, "Invalid attribute on nested item: drm_scheme");
        drmUuid = getDrmUuid(reader.nextString());
        break;
      case "drm_license_url":
        Assertions.checkState(!insidePlaylist,
            "Invalid attribute on nested item: drm_license_url");
        drmLicenseUrl = reader.nextString();
        break;
      case "drm_key_request_properties":
        Assertions.checkState(!insidePlaylist,
            "Invalid attribute on nested item: drm_key_request_properties");
        ArrayList<String> drmKeyRequestPropertiesList = new ArrayList<>();
        reader.beginObject();
        while (reader.hasNext()) {
          drmKeyRequestPropertiesList.add(reader.nextName());
          drmKeyRequestPropertiesList.add(reader.nextString());
        }
        reader.endObject();
        drmKeyRequestProperties = drmKeyRequestPropertiesList.toArray(new String[0]);
        break;
      case "prefer_extension_decoders":
        Assertions.checkState(!insidePlaylist,
            "Invalid attribute on nested item: prefer_extension_decoders");
        preferExtensionDecoders = reader.nextBoolean();
        break;
      case "playlist":
        Assertions.checkState(!insidePlaylist, "Invalid nesting of playlists");
        playlistSamples = new ArrayList<>();
        reader.beginArray();
        while (reader.hasNext()) {
          playlistSamples.add((UriSample) readEntry(reader, true));
        }
        reader.endArray();
        break;
      default:
        throw new ParserException("Unsupported attribute name: " + name);
    }
  }
  reader.endObject();

  if (playlistSamples != null) {
    UriSample[] playlistSamplesArray = playlistSamples.toArray(
        new UriSample[playlistSamples.size()]);
    return new PlaylistSample(sampleName, drmUuid, drmLicenseUrl, drmKeyRequestProperties,
        preferExtensionDecoders, playlistSamplesArray);
  } else {
    return new UriSample(sampleName, drmUuid, drmLicenseUrl, drmKeyRequestProperties,
        preferExtensionDecoders, uri, extension);
  }
}
 
Example 18
Source File: GeoJSONUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void createLayerFromGeoJSONStream(VectorLayer layer, InputStream in, IProgressor progressor, boolean isWGS84) throws IOException, NGException {
    int streamSize = in.available();
    if(null != progressor){
        progressor.setIndeterminate(false);
        progressor.setMax(streamSize);
        progressor.setMessage(layer.getContext().getString(R.string.start_fill_layer) + " " + layer.getName());
    }

    SQLiteDatabase db = null;
    if(layer.getFields() != null && layer.getFields().isEmpty()){
        db = DatabaseContext.getDbForLayer(layer);
    }

    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    long counter = 0;
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(GeoConstants.GEOJSON_TYPE_FEATURES)) {
            reader.beginArray();
            while (reader.hasNext()) {
                Feature feature = readGeoJSONFeature(reader, layer, isWGS84);
                if (null != feature) {
                    if (layer.getFields() == null || layer.getFields().isEmpty()) {
                        if (feature.getGeometry() != null)
                            layer.create(feature.getGeometry().getType(), feature.getFields());

                        db = DatabaseContext.getDbForLayer(layer);
                    }

                    if (feature.getGeometry() != null) {
                        layer.createFeatureBatch(feature, db);
                        if(null != progressor){
                            if (progressor.isCanceled()) {
                                layer.save();
                                return;
                            }
                            progressor.setValue(streamSize - in.available());
                            progressor.setMessage(layer.getContext().getString(R.string.process_features) + ": " + counter++);
                        }
                    }
                }
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    reader.close();

    //if(null != db)
    //   db.close(); // return pragma to init
    layer.save();
}
 
Example 19
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void readNGWFeatureFields(Feature feature, JsonReader reader, List<Field> fields)
        throws IOException, IllegalStateException, NumberFormatException
{
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (reader.peek() == JsonToken.NULL) {
            reader.skipValue();
        } else {
            boolean bAdded = false;
            for (Field field : fields) {
                if (field.getName().equals(name) || field.getName().equals(LayerUtil.normalizeFieldName(name))) {
                    switch (field.getType()) {
                        case GeoConstants.FTReal:
                            feature.setFieldValue(field.getName(), reader.nextDouble());
                            bAdded = true;
                            break;
                        case GeoConstants.FTInteger:
                            feature.setFieldValue(field.getName(), reader.nextInt());
                            bAdded = true;
                            break;
                        case GeoConstants.FTString:
                            feature.setFieldValue(field.getName(), reader.nextString());
                            bAdded = true;
                            break;
                        case GeoConstants.FTDate:
                        case GeoConstants.FTTime:
                        case GeoConstants.FTDateTime:
                            readNGWDate(feature, reader, field.getName());
                            bAdded = true;
                            break;
                        default:
                            break;
                    }
                    break;
                }
            }
            if (!bAdded) {
                reader.skipValue();
            }
        }
    }
    reader.endObject();
}