Java Code Examples for com.google.api.client.json.JsonGenerator#serialize()
The following examples show how to use
com.google.api.client.json.JsonGenerator#serialize() .
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 |
/** * 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: JsonHttpContent.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 3
Source File: AbstractJsonGeneratorTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 4
Source File: AbstractJsonGeneratorTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 5
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 6
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 7
Source File: RemoteGoogleDriveConnector.java From cloudsync with GNU General Public License v2.0 | 5 votes |
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: ConfigDataStoreFactory.java From googleads-shopping-samples with Apache License 2.0 | 5 votes |
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 9
Source File: FileCredentialStore.java From google-oauth-java-client with Apache License 2.0 | 5 votes |
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 10
Source File: FileCredentialStoreTest.java From google-oauth-java-client with Apache License 2.0 | 5 votes |
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 11
Source File: FileCredentialStore.java From android-oauth-client with Apache License 2.0 | 5 votes |
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(); } }