com.google.api.client.json.JsonGenerator Java Examples

The following examples show how to use com.google.api.client.json.JsonGenerator. 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: UnauthorizedServlet.java    From HotswapAgentExamples with GNU General Public License v2.0 6 votes vote down vote up
protected void unauthorized(HttpServletRequest req, HttpServletResponse resp)
		throws IOException {

	String userAgent = req.getHeader("User-Agent");

	resp.setHeader("Content-Type", "text/javascript");
	resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

	JsonGenerator generator = jsonFactory.createJsonGenerator(resp.getWriter());
	generator.writeStartObject();
	generator.writeFieldName("error");
	generator.writeString("Login required.");
	generator.writeFieldName("q");
	generator.writeString(req.getQueryString());
	generator.writeFieldName("agent");
	generator.writeString(userAgent);
	generator.writeEndObject();
	generator.flush();
}
 
Example #2
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 #3
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();
  }
}
 
Example #4
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 #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: ConfigDataStoreFactory.java    From googleads-shopping-samples with Apache License 2.0 5 votes vote down vote up
private void writeToken() throws IOException {
  try (OutputStream outputStream = new FileOutputStream(tokenFile);
      OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream)) {
    JsonGenerator generator = new JacksonFactory().createJsonGenerator(outputWriter);
    generator.enablePrettyPrint();
    generator.serialize(token);
    generator.flush();
  }
}
 
Example #7
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 #8
Source File: JsonHttpContent.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void writeTo(OutputStream out) throws IOException {
  JsonGenerator generator = jsonFactory.createJsonGenerator(out, getCharset());
  if (wrapperKey != null) {
    generator.writeStartObject();
    generator.writeFieldName(wrapperKey);
  }
  generator.serialize(data);
  if (wrapperKey != null) {
    generator.writeEndObject();
  }
  generator.flush();
}
 
Example #9
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final void testGenerateFeed() throws Exception {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JsonGenerator generator = newFactory().createJsonGenerator(out, Charsets.UTF_8);
  Feed feed = new Feed();
  Entry entryFoo = new Entry();
  entryFoo.title = "foo";
  Entry entryBar = new Entry();
  entryBar.title = "bar";
  feed.entries = new ArrayList<Entry>();
  feed.entries.add(entryFoo);
  feed.entries.add(entryBar);
  generator.serialize(feed);
  generator.flush();
  assertEquals(JSON_FEED, StringUtils.newStringUtf8(out.toByteArray()));
}
 
Example #10
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final void testGenerateEntry() throws Exception {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JsonGenerator generator = newFactory().createJsonGenerator(out, Charsets.UTF_8);
  Entry entry = new Entry();
  entry.title = "foo";
  generator.serialize(entry);
  generator.flush();
  assertEquals(JSON_ENTRY, StringUtils.newStringUtf8(out.toByteArray()));
}
 
Example #11
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 #12
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 #13
Source File: GsonGeneratorTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
protected JsonGenerator newGenerator(Writer writer) throws IOException {
  return FACTORY.createJsonGenerator(writer);
}
 
Example #14
Source File: GsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(Writer writer) {
  return new GsonGenerator(this, new JsonWriter(writer));
}
 
Example #15
Source File: GsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) {
  return createJsonGenerator(new OutputStreamWriter(out, enc));
}
 
Example #16
Source File: MockJsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(Writer writer) throws IOException {
  return new MockJsonGenerator(this);
}
 
Example #17
Source File: MockJsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) throws IOException {
  return new MockJsonGenerator(this);
}
 
Example #18
Source File: JacksonGeneratorTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
protected JsonGenerator newGenerator(Writer writer) throws IOException {
  return FACTORY.createJsonGenerator(writer);
}
 
Example #19
Source File: JacksonGenerator.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
JacksonGenerator(JacksonFactory factory, com.fasterxml.jackson.core.JsonGenerator generator) {
  this.factory = factory;
  this.generator = generator;
}
 
Example #20
Source File: JacksonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(Writer writer) throws IOException {
  return new JacksonGenerator(this, factory.createJsonGenerator(writer));
}
 
Example #21
Source File: JacksonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) throws IOException {
  return new JacksonGenerator(
      this, factory.createJsonGenerator(out, com.fasterxml.jackson.core.JsonEncoding.UTF8));
}
 
Example #22
Source File: AndroidJsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(Writer writer) {
  return new AndroidJsonGenerator(this, new JsonWriter(writer));
}
 
Example #23
Source File: AndroidJsonFactory.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) {
  return createJsonGenerator(new OutputStreamWriter(out, enc));
}
 
Example #24
Source File: AbstractJsonGeneratorTest.java    From google-http-java-client with Apache License 2.0 votes vote down vote up
protected abstract JsonGenerator newGenerator(Writer writer) throws IOException;