Java Code Examples for com.google.api.client.json.JsonObjectParser#parseAndClose()
The following examples show how to use
com.google.api.client.json.JsonObjectParser#parseAndClose() .
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: GoogleCredential.java From google-api-java-client with Apache License 2.0 | 6 votes |
/** * {@link Beta} <br/> * Return a credential defined by a Json file. * * @param credentialStream the stream with the credential definition. * @param transport the transport for Http calls. * @param jsonFactory the factory for Json parsing and formatting. * @return the credential defined by the credentialStream. * @throws IOException if the credential cannot be created from the stream. */ @Beta public static GoogleCredential fromStream(InputStream credentialStream, HttpTransport transport, JsonFactory jsonFactory) throws IOException { Preconditions.checkNotNull(credentialStream); Preconditions.checkNotNull(transport); Preconditions.checkNotNull(jsonFactory); JsonObjectParser parser = new JsonObjectParser(jsonFactory); GenericJson fileContents = parser.parseAndClose( credentialStream, OAuth2Utils.UTF_8, GenericJson.class); String fileType = (String) fileContents.get("type"); if (fileType == null) { throw new IOException("Error reading credentials from stream, 'type' field not specified."); } if (USER_FILE_TYPE.equals(fileType)) { return fromStreamUser(fileContents, transport, jsonFactory); } if (SERVICE_ACCOUNT_FILE_TYPE.equals(fileType)) { return fromStreamServiceAccount(fileContents, transport, jsonFactory); } throw new IOException(String.format( "Error reading credentials from stream, 'type' value '%s' not recognized." + " Expecting '%s' or '%s'.", fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE)); }
Example 2
Source File: AbstractJsonParserTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testParse_basic() throws IOException { JsonObjectParser parser = new JsonObjectParser(newJsonFactory()); InputStream inputStream = new ByteArrayInputStream(TEST_JSON.getBytes(StandardCharsets.UTF_8)); GenericJson json = parser.parseAndClose(inputStream, StandardCharsets.UTF_8, GenericJson.class); assertTrue(json.get("strValue") instanceof String); assertEquals("bar", json.get("strValue")); assertTrue(json.get("intValue") instanceof BigDecimal); assertEquals(new BigDecimal(123), json.get("intValue")); assertTrue(json.get("boolValue") instanceof Boolean); assertEquals(Boolean.FALSE, json.get("boolValue")); }
Example 3
Source File: AbstractJsonParserTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testParse_bigDecimal() throws IOException { JsonObjectParser parser = new JsonObjectParser(newJsonFactory()); InputStream inputStream = new ByteArrayInputStream(TEST_JSON_BIG_DECIMAL.getBytes(StandardCharsets.UTF_8)); GenericJson json = parser.parseAndClose(inputStream, StandardCharsets.UTF_8, GenericJson.class); assertTrue(json.get("bigDecimalValue") instanceof BigDecimal); assertEquals(new BigDecimal("1559341956102"), json.get("bigDecimalValue")); }
Example 4
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testJsonObjectParser_inputStream() throws Exception { JsonFactory factory = newFactory(); JsonObjectParser parser = new JsonObjectParser(factory); Simple simple = parser.parseAndClose( new ByteArrayInputStream(StringUtils.getBytesUtf8(SIMPLE)), Charsets.UTF_8, Simple.class); assertEquals("b", simple.a); }
Example 5
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testJsonObjectParser_readerWrapped() throws Exception { JsonFactory factory = newFactory(); JsonObjectParser parser = new JsonObjectParser.Builder(factory).setWrapperKeys(Collections.singleton("d")).build(); Simple simple = parser.parseAndClose(new StringReader(SIMPLE_WRAPPED), Simple.class); assertEquals("b", simple.a); }
Example 6
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testJsonObjectParser_inputStreamWrapped() throws Exception { JsonFactory factory = newFactory(); JsonObjectParser parser = new JsonObjectParser.Builder(factory).setWrapperKeys(Collections.singleton("d")).build(); Simple simple = parser.parseAndClose( new ByteArrayInputStream(StringUtils.getBytesUtf8(SIMPLE_WRAPPED)), Charsets.UTF_8, Simple.class); assertEquals("b", simple.a); }
Example 7
Source File: DefaultCredentialProvider.java From google-api-java-client with Apache License 2.0 | 5 votes |
@Override protected TokenResponse executeRefreshToken() throws IOException { GenericUrl tokenUrl = new GenericUrl(getTokenServerEncodedUrl()); HttpRequest request = getTransport().createRequestFactory().buildGetRequest(tokenUrl); JsonObjectParser parser = new JsonObjectParser(getJsonFactory()); request.setParser(parser); request.getHeaders().set("Metadata-Flavor", "Google"); request.setThrowExceptionOnExecuteError(false); HttpResponse response = request.execute(); int statusCode = response.getStatusCode(); if (statusCode == HttpStatusCodes.STATUS_CODE_OK) { InputStream content = response.getContent(); if (content == null) { // Throw explicitly rather than allow a later null reference as default mock // transports return success codes with empty contents. throw new IOException("Empty content from metadata token server request."); } return parser.parseAndClose(content, response.getContentCharset(), TokenResponse.class); } if (statusCode == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { throw new IOException(String.format("Error code %s trying to get security access token from" + " Compute Engine metadata for the default service account. This may be because" + " the virtual machine instance does not have permission scopes specified.", statusCode)); } throw new IOException(String.format("Unexpected Error code %s trying to get security access" + " token from Compute Engine metadata for the default service account: %s", statusCode, response.parseAsString())); }
Example 8
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 4 votes |
public void testJsonObjectParser_reader() throws Exception { JsonFactory factory = newFactory(); JsonObjectParser parser = new JsonObjectParser(factory); Simple simple = parser.parseAndClose(new StringReader(SIMPLE), Simple.class); assertEquals("b", simple.a); }
Example 9
Source File: GoogleJsonError.java From google-api-java-client with Apache License 2.0 | 3 votes |
/** * Parses the given error HTTP response using the given JSON factory. * * @param jsonFactory JSON factory * @param response HTTP response * @return new instance of the Google JSON error information * @throws IllegalArgumentException if content type is not {@link Json#MEDIA_TYPE} or if expected * {@code "data"} or {@code "error"} key is not found */ public static GoogleJsonError parse(JsonFactory jsonFactory, HttpResponse response) throws IOException { JsonObjectParser jsonObjectParser = new JsonObjectParser.Builder(jsonFactory).setWrapperKeys( Collections.singleton("error")).build(); return jsonObjectParser.parseAndClose( response.getContent(), response.getContentCharset(), GoogleJsonError.class); }