com.google.api.client.json.JsonParser类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(164)

本文整理了Java中com.google.api.client.json.JsonParser类的一些代码示例,展示了JsonParser类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonParser类的具体详情如下:
包路径:com.google.api.client.json.JsonParser
类名称:JsonParser

JsonParser介绍

暂无

代码示例

代码示例来源:origin: com.google.api.client/google-api-client-googleapis

@Override
Object parseItemInternal() throws IOException {
 parser.nextToken();
 String key = parser.getText();
 if (key != "kind") {
  throw new IllegalArgumentException("expected kind field: " + key);
 }
 parser.nextToken();
 String kind = parser.getText();
 Class<?> itemClass = kindToItemClassMap.get(kind);
 if (itemClass == null) {
  throw new IllegalArgumentException("unrecognized kind: " + kind);
 }
 return parser.parse(itemClass, null);
}

代码示例来源:origin: firebase/firebase-admin-java

private Map<String, Object> parseCustomClaims(String customClaims, JsonFactory jsonFactory) {
 if (Strings.isNullOrEmpty(customClaims)) {
  return ImmutableMap.of();
 }
 try {
  Map<String, Object> parsed = new HashMap<>();
  jsonFactory.createJsonParser(customClaims).parseAndClose(parsed);
  return ImmutableMap.copyOf(parsed);
 } catch (IOException e) {
  throw new IllegalArgumentException("Failed to parse custom claims json", e);
 }
}

代码示例来源:origin: com.google.api.client/google-api-client-googleapis

/** Closes the underlying parser. */
public void close() throws IOException {
 this.parser.close();
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testSkipChildren_array() throws Exception {
 JsonParser parser = newFactory().createJsonParser(JSON_FEED);
 parser.nextToken();
 parser.skipToKey("entries");
 parser.skipChildren();
 assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testParser_anyType() throws Exception {
 JsonFactory factory = newFactory();
 JsonParser parser;
 parser = factory.createJsonParser(ANY_TYPE);
 parser.nextToken();
 AnyType result = parser.parse(AnyType.class);
 assertEquals(ANY_TYPE, factory.toString(result));
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testParser_emptyArray() throws Exception {
 JsonFactory factory = newFactory();
 String[] result = factory.createJsonParser(EMPTY_ARRAY).parse(String[].class);
 assertEquals(EMPTY_ARRAY, factory.toString(result));
 // check types and values
 assertEquals(0, result.length);
}

代码示例来源:origin: googlegenomics/dockerflow

/** Deserialize from json. */
public static <T> T fromJson(String s, Class<T> c) throws IOException {
 FileUtils.LOG.debug("Deserializing from json to " + c);
 T retval;
 // For some reason, this only works for auto-generated Google API
 // classes
 if (c.toString().startsWith("com.google.api.services.")) {
  FileUtils.LOG.debug("Using Google APIs JsonParser");
  retval = Utils.getDefaultJsonFactory().createJsonParser(s).parse(c);
 } else {
  FileUtils.LOG.debug("Using Gson");
  retval = new GsonBuilder().setLenient().create().fromJson(s, c);
 }
 return retval;
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testParser_partialEmpty() throws Exception {
 JsonFactory factory = newFactory();
 JsonParser parser;
 parser = factory.createJsonParser(EMPTY_OBJECT);
 parser.nextToken();
 parser.nextToken();
 // current token is now end_object
 @SuppressWarnings("unchecked")
 HashMap<String, Object> result = parser.parseAndClose(HashMap.class);
 assertEquals(EMPTY_OBJECT, factory.toString(result));
 // check types and values
 assertTrue(result.isEmpty());
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testParse_emptyMap() throws Exception {
 JsonParser parser = newFactory().createJsonParser(EMPTY_OBJECT);
 parser.nextToken();
 @SuppressWarnings("unchecked")
 HashMap<String, Object> map = parser.parseAndClose(HashMap.class);
 assertTrue(map.isEmpty());
}

代码示例来源:origin: com.google.api-client/google-api-client

JsonParser parser = null;
try {
 parser = jsonFactory.createJsonParser(response.getContent());
 JsonToken currentToken = parser.getCurrentToken();
  currentToken = parser.nextToken();
  parser.skipToKey("error");
  if (parser.getCurrentToken() == JsonToken.VALUE_STRING) {
   detailString = parser.getText();
  } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
   details = parser.parseAndClose(GoogleJsonError.class);
   detailString = details.toPrettyString();
  response.ignore();
 } else if (details == null) {
  parser.close();

代码示例来源:origin: com.google.api-client/google-api-client

clock.currentTimeMillis() + getCacheTimeInSec(certsResponse.getHeaders()) * 1000;
JsonParser parser = jsonFactory.createJsonParser(certsResponse.getContent());
JsonToken currentToken = parser.getCurrentToken();
 currentToken = parser.nextToken();
 while (parser.nextToken() != JsonToken.END_OBJECT) {
  parser.nextToken();
  String certValue = parser.getText();
  X509Certificate x509Cert = (X509Certificate) factory.generateCertificate(
    new ByteArrayInputStream(StringUtils.getBytesUtf8(certValue)));
 parser.close();

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testParse() throws Exception {
 JsonParser parser = newFactory().createJsonParser(CONTAINED_MAP);
 parser.nextToken();
 A a = parser.parse(A.class);
 assertEquals(ImmutableMap.of("title", "foo"), a.map);
}

代码示例来源:origin: com.octo.android.robospice/robospice-google-http-client

@Override
protected T readCacheDataFromFile(File file) throws CacheLoadingException {
  try {
    JsonParser jsonParser = jsonFactory.createJsonParser(new FileReader(file));
    T result = jsonParser.parse(getHandledClass(), null);
    jsonParser.close();
    return result;
  } catch (FileNotFoundException e) {
    // Should not occur (we test before if file exists)
    // Do not throw, file is not cached
    Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
    return null;
  } catch (Exception e) {
    throw new CacheLoadingException(e);
  }
}

代码示例来源:origin: com.google.auth/google-auth-library-oauth2-http

@Override
public AccessToken refreshAccessToken() throws IOException {
 Socket socket = new Socket("localhost", this.getAuthPort());
 socket.setSoTimeout(READ_TIMEOUT_MS);
 AccessToken token;
 try {
  OutputStream os = socket.getOutputStream();
  os.write(GET_AUTH_TOKEN_REQUEST_BYTES);
 
  BufferedReader input =
    new BufferedReader(new InputStreamReader(socket.getInputStream()));
  input.readLine(); // Skip over the first line
  JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser(input);
  List<Object> messageArray = (List<Object>) parser.parseArray(ArrayList.class, Object.class);
  String accessToken = messageArray.get(ACCESS_TOKEN_INDEX).toString();
  token =  new AccessToken(accessToken, null);
 } finally {
  socket.close();
 }
 return token;
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testCurrentToken() throws Exception {
 JsonParser parser = newFactory().createJsonParser(JSON_FEED);
 assertNull(parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.START_ARRAY, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
 parser.nextToken();
 assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
 parser.nextToken();

代码示例来源:origin: com.google.http-client/google-http-client-test

public final void testGson() throws Exception {
 byte[] asciiJson = Charsets.UTF_8.encode("{ \"foo\": 123 }").array();
 JsonParser jp =
   newFactory().createJsonParser(new ByteArrayInputStream(asciiJson), Charsets.UTF_8);
 assertEquals(com.google.api.client.json.JsonToken.START_OBJECT, jp.nextToken());
 assertEquals(com.google.api.client.json.JsonToken.FIELD_NAME, jp.nextToken());
 assertEquals(com.google.api.client.json.JsonToken.VALUE_NUMBER_INT, jp.nextToken());
 assertEquals(123, jp.getIntValue());
 assertEquals(com.google.api.client.json.JsonToken.END_OBJECT, jp.nextToken());
}

代码示例来源:origin: com.google.http-client/google-http-client-test

public void testNextToken() throws Exception {
 JsonParser parser = newFactory().createJsonParser(JSON_FEED);
 assertEquals(JsonToken.START_OBJECT, parser.nextToken());
 assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
 assertEquals(JsonToken.START_ARRAY, parser.nextToken());
 assertEquals(JsonToken.START_OBJECT, parser.nextToken());
 assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
 assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
 assertEquals(JsonToken.END_OBJECT, parser.nextToken());
 assertEquals(JsonToken.START_OBJECT, parser.nextToken());
 assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
 assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
 assertEquals(JsonToken.END_OBJECT, parser.nextToken());
 assertEquals(JsonToken.END_ARRAY, parser.nextToken());
 assertEquals(JsonToken.END_OBJECT, parser.nextToken());
 assertNull(parser.nextToken());
}

代码示例来源:origin: googleapis/google-cloud-java

new JacksonFactory()
  .createJsonParser(new FileInputStream(args[0]))
  .parseAndClose(String[].class);

代码示例来源:origin: com.google.api.client/google-api-client-googleapis

JsonParser parser = JsonHttpParser.parserForResponse(jsonFactory, response);
try {
 parser.skipToKey(response.isSuccessStatusCode ? "data" : "error");
 if (parser.getCurrentToken() == JsonToken.END_OBJECT) {
  throw new IllegalArgumentException("data key not found");
} finally {
 if (failed) {
  parser.close();

代码示例来源:origin: com.google.api.client/google-api-client-googleapis

/**
 * Forces the discovery document to be loaded, even if the service definition has already been
 * loaded.
 */
public void load() throws IOException {
 GoogleUrl url = discoveryUrl.clone();
 url.put("api", name);
 HttpRequest request = discoveryTransport.createRequestFactory().buildGetRequest(url);
 JsonParser parser = JsonCParser.parserForResponse(jsonFactory, request.execute());
 parser.skipToKey(name);
 DiscoveryDocument doc = new DiscoveryDocument();
 APIDefinition apiDefinition = doc.apiDefinition;
 parser.parseAndClose(apiDefinition, null);
 serviceDefinition = doc.apiDefinition.get(version);
 Preconditions.checkNotNull(serviceDefinition, "version not found: %s", version);
}

相关文章