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

The following examples show how to use android.util.JsonReader#peek() . 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: 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 2
Source File: BundleDeltaClient.java    From react-native-GPay with MIT License 6 votes vote down vote up
private static int patchDelta(JsonReader jsonReader, LinkedHashMap<Number, byte[]> map)
  throws IOException {
  jsonReader.beginArray();

  int numModules = 0;
  while (jsonReader.hasNext()) {
    jsonReader.beginArray();

    int moduleId = jsonReader.nextInt();

    if (jsonReader.peek() == JsonToken.NULL) {
      jsonReader.skipValue();
      map.remove(moduleId);
    } else {
      map.put(moduleId, jsonReader.nextString().getBytes());
    }

    jsonReader.endArray();
    numModules++;
  }

  jsonReader.endArray();

  return numModules;
}
 
Example 3
Source File: GalleryData.java    From NClientV2 with Apache License 2.0 6 votes vote down vote up
private void parseJSON(JsonReader jr) throws IOException {
    jr.beginObject();
    while(jr.peek()!= JsonToken.END_OBJECT){
        switch(jr.nextName()){
            case "upload_date":uploadDate=new Date(jr.nextLong()*1000);break;
            case "num_favorites":favoriteCount=jr.nextInt();break;
            case "media_id":mediaId=jr.nextInt();break;
            case "title":readTitles(jr);break;
            case "images":readImages(jr); break;
            case "tags":readTags(jr);break;
            case "id":id=jr.nextInt();break;
            case "num_pages":pageCount=jr.nextInt();break;
            case "error":jr.skipValue(); valid=false;break;
            default:jr.skipValue();break;
        }
    }
    jr.endObject();
}
 
Example 4
Source File: LogResponse.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@NonNull
public static LogResponse fromJson(@NonNull Reader reader) throws IOException {
  JsonReader jsonReader = new JsonReader(reader);
  try {
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
      String name = jsonReader.nextName();
      if (name.equals("nextRequestWaitMillis")) {
        if (jsonReader.peek() == JsonToken.STRING) {
          return LogResponse.create(Long.parseLong(jsonReader.nextString()));
        } else {
          return LogResponse.create(jsonReader.nextLong());
        }
      }
      jsonReader.skipValue();
    }
    throw new IOException("Response is missing nextRequestWaitMillis field.");
  } finally {
    jsonReader.close();
  }
}
 
Example 5
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 6
Source File: JSDebuggerWebSocketClient.java    From react-native-GPay with MIT License 5 votes vote down vote up
@Override
public void onMessage(WebSocket webSocket, String text) {
  Integer replyID = null;

  try {
    JsonReader reader = new JsonReader(new StringReader(text));
    String result = null;
    reader.beginObject();
    while (reader.hasNext()) {
      String field = reader.nextName();

      if (JsonToken.NULL == reader.peek()) {
        reader.skipValue();
        continue;
      }

      if ("replyID".equals(field)) {
        replyID = reader.nextInt();
      } else if ("result".equals(field)) {
        result = reader.nextString();
      } else if ("error".equals(field)) {
        String error = reader.nextString();
        abort(error, new JavascriptException(error));
      }
    }
    if (replyID != null) {
      triggerRequestSuccess(replyID, result);
    }
  } catch (IOException e) {
    if (replyID != null) {
      triggerRequestFailure(replyID, e);
    } else {
      abort("Parsing response message from websocket failed", e);
    }
  }
}
 
Example 7
Source File: BlocklistProcessor.java    From firefox-echo-show with Mozilla Public License 2.0 5 votes vote down vote up
private static void extractSite(final JsonReader reader, final UrlListCallback callback) throws IOException {
    reader.beginObject();

    final String siteOwner = reader.nextName();
    {
        reader.beginObject();

        while (reader.hasNext()) {
            // We can get the site name using reader.nextName() here:
            reader.skipValue();

            JsonToken nextToken = reader.peek();

            if (nextToken.name().equals("STRING")) {
                // Sometimes there's a "dnt" entry, with unspecified purpose.
                reader.skipValue();
            } else {
                reader.beginArray();

                while (reader.hasNext()) {
                    final String blockURL = reader.nextString();
                    callback.put(blockURL, siteOwner);
                }

                reader.endArray();
            }
        }

        reader.endObject();
    }

    reader.endObject();
}
 
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: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Feature readNGWFeature(
        JsonReader reader,
        List<Field> fields,
        int nSRS)
        throws IOException, IllegalStateException, NumberFormatException, OutOfMemoryError
{
    final Feature feature = new Feature(Constants.NOT_FOUND, fields);

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(NGWUtil.NGWKEY_ID)) {
            feature.setId(reader.nextLong());
        } else if (name.equals(NGWUtil.NGWKEY_GEOM)) {
            String wkt = reader.nextString();
            GeoGeometry geom = GeoGeometryFactory.fromWKT(wkt, nSRS);
            geom.setCRS(nSRS);
            if (nSRS != GeoConstants.CRS_WEB_MERCATOR) {
                geom.project(GeoConstants.CRS_WEB_MERCATOR);
            }
            feature.setGeometry(geom);
        } else if (name.equals(NGWUtil.NGWKEY_FIELDS)) {
            readNGWFeatureFields(feature, reader, fields);
        } else if (name.equals(NGWUtil.NGWKEY_EXTENSIONS)) {
            if (reader.peek() != JsonToken.NULL) {
                readNGWFeatureAttachments(feature, reader);
            }
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return feature;
}
 
Example 10
Source File: User.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public User(JsonReader reader) throws IOException {
    reader.beginObject();
    while(reader.peek()!= JsonToken.END_OBJECT){
        switch (reader.nextName()){
            case "id":id=reader.nextInt();break;
            case "post_date":username=reader.nextString();break;
            case "avatar_url":avatarUrl=reader.nextString();break;
            default:reader.skipValue();break;
        }
    }
    reader.endObject();
}
 
Example 11
Source File: VersionChecker.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
private static String[] parseVersionJson(JsonReader jr) throws IOException {
    String[]vars=new String[3];//ver code,body,changelog
    jr.beginObject();
    while(jr.peek()!=JsonToken.END_OBJECT){
        switch (jr.nextName()){
            case "tag_name":vars[0]=jr.nextString();break;
            case "body":vars[1]=jr.nextString();break;
            case "assets":
                jr.beginArray();
                while(jr.hasNext()){
                    if(vars[2]!=null){
                        jr.skipValue();
                        continue;
                    }
                    jr.beginObject();
                    while(jr.peek()!=JsonToken.END_OBJECT){
                        if ("browser_download_url".equals(jr.nextName())) {
                            String url=jr.nextString();
                            if(url.contains("Release"))
                                vars[2] = url;
                        } else {
                            jr.skipValue();
                        }
                    }
                    jr.endObject();
                }
                jr.endArray();
                break;
            default:jr.skipValue();break;
        }
    }
    return vars;
}
 
Example 12
Source File: BlocklistProcessor.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
private static void extractSite(final JsonReader reader, final UrlListCallback callback) throws IOException {
    reader.beginObject();

    final String siteOwner = reader.nextName();
    {
        reader.beginObject();

        while (reader.hasNext()) {
            // We can get the site name using reader.nextName() here:
            reader.skipValue();

            JsonToken nextToken = reader.peek();

            if (nextToken.name().equals("STRING")) {
                // Sometimes there's a "dnt" entry, with unspecified purpose.
                reader.skipValue();
            } else {
                reader.beginArray();

                while (reader.hasNext()) {
                    final String blockURL = reader.nextString();
                    callback.put(blockURL, siteOwner);
                }

                reader.endArray();
            }
        }

        reader.endObject();
    }

    reader.endObject();
}
 
Example 13
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 14
Source File: Tag.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public Tag(JsonReader jr) throws IOException {
    jr.beginObject();
    while(jr.peek()!= JsonToken.END_OBJECT){
        switch (jr.nextName()){
            case "count":count=jr.nextInt();break;
            case "type":type=TagType.typeByName(jr.nextString());break;
            case "id":id=jr.nextInt();break;
            case "name":name=jr.nextString();break;
            case "url": LogUtility.d("Tag URL: "+jr.nextString());break;
            default:jr.skipValue();break;
        }
    }
    jr.endObject();
}
 
Example 15
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 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: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
private static ZCashTransactionDetails_taddr readTx(JsonReader reader) throws IOException, IllegalStateException {
  ZCashTransactionDetails_taddr tx = new ZCashTransactionDetails_taddr();
  if(reader.peek() != JsonToken.BEGIN_OBJECT) {
    throw new IOException("Cannot parse JSON");
  }

  reader.beginObject();
  while (reader.peek() != JsonToken.END_OBJECT) {
    String name = reader.nextName();
    switch (name) {
      case HASH:
        tx.hash = reader.nextString();
        break;
      case MAINCHAIN:
        tx.mainChain = reader.nextBoolean();
        break;
      case FEE:
        tx.fee = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case TYPE:
        tx.type = reader.nextString();
        break;
      case SHIELDED:
        tx.shielded = reader.nextBoolean();
        break;
      case INDEX:
        tx.index = reader.nextLong();
        break;
      case BLOCKHASH:
        tx.blockHash = reader.nextString();
        break;
      case BLOCKHEIGHT:
        tx.blockHeight = reader.nextLong();
        break;
      case VERSION:
        tx.version = reader.nextLong();
        break;
      case LOCKTIME:
        tx.locktime = reader.nextLong();
        break;
      case TIME:
        tx.time = reader.nextLong();
        break;
      case TIMESTAMP:
        tx.timestamp = reader.nextLong();
        break;
      case VIN:
        tx.vin = readTxInputs(reader);
        break;
      case VOUT:
        tx.vout = readTxOutputs(reader, null);
        break;
      case VJOINSPLIT:
        skipJoinSplits(reader);
        break;
      case VALUE:
        tx.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case OUTPUTVALUE:
        tx.outputValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case SHIELDEDVALUE:
        tx.shieldedValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      default:
        reader.skipValue();
    }
  }

  reader.endObject();
  return tx;
}
 
Example 18
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
private static ZCashTransactionDetails_taddr readTx(JsonReader reader) throws IOException, IllegalStateException {
  ZCashTransactionDetails_taddr tx = new ZCashTransactionDetails_taddr();
  if(reader.peek() != JsonToken.BEGIN_OBJECT) {
    throw new IOException("Cannot parse JSON");
  }

  reader.beginObject();
  while (reader.peek() != JsonToken.END_OBJECT) {
    String name = reader.nextName();
    switch (name) {
      case HASH:
        tx.hash = reader.nextString();
        break;
      case MAINCHAIN:
        tx.mainChain = reader.nextBoolean();
        break;
      case FEE:
        tx.fee = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case TYPE:
        tx.type = reader.nextString();
        break;
      case SHIELDED:
        tx.shielded = reader.nextBoolean();
        break;
      case INDEX:
        tx.index = reader.nextLong();
        break;
      case BLOCKHASH:
        tx.blockHash = reader.nextString();
        break;
      case BLOCKHEIGHT:
        tx.blockHeight = reader.nextLong();
        break;
      case VERSION:
        tx.version = reader.nextLong();
        break;
      case LOCKTIME:
        tx.locktime = reader.nextLong();
        break;
      case TIME:
        tx.time = reader.nextLong();
        break;
      case TIMESTAMP:
        tx.timestamp = reader.nextLong();
        break;
      case VIN:
        tx.vin = readTxInputs(reader);
        break;
      case VOUT:
        tx.vout = readTxOutputs(reader, null);
        break;
      case VJOINSPLIT:
        skipJoinSplits(reader);
        break;
      case VALUE:
        tx.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case OUTPUTVALUE:
        tx.outputValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case SHIELDEDVALUE:
        tx.shieldedValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      default:
        reader.skipValue();
    }
  }

  reader.endObject();
  return tx;
}
 
Example 19
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 20
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();
}