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

The following examples show how to use com.google.api.client.json.JsonParser#skipToKey() . 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: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipToKey_found() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipToKey("title");
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  assertEquals("foo", parser.getText());
}
 
Example 2
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipToKey_startWithFieldName() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.nextToken();
  parser.skipToKey("title");
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  assertEquals("foo", parser.getText());
}
 
Example 3
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipChildren_string() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipToKey("title");
  parser.skipChildren();
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  assertEquals("foo", parser.getText());
}
 
Example 4
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSkipChildren_array() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_FEED);
  parser.nextToken();
  parser.skipToKey("entries");
  parser.skipChildren();
  assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
}
 
Example 5
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 6
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_missingEmpty() throws Exception {
  JsonParser parser = newFactory().createJsonParser(EMPTY_OBJECT);
  parser.nextToken();
  parser.skipToKey("missing");
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example 7
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_missing() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipToKey("missing");
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example 8
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_firstKey() throws Exception {
  JsonParser parser = createParser(JSON_THREE_ELEMENTS);
  assertEquals("one", parser.skipToKey(ImmutableSet.of("one")));
  parser.skipToKey("num");
  assertEquals(1, parser.getIntValue());
}
 
Example 9
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_lastKey() throws Exception {
  JsonParser parser = createParser(JSON_THREE_ELEMENTS);
  assertEquals("three", parser.skipToKey(ImmutableSet.of("three")));
  parser.skipToKey("num");
  assertEquals(3, parser.getIntValue());
}
 
Example 10
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipToKey_multipleKeys() throws Exception {
  JsonParser parser = createParser(JSON_THREE_ELEMENTS);
  assertEquals("two", parser.skipToKey(ImmutableSet.of("foo", "three", "two")));
  parser.skipToKey("num");
  assertEquals(2, parser.getIntValue());
}