Java Code Examples for com.google.api.client.json.JsonParser#parseAndClose()

The following examples show how to use com.google.api.client.json.JsonParser#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: InstanceIdClientImplTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
private void checkTopicManagementRequest(
    HttpRequest request, TopicManagementResponse result) throws IOException {
  assertEquals(1, result.getSuccessCount());
  assertEquals(1, result.getFailureCount());
  assertEquals(1, result.getErrors().size());
  assertEquals(1, result.getErrors().get(0).getIndex());
  assertEquals("unknown-error", result.getErrors().get(0).getReason());

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  request.getContent().writeTo(out);
  Map<String, Object> parsed = new HashMap<>();
  JsonParser parser = Utils.getDefaultJsonFactory().createJsonParser(out.toString());
  parser.parseAndClose(parsed);
  assertEquals(2, parsed.size());
  assertEquals("/topics/test-topic", parsed.get("to"));
  assertEquals(ImmutableList.of("id1", "id2"), parsed.get("registration_tokens"));
}
 
Example 2
Source File: FirebaseMessagingClientImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private FirebaseMessagingException createExceptionFromResponse(HttpResponseException e) {
  MessagingServiceErrorResponse response = new MessagingServiceErrorResponse();
  if (e.getContent() != null) {
    try {
      JsonParser parser = jsonFactory.createJsonParser(e.getContent());
      parser.parseAndClose(response);
    } catch (IOException ignored) {
      // ignored
    }
  }

  return newException(response, e);
}
 
Example 3
Source File: InstanceIdClientImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private FirebaseMessagingException createExceptionFromResponse(HttpResponseException e) {
  InstanceIdServiceErrorResponse response = new InstanceIdServiceErrorResponse();
  if (e.getContent() != null) {
    try {
      JsonParser parser = jsonFactory.createJsonParser(e.getContent());
      parser.parseAndClose(response);
    } catch (IOException ignored) {
      // ignored
    }
  }
  return newException(response, e);
}
 
Example 4
Source File: FirebaseProjectManagementServiceImplTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private void checkRequestPayload(int index, Map<String, String> expected) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse(index).getRequest().getContent().writeTo(out);
  JsonParser parser = Utils.getDefaultJsonFactory().createJsonParser(out.toString());
  Map<String, String> parsed = new HashMap<>();
  parser.parseAndClose(parsed);
  assertEquals(expected, parsed);
}
 
Example 5
Source File: FirebaseMessagingClientImplTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private void checkRequest(
    HttpRequest request, Map<String, Object> expected) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  request.getContent().writeTo(out);
  JsonParser parser = Utils.getDefaultJsonFactory().createJsonParser(out.toString());
  Map<String, Object> parsed = new HashMap<>();
  parser.parseAndClose(parsed);
  assertEquals(expected, parsed);
}
 
Example 6
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
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
  }
}
 
Example 7
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 8
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 9
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 11
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 12
Source File: ReceiveMessageServlet.java    From cloud-pubsub-samples-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final void doPost(final HttpServletRequest req,
                         final HttpServletResponse resp)
        throws IOException {
    // Validating unique subscription token before processing the message
    String subscriptionToken = System.getProperty(
            Constants.BASE_PACKAGE + ".subscriptionUniqueToken");
    if (!subscriptionToken.equals(req.getParameter("token"))) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        resp.getWriter().close();
        return;
    }

    ServletInputStream inputStream = req.getInputStream();

    // Parse the JSON message to the POJO model class
    JsonParser parser = JacksonFactory.getDefaultInstance()
            .createJsonParser(inputStream);
    parser.skipToKey("message");
    PubsubMessage message = parser.parseAndClose(PubsubMessage.class);

    // Store the message in the datastore
    Entity messageToStore = new Entity("PubsubMessage");
    messageToStore.setProperty("message",
            new String(message.decodeData(), "UTF-8"));
    messageToStore.setProperty("receipt-time", System.currentTimeMillis());
    DatastoreService datastore =
            DatastoreServiceFactory.getDatastoreService();
    datastore.put(messageToStore);

    // Invalidate the cache
    MemcacheService memcacheService =
            MemcacheServiceFactory.getMemcacheService();
    memcacheService.delete(Constants.MESSAGE_CACHE_KEY);

    // Acknowledge the message by returning a success code
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.getWriter().close();
}
 
Example 13
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testParse_emptyGenericJson() throws Exception {
  JsonParser parser = newFactory().createJsonParser(EMPTY_OBJECT);
  parser.nextToken();
  GenericJson json = parser.parseAndClose(GenericJson.class);
  assertTrue(json.isEmpty());
}
 
Example 14
Source File: BigQueryTimePartitioning.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
static TimePartitioning getFromJson(String json) throws IOException {
  JsonParser parser = JacksonFactory.getDefaultInstance().createJsonParser(json);
  return parser.parseAndClose(TimePartitioning.class);
}
 
Example 15
Source File: BigQueryTableHelper.java    From hadoop-connectors with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}