com.google.api.client.json.JsonParser.parseAndClose()方法的使用及代码示例

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

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

JsonParser.parseAndClose介绍

暂无

代码示例

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

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

代码示例来源: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

@Override
public <T> T parse(HttpResponse response, Class<T> dataClass) throws IOException {
 return parserForResponse(jsonFactory, response).parseAndClose(dataClass, null);
}

代码示例来源:origin: com.google.cloud.bigdataoss/bigquery-connector

/**
 * Parses table schema JSON into {@link TableSchema}.
 *
 * @param tableSchemaJson JSON table schema to convert to {@link TableSchema}
 * @return {@link TableSchema}
 * @throws IOException if the JSON is invalid.
 */
static TableSchema parseTableSchema(String tableSchemaJson) throws IOException {
 JsonParser parser = JacksonFactory.getDefaultInstance().createJsonParser(tableSchemaJson);
 return parser.parseAndClose(TableSchema.class);
}

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

public void testParseFeed() throws Exception {
 JsonParser parser = newFactory().createJsonParser(JSON_FEED);
 parser.nextToken();
 Feed feed = parser.parseAndClose(Feed.class);
 Iterator<Entry> iterator = feed.entries.iterator();
 assertEquals("foo", iterator.next().title);
 assertEquals("bar", iterator.next().title);
 assertFalse(iterator.hasNext());
}

代码示例来源: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.http-client/google-http-client-test

@SuppressWarnings("unchecked")
public void testParseEntryAsMap() throws Exception {
 JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
 parser.nextToken();
 HashMap<String, Object> map = parser.parseAndClose(HashMap.class);
 assertEquals("foo", map.remove("title"));
 assertTrue(map.isEmpty());
}

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

public void testParseEntry() throws Exception {
 Entry entry = newFactory().createJsonParser(JSON_ENTRY).parseAndClose(Entry.class);
 assertEquals("foo", entry.title);
}

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

public void testParse_empty() throws Exception {
 JsonParser parser = newFactory().createJsonParser(EMPTY);
 parser.nextToken();
 try {
  parser.parseAndClose(HashMap.class);
  fail("expected " + IllegalArgumentException.class);
 } catch (IllegalArgumentException e) {
  // expected
 }
}

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

private void handleTopicManagementHttpError(
  HttpResponseException e) throws FirebaseMessagingException {
 InstanceIdServiceErrorResponse response = new InstanceIdServiceErrorResponse();
 try {
  JsonParser parser = jsonFactory.createJsonParser(e.getContent());
  parser.parseAndClose(response);
 } catch (IOException ignored) {
  // ignored
 }
 // Infer error code from HTTP status
 String code = IID_ERROR_CODES.get(e.getStatusCode());
 if (code == null) {
  code = UNKNOWN_ERROR;
 }
 String msg = response.error;
 if (Strings.isNullOrEmpty(msg)) {
  msg = String.format("Unexpected HTTP response with status: %d; body: %s",
    e.getStatusCode(), e.getContent());
 }
 throw new FirebaseMessagingException(code, msg, e);
}

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

public void testParse_emptyGenericJson() throws Exception {
 JsonParser parser = newFactory().createJsonParser(EMPTY_OBJECT);
 parser.nextToken();
 GenericJson json = parser.parseAndClose(GenericJson.class);
 assertTrue(json.isEmpty());
}

代码示例来源: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: firebase/firebase-admin-java

private void handleSendHttpError(HttpResponseException e) throws FirebaseMessagingException {
 MessagingServiceErrorResponse response = new MessagingServiceErrorResponse();
 if (e.getContent() != null) {
  try {
   JsonParser parser = jsonFactory.createJsonParser(e.getContent());
   parser.parseAndClose(response);
  } catch (IOException ignored) {
   // ignored
  }
 }
 String code = FCM_ERROR_CODES.get(response.getErrorCode());
 if (code == null) {
  code = UNKNOWN_ERROR;
 }
 String msg = response.getErrorMessage();
 if (Strings.isNullOrEmpty(msg)) {
  msg = String.format("Unexpected HTTP response with status: %d; body: %s",
    e.getStatusCode(), e.getContent());
 }
 throw new FirebaseMessagingException(code, msg, e);
}

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

private static FirebaseOptions getOptionsFromEnvironment() throws IOException {
  String defaultConfig = System.getenv(FIREBASE_CONFIG_ENV_VAR);
  if (Strings.isNullOrEmpty(defaultConfig)) {
   return new FirebaseOptions.Builder()
    .setCredentials(GoogleCredentials.getApplicationDefault())
    .build();
  }

  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
  JsonParser parser;
  if (defaultConfig.startsWith("{")) {
   parser = jsonFactory.createJsonParser(defaultConfig);
  } else {
   FileReader reader;
   reader = new FileReader(defaultConfig);
   parser = jsonFactory.createJsonParser(reader);
  }
  parser.parseAndClose(builder);
  builder.setCredentials(GoogleCredentials.getApplicationDefault());
  return builder.build();
 }
}

代码示例来源:origin: com.google.cloud/google-cloud-bigquery

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

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

public void testParser_partialEntry() throws Exception {
 JsonFactory factory = newFactory();
 JsonParser parser;
 parser = factory.createJsonParser(JSON_ENTRY);
 parser.nextToken();
 parser.nextToken();
 // current token is now field_name
 Entry result = parser.parseAndClose(Entry.class);
 assertEquals(JSON_ENTRY, factory.toString(result));
 // check types and values
 assertEquals("foo", result.title);
}

代码示例来源: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);
}

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

@Override
 protected String execute() throws FirebaseMessagingException {
  ImmutableMap.Builder<String, Object> payload = ImmutableMap.<String, Object>builder()
    .put("message", message);
  if (dryRun) {
   payload.put("validate_only", true);
  }
  HttpResponse response = null;
  try {
   HttpRequest request = requestFactory.buildPostRequest(
     new GenericUrl(url), new JsonHttpContent(jsonFactory, payload.build()));
   request.getHeaders().set("X-GOOG-API-FORMAT-VERSION", "2");
   request.setParser(new JsonObjectParser(jsonFactory));
   request.setResponseInterceptor(interceptor);
   response = request.execute();
   MessagingServiceResponse parsed = new MessagingServiceResponse();
   jsonFactory.createJsonParser(response.getContent()).parseAndClose(parsed);
   return parsed.name;
  } catch (HttpResponseException e) {
   handleSendHttpError(e);
   return null;
  } catch (IOException e) {
   throw new FirebaseMessagingException(
     INTERNAL_ERROR, "Error while calling FCM backend service", e);
  } finally {
   disconnectQuietly(response);
  }
 }
};

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

response = request.execute();
InstanceIdServiceResponse parsed = new InstanceIdServiceResponse();
jsonFactory.createJsonParser(response.getContent()).parseAndClose(parsed);
checkState(parsed.results != null && !parsed.results.isEmpty(),
  "unexpected response from topic management service");

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

<T> void makeRequest(
  HttpRequest baseRequest,
  T parsedResponseInstance,
  String requestIdentifier,
  String requestIdentifierDescription) throws FirebaseProjectManagementException {
 HttpResponse response = null;
 try {
  baseRequest.getHeaders().set(CLIENT_VERSION_HEADER, clientVersion);
  baseRequest.setParser(new JsonObjectParser(jsonFactory));
  baseRequest.setResponseInterceptor(interceptor);
  response = baseRequest.execute();
  jsonFactory.createJsonParser(response.getContent(), Charsets.UTF_8)
    .parseAndClose(parsedResponseInstance);
 } catch (Exception e) {
  handleError(requestIdentifier, requestIdentifierDescription, e);
 } finally {
  disconnectQuietly(response);
 }
}

相关文章