Java Code Examples for com.google.api.client.json.JsonGenerator#close()

The following examples show how to use com.google.api.client.json.JsonGenerator#close() . 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: DataflowApiUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the serialized size (in bytes) of the {@link GenericJson} object that will be
 * serialized and sent to the Google Cloud Dataflow service API.
 *
 * <p>Uses only constant memory.
 */
public static long computeSerializedSizeBytes(GenericJson object) throws IOException {
  JsonFactory factory = object.getFactory();
  if (factory == null) {
    factory = Transport.getJsonFactory();
  }

  CountingOutputStream stream = new CountingOutputStream(ByteStreams.nullOutputStream());
  JsonGenerator generator = null;
  try {
    generator = factory.createJsonGenerator(stream, StandardCharsets.UTF_8);
    generator.serialize(object);
    generator.close(); // also closes the stream.
  } finally {
    if (generator != null) {
      generator.close();
    }
  }
  return stream.getCount();
}
 
Example 2
Source File: AbstractJsonGeneratorTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSerialize_simpleMap() throws Exception {
  StringWriter writer = new StringWriter();
  JsonGenerator generator = newGenerator(writer);

  Map<String, String> map = new HashMap<String, String>();
  map.put("a", "b");

  generator.serialize(map);
  generator.close();
  assertEquals("{\"a\":\"b\"}", writer.toString());
}
 
Example 3
Source File: AbstractJsonGeneratorTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSerialize_iterableMap() throws Exception {
  StringWriter writer = new StringWriter();
  JsonGenerator generator = newGenerator(writer);

  Map<String, String> map = new IterableMap();
  map.put("a", "b");

  generator.serialize(map);
  generator.close();
  assertEquals("{\"a\":\"b\"}", writer.toString());
}
 
Example 4
Source File: RemoteGoogleDriveConnector.java    From cloudsync with GNU General Public License v2.0 5 votes vote down vote up
private void storeClientToken(final JsonFactory jsonFactory) throws IOException
{
	final StringWriter jsonTrWriter = new StringWriter();
	final JsonGenerator generator = jsonFactory.createJsonGenerator(jsonTrWriter);
	generator.serialize(clientToken);
	generator.flush();
	generator.close();

	FileUtils.writeStringToFile(clientTokenPath.toFile(), jsonTrWriter.toString(), charset);
}
 
Example 5
Source File: FileCredentialStore.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
private void save() throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  try {
    JsonGenerator generator = jsonFactory.createJsonGenerator(fos, Charsets.UTF_8);
    generator.serialize(credentials);
    generator.close();
  } finally {
    fos.close();
  }
}
 
Example 6
Source File: FileCredentialStoreTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
private File createTempFile() throws Exception {
  File result = File.createTempFile("credentials", null);
  result.deleteOnExit();
  JsonGenerator generator =
      JSON_FACTORY.createJsonGenerator(new FileOutputStream(result), Charsets.UTF_8);
  generator.serialize(new FilePersistedCredentials());
  generator.close();
  return result;
}
 
Example 7
Source File: FileCredentialStore.java    From android-oauth-client with Apache License 2.0 5 votes vote down vote up
private void save() throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  try {
    JsonGenerator generator = jsonFactory.createJsonGenerator(fos, Charsets.UTF_8);
    generator.serialize(credentials);
    generator.close();
  } finally {
    fos.close();
  }
}