Java Code Examples for android.util.JsonReader

The following are top voted examples for showing how to use android.util.JsonReader. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: nifi-android-s2s   File: PeerListParser.java   View source code 7 votes vote down vote up
public static List<Peer> parsePeers(InputStream inputStream) throws IOException {
    List<Peer> result = null;
    JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, Charsets.UTF_8));
    try {
        jsonReader.beginObject();
        try {
            while (jsonReader.hasNext()) {
                if (PEERS.equals(jsonReader.nextName())) {
                    result = parsePeersArray(jsonReader);
                } else {
                    jsonReader.skipValue();
                }
            }
        } finally {
            jsonReader.endObject();
        }
    } finally {
        jsonReader.close();
    }
    return result == null ? Collections.<Peer>emptyList() : result;
}
 
Example 2
Project: GitHub   File: Realm.java   View source code 6 votes vote down vote up
/**
 * Creates a Realm object for each object in a JSON array. This must be done within a transaction.
 * JSON properties with unknown properties will be ignored. If a {@link RealmObject} field is not present in the
 * JSON object the {@link RealmObject} field will be set to the default value for that type.
 * <p>
 * This API is only available in API level 11 or later.
 *
 * <p>
 * This method currently does not support value list field.
 *
 * @param clazz type of Realm objects created.
 * @param inputStream the JSON array as a InputStream. All objects in the array must be of the specified class.
 * @throws RealmException if mapping from JSON fails.
 * @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
 * {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
 * @throws IOException if something was wrong with the input stream.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public <E extends RealmModel> void createAllFromJson(Class<E> clazz, InputStream inputStream) throws IOException {
    //noinspection ConstantConditions
    if (clazz == null || inputStream == null) {
        return;
    }
    checkIfValid();

    JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
    try {
        reader.beginArray();
        while (reader.hasNext()) {
            configuration.getSchemaMediator().createUsingJsonStream(clazz, this, reader);
        }
        reader.endArray();
    } finally {
        reader.close();
    }
}
 
Example 3
Project: ExoPlayer-Offline   File: SampleChooserActivity.java   View source code 6 votes vote down vote up
@Override
protected List<SampleGroup> doInBackground(String... uris) {
  List<SampleGroup> result = new ArrayList<>();
  Context context = getApplicationContext();
  String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
  DataSource dataSource = new DefaultDataSource(context, null, userAgent, false);
  for (String uri : uris) {
    DataSpec dataSpec = new DataSpec(Uri.parse(uri));
    InputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      readSampleGroups(new JsonReader(new InputStreamReader(inputStream, "UTF-8")), result);
    } catch (Exception e) {
      Log.e(TAG, "Error loading sample list: " + uri, e);
      sawError = true;
    } finally {
      Util.closeQuietly(dataSource);
    }
  }
  return result;
}
 
Example 4
Project: nifi-android-s2s   File: Port.java   View source code 6 votes vote down vote up
public Port(JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();
    String id = null;
    String name = null;
    while (jsonReader.hasNext()) {
        String key = jsonReader.nextName();
        if (id == null && ID.equals(key)) {
            id = jsonReader.nextString();
        } else if (name == null && NAME.equals(key)) {
            name = jsonReader.nextString();
        } else {
            jsonReader.skipValue();
        }
    }
    jsonReader.endObject();
    this.id = id;
    this.name = name;
}
 
Example 5
Project: BTNotifierAndroid   File: IncomingMessage.java   View source code 6 votes vote down vote up
public IncomingMessage(String message) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(message));
    reader.beginObject();
    if (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("event")) {
            String typeString = reader.nextString();
            if (typeString.equals("action")) {
                eventType = EventType.ACTION;
            } else if (typeString.equals("dismiss")) {
                eventType = EventType.DISMISS;
            } else if (typeString.equals("blacklist")) {
                eventType = EventType.BLACKLIST;
            } else if (typeString.equals("sms")) {
                eventType = EventType.SMS;
            }
        }
    }
    jsonReader = reader;
}
 
Example 6
Project: pivaa   File: AboutJSONParser.java   View source code 6 votes vote down vote up
/**
 * Reading JSON stream from InputStream
 * @param in
 * @return
 * @throws IOException
 */
private ArrayList<AboutRecord> readJsonStream(InputStream in) {
    try {
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        try {
            return readMessagesArray(reader);
        } finally {
            reader.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    ArrayList<AboutRecord> list = new ArrayList<AboutRecord>();
    return list;
}
 
Example 7
Project: pivaa   File: AboutJSONParser.java   View source code 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 8
Project: firefox-tv   File: BlocklistProcessor.java   View source code 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 9
Project: aos-MediaLib   File: MovieIdParser.java   View source code 6 votes vote down vote up
private static String readName(JsonReader reader) throws IOException {
    reader.beginObject();
    String name;
    String returnValue = null;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_NAME:
                returnValue = reader.nextString();
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
    return returnValue;
}
 
Example 10
Project: aos-MediaLib   File: MovieIdParser.java   View source code 6 votes vote down vote up
private static void readCasts(JsonReader reader, MovieTags target) throws IOException {
    reader.beginObject();
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_CAST:
                readActors(reader, target);
                break;
            case KEY_CREW:
                readDirectors(reader, target);
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
}
 
Example 11
Project: aos-MediaLib   File: MovieIdParser.java   View source code 6 votes vote down vote up
private static String readDirector(JsonReader reader) throws IOException {
    reader.beginObject();
    String name;
    String personName = null;
    boolean isDirector = false;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_NAME:
                personName = reader.nextString();
                break;
            case KEY_JOB:
                if (DIRECTOR.equals(reader.nextString()))
                    isDirector = true;
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
    return isDirector ? personName : null;
}
 
Example 12
Project: aos-MediaLib   File: MovieIdParser.java   View source code 6 votes vote down vote up
private static void readActor(JsonReader reader, MovieTags target) throws IOException {
    reader.beginObject();
    String name;
    String personName = null;
    String personRole = null;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_NAME:
                personName = reader.nextString();
                break;
            case KEY_CHARACTER:
                personRole = reader.nextString();
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
    target.addActorIfAbsent(personName, personRole);
}
 
Example 13
Project: aos-MediaLib   File: MovieIdParser.java   View source code 6 votes vote down vote up
private static String readCountry(JsonReader reader) throws IOException {
    String certification = null;
    boolean isUS = false;

    reader.beginObject();
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_CERTIFICATION:
                certification = reader.nextString();
                break;
            case KEY_ISO_3166:
                if (COUNTRY_US.equals(reader.nextString()))
                    isUS = true;
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();

    return isUS ? certification : null;
}
 
Example 14
Project: aos-MediaLib   File: MovieIdImageParser.java   View source code 6 votes vote down vote up
@Override
protected MovieIdImagesResult getResult(JsonReader reader, String config) throws IOException {
    MovieIdImagesResult myResult = new MovieIdImagesResult();
    LinkedList<Image> posters = new LinkedList<Image>();
    LinkedList<Image> backdrops = new LinkedList<Image>();

    reader.beginObject();
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_POSTERS:
                readImages(reader, posters, config);
                break;
            case KEY_BACKDROPS:
                readImages(reader, backdrops, config);
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();

    myResult.posterPaths = getSortedPaths(posters);
    myResult.backdropPaths = getSortedPaths(backdrops);
    return myResult;
}
 
Example 15
Project: aos-MediaLib   File: MovieIdImageParser.java   View source code 6 votes vote down vote up
private static Image readImage(JsonReader reader, String preferredLanguage, int imageNumber) throws IOException {
    reader.beginObject();
    String filePath = null;
    String language = null;
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_FILE_PATH:
                filePath = reader.nextString();
                break;
            case KEY_ISO_639_1:
                language = reader.nextString();
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
    return new Image(filePath, language, preferredLanguage, imageNumber);
}
 
Example 16
Project: aos-MediaLib   File: SearchMovieParser.java   View source code 6 votes vote down vote up
@Override
protected List<SearchResult> getResult(JsonReader reader, Integer config) throws IOException {
    List<SearchResult> result = new LinkedList<SearchResult>();

    reader.beginObject();
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_RESULTS:
                readResults(reader, result, config);
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();

    return result;
}
 
Example 17
Project: aos-MediaLib   File: SearchMovieParser.java   View source code 6 votes vote down vote up
public static SearchResult readResult(JsonReader reader) throws IOException {
    SearchResult item = new SearchResult();

    reader.beginObject();
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_TITLE:
                item.setTitle(reader.nextString());
                break;
            case KEY_ID:
                item.setId(reader.nextInt());
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();

    return item;
}
 
Example 18
Project: aos-MediaLib   File: SearchMovieTrailerParser.java   View source code 6 votes vote down vote up
@Override
protected List<SearchMovieTrailerResult.TrailerResult> getResult(JsonReader reader, Integer config) throws IOException {
    List<SearchMovieTrailerResult.TrailerResult> result = new LinkedList<SearchMovieTrailerResult.TrailerResult>();

    reader.beginObject();
    String name;
    while ((name = getNextNotNullName(reader)) != null) {
        switch(MATCHER.match(name)) {
            case KEY_RESULTS:
                readResults(reader, result, 40);
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();

    return result;
}
 
Example 19
Project: webtrekk-android-sdk   File: WebtrekkRecommendations.java   View source code 6 votes vote down vote up
private boolean processResponseMain(JsonReader reader) {
    try {
        mRequestResults = new ArrayList<>();
        reader.beginArray();
        while (reader.hasNext())
        {
            reader.beginObject();
            if (reader.nextName().equals("reco"))
            {
                processResponseRecoItem(reader, mRequestResults);
            }else
                reader.skipValue();
            reader.endObject();
        }
        reader.endArray();
    } catch (IOException e) {
        mQueryResult = QueryRecommendationResult.INCORRECT_RESPONSE;
        WebtrekkLogging.log("Incorrect response structure:"+e);
        mRequestResults = null;
        return false;
    }
    return true;
}
 
Example 20
Project: focus-android   File: BlocklistProcessor.java   View source code 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 21
Project: werewolves-app   File: RoleLoader.java   View source code 6 votes vote down vote up
private static Role readRole(JsonReader reader) throws IOException {
    String name = null;
    String description = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String property = reader.nextName();
        switch (property) {
            case NAME_PROPERTY:
                name = reader.nextString();
                break;
            case DESCRIPTION_PROPERTY:
                description = reader.nextString();
                break;
            default:
                Log.w(LOG_TAG, "Unknown property: " + property);
                break;
        }
    }
    if (name == null || description == null) {
        Log.w(LOG_TAG, "name: " + name + ", description: " + description);
        return null;
    }
    reader.endObject();
    return new Role(name, description);
}
 
Example 22
Project: SoldierWeather   File: Utility.java   View source code 6 votes vote down vote up
/**
 * 解析和处理服务器返回的数据
 * @param soldierWeatherDb
 * @param in
 * @return
 */
public static boolean handleResponse(SoldierWeatherDB soldierWeatherDb, InputStream in) {
    LogUtil.log("Utility", "handleResponse", LogUtil.DEBUG);
    soldierWeatherDB = soldierWeatherDb;
    JsonReader reader = new JsonReader(new InputStreamReader(in));
    boolean flag = false;
    try {
        reader.beginObject();
        while (reader.hasNext()) {
            String nodeName = reader.nextName();
            if (nodeName.equals("resultcode")) {
                LogUtil.log("Utility", "resultcode = " + reader.nextString(), LogUtil.NOTHING);
                flag = true;
            } else if (nodeName.equals("result") && flag) {
                saveAreaToDatabase(reader);
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 23
Project: cat-avatar-generator-app   File: AvatarPartDb.java   View source code 6 votes vote down vote up
private void readParts(JsonReader reader) throws IOException {
    reader.beginArray();
    Vector<AvatarPart> parts = null;
    String currentCategory = "";
    while (reader.hasNext()) {
        reader.beginArray();
        String category = reader.nextString();
        if (!category.equals(currentCategory)) {
            parts = new Vector<>();
            mParts.put(category, parts);
            currentCategory = category;
        }
        AvatarPart part = null;
        String filename = reader.nextString();
        int x = reader.nextInt();
        int y = reader.nextInt();
        if (!TextUtils.isEmpty(filename)) {
            part = new AvatarPart(filename, x, y);
        }
        assert parts != null;
        parts.add(part);
        reader.endArray();
    }
    reader.endArray();
}
 
Example 24
Project: memoria-politica   File: CandidateDao.java   View source code 6 votes vote down vote up
private List<Politician> findCandidates(List<String> cpfList, JsonReader reader) throws IOException {
    reader.beginArray();

    List<Politician> candidates = new ArrayList<>();
    while (reader.hasNext()) {
        reader.beginObject();
        String polCpf = reader.nextName();
        int index = cpfList.indexOf(polCpf);
        if (index != -1) {
            Politician pol = new Politician(polCpf);
            readJsonObject(reader, pol);
            candidates.add(pol);
        }
        else {
            reader.skipValue();
        }
        reader.endObject();
    }

    reader.endArray();
    return candidates;
}
 
Example 25
Project: cosi   File: FileUtils.java   View source code 6 votes vote down vote up
private static JSONObject readRootObject(JsonReader reader, boolean isFolder) throws IOException, JSONException {
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();

        if (name.equals("value")) {
            JSONObject result = FileUtils.readObject(reader, isFolder);
            reader.endObject();
            return result;
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    return null;
}
 
Example 26
Project: lottie-android   File: ColorParser.java   View source code 6 votes vote down vote up
@Override public Integer parse(JsonReader reader, float scale) throws IOException {
  boolean isArray = reader.peek() == JsonToken.BEGIN_ARRAY;
  if (isArray) {
    reader.beginArray();
  }
  double r = reader.nextDouble();
  double g = reader.nextDouble();
  double b = reader.nextDouble();
  double a = reader.nextDouble();
  if (isArray) {
    reader.endArray();
  }

  if (r <= 1 && g <= 1 && b <= 1 && a <= 1) {
    r *= 255;
    g *= 255;
    b *= 255;
    a *= 255;
  }

  return Color.argb((int) a, (int) r, (int) g, (int) b);
}
 
Example 27
Project: lottie-android   File: AnimatableTextPropertiesParser.java   View source code 6 votes vote down vote up
public static AnimatableTextProperties parse(
    JsonReader reader, LottieComposition composition) throws IOException {
  AnimatableTextProperties anim = null;

  reader.beginObject();
  while (reader.hasNext()) {
    switch (reader.nextName()) {
      case "a":
        anim = parseAnimatableTextProperties(reader, composition);
        break;
      default:
        reader.skipValue();
    }
  }
  reader.endObject();
  if (anim == null) {
    // Not sure if this is possible.
    return new AnimatableTextProperties(null, null, null, null);
  }
  return anim;
}
 
Example 28
Project: lottie-android   File: MergePathsParser.java   View source code 6 votes vote down vote up
static MergePaths parse(JsonReader reader) throws IOException {
  String name = null;
  MergePaths.MergePathsMode mode = null;

  while (reader.hasNext()) {
    switch (reader.nextName()) {
      case "nm":
        name = reader.nextString();
        break;
      case "mm":
        mode =  MergePaths.MergePathsMode.forId(reader.nextInt());
        break;
      default:
        reader.skipValue();
    }
  }

  return new MergePaths(name, mode);
}
 
Example 29
Project: lottie-android   File: LottieCompositionParser.java   View source code 6 votes vote down vote up
private static void parseLayers(JsonReader reader, LottieComposition composition,
    List<Layer> layers, LongSparseArray<Layer> layerMap) throws IOException {
  int imageCount = 0;
  reader.beginArray();
  while (reader.hasNext()) {
    Layer layer = LayerParser.parse(reader, composition);
    if (layer.getLayerType() == Layer.LayerType.Image) {
      imageCount++;
    }
    layers.add(layer);
    layerMap.put(layer.getId(), layer);

    if (imageCount > 4) {
      L.warn("You have " + imageCount + " images. Lottie should primarily be " +
          "used with shapes. If you are using Adobe Illustrator, convert the Illustrator layers" +
          " to shape layers.");
    }
  }
  reader.endArray();
}
 
Example 30
Project: lottie-android   File: LottieCompositionParser.java   View source code 6 votes vote down vote up
private static void parseFonts(JsonReader reader, Map<String, Font> fonts) throws IOException {

    reader.beginObject();
    while (reader.hasNext()) {
      switch (reader.nextName()) {
        case "list":
          reader.beginArray();
          while (reader.hasNext()) {
            Font font = FontParser.parse(reader);
            fonts.put(font.getName(), font);
          }
          reader.endArray();
          break;
        default:
          reader.skipValue();
      }
    }
    reader.endObject();
  }
 
Example 31
Project: lottie-android   File: ShapePathParser.java   View source code 6 votes vote down vote up
static ShapePath parse(
    JsonReader reader, LottieComposition composition) throws IOException {
  String name = null;
  int ind = 0;
  AnimatableShapeValue shape = null;

  while (reader.hasNext()) {
    switch (reader.nextName()) {
      case "nm":
        name = reader.nextString();
        break;
      case "ind":
        ind = reader.nextInt();
        break;
      case "ks":
        shape = AnimatableValueParser.parseShapeData(reader, composition);
        break;
      default:
        reader.skipValue();
    }
  }

  return new ShapePath(name, ind, shape);
}
 
Example 32
Project: lottie-android   File: PointFParser.java   View source code 6 votes vote down vote up
@Override public PointF parse(JsonReader reader, float scale) throws IOException {
  JsonToken token = reader.peek();
  if (token == JsonToken.BEGIN_ARRAY) {
    return JsonUtils.jsonToPoint(reader, scale);
  } else if (token == JsonToken.BEGIN_OBJECT) {
    return JsonUtils.jsonToPoint(reader, scale);
  } else if (token == JsonToken.NUMBER) {
    // This is the case where the static value for a property is an array of numbers.
    // We begin the array to see if we have an array of keyframes but it's just an array
    // of static numbers instead.
    PointF point = new PointF((float) reader.nextDouble() * scale, (float) reader.nextDouble() * scale);
    while (reader.hasNext()) {
      reader.skipValue();
    }
    return point;
  } else {
    throw new IllegalArgumentException("Cannot convert json to point. Next token is " + token);
  }
}
 
Example 33
Project: lottie-android   File: JsonUtils.java   View source code 6 votes vote down vote up
private static PointF jsonObjectToPoint(JsonReader reader, float scale) throws IOException {
  float x = 0f;
  float y = 0f;
  reader.beginObject();
  while (reader.hasNext()) {
    switch (reader.nextName()) {
      case "x":
        x = valueFromObject(reader);
        break;
      case "y":
        y = valueFromObject(reader);
        break;
      default:
        reader.skipValue();
    }
  }
  reader.endObject();
  return new PointF(x * scale, y * scale);
}
 
Example 34
Project: lottie-android   File: JsonUtils.java   View source code 6 votes vote down vote up
static float valueFromObject(JsonReader reader) throws IOException {
  JsonToken token = reader.peek();
  switch (token) {
    case NUMBER:
      return (float) reader.nextDouble();
    case BEGIN_ARRAY:
      reader.beginArray();
      float val = (float) reader.nextDouble();
      while (reader.hasNext()) {
        reader.skipValue();
      }
      reader.endArray();
      return val;
    default:
      throw new IllegalArgumentException("Unknown value for token of type " + token);
  }
}
 
Example 35
Project: segment-android   File: JsonUtils.java   View source code 6 votes vote down vote up
/** Reads the next value in the {@link JsonReader}. */
private static Object readValue(JsonReader reader) throws IOException {
  JsonToken token = reader.peek();
  switch (token) {
    case BEGIN_OBJECT:
      return readerToMap(reader);
    case BEGIN_ARRAY:
      return readerToList(reader);
    case BOOLEAN:
      return reader.nextBoolean();
    case NULL:
      reader.nextNull(); // consume the null token
      return null;
    case NUMBER:
      return reader.nextDouble();
    case STRING:
      return reader.nextString();
    default:
      throw new IllegalStateException("Invalid token " + token);
  }
}
 
Example 36
Project: nuntius-android   File: IncomingMessage.java   View source code 6 votes vote down vote up
public IncomingMessage(String message) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(message));
    reader.beginObject();
    if (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("event")) {
            String typeString = reader.nextString();
            if (typeString.equals("action")) {
                eventType = EventType.ACTION;
            } else if (typeString.equals("dismiss")) {
                eventType = EventType.DISMISS;
            } else if (typeString.equals("blacklist")) {
                eventType = EventType.BLACKLIST;
            } else if (typeString.equals("sms")) {
                eventType = EventType.SMS;
            }
        }
    }
    jsonReader = reader;
}
 
Example 37
Project: BusMan   File: ConfigureTagActivity.java   View source code 6 votes vote down vote up
@Override
public void readJson(JsonReader reader) throws IOException {
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();

        switch (name) {
            case FIELD_ID:
                this.id = reader.nextString();
                break;
            case FIELD_NAME:
                this.name = reader.nextString();
                break;
            case FIELD_COMMENT:
                reader.skipValue();
                break;
            default:
                if (Log.isLoggable(TAG, Log.WARN)) {
                    Log.w(TAG, "Unknown rider key: " + name);
                }
                reader.skipValue();
                break;
        }
    }
    reader.endObject();
}
 
Example 38
Project: SMSGateway   File: JSONParser.java   View source code 6 votes vote down vote up
public SMSMessage parse() throws IOException {
    JsonReader jsonReader = new JsonReader(new StringReader(jsonString));

    String recipient = null;
    String message = null;

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        String attributeName = jsonReader.nextName();
        if (attributeName.equals("sender_recipient")) {
            recipient = jsonReader.nextString();
        } else if (attributeName.equals("text")) {
            message = jsonReader.nextString();
        } else {
            jsonReader.skipValue();
        }
    }
    jsonReader.endObject();

    return new SMSMessage(recipient, message);
}
 
Example 39
Project: autoparse-json   File: JsonParserUtils.java   View source code 6 votes vote down vote up
/**
 * Parse the next value as an object, but do not attempt to convert it or any children to a
 * known type. The returned object will be a {@link JSONObject} and all children will be
 * JSONObjects, JSONArrays, and primitives.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 */
public static JSONObject parseAsJsonObject(JsonReader reader, String key) throws IOException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_OBJECT);
    JSONObject result = new JSONObject();
    reader.beginObject();
    while (reader.hasNext()) {
        try {
            result.put(reader.nextName(), parseNextValue(reader, false));
        } catch (JSONException e) {
            throw new RuntimeException("This should be impossible.", e);
        }
    }
    reader.endObject();
    return result;
}
 
Example 40
Project: nexus-gallery   File: ImagePreset.java   View source code 6 votes vote down vote up
/**
 * populates preset from JSON stream
 *
 * @param sreader a JSON string
 * @return true on success if false ImagePreset is undefined
 */
public boolean readJson(JsonReader sreader) throws IOException {
    sreader.beginObject();

    while (sreader.hasNext()) {
        String name = sreader.nextName();
        FilterRepresentation filter = creatFilterFromName(name);
        if (filter == null) {
            Log.w(LOGTAG, "UNKNOWN FILTER! " + name);
            return false;
        }
        filter.deSerializeRepresentation(sreader);
        addFilter(filter);
    }
    sreader.endObject();
    return true;
}