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

The following examples show how to use com.google.api.client.json.JsonParser#nextToken() . 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 6 votes vote down vote up
public void testParser_mapType() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(MAP_TYPE);
  parser.nextToken();
  MapOfMapType result = parser.parse(MapOfMapType.class);
  // serialize
  assertEquals(MAP_TYPE, factory.toString(result));
  // check parsed result
  Map<String, Map<String, Integer>>[] value = result.value;
  Map<String, Map<String, Integer>> firstMap = value[0];
  Map<String, Integer> map1 = firstMap.get("map1");
  Integer integer = map1.get("k1");
  assertEquals(1, integer.intValue());
  Map<String, Integer> map2 = firstMap.get("map2");
  assertEquals(3, map2.get("kk1").intValue());
  assertEquals(4, map2.get("kk2").intValue());
}
 
Example 2
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void testParser_nullValue() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(NULL_VALUE);
  parser.nextToken();
  StringNullValue result = parser.parse(StringNullValue.class);
  // serialize
  assertEquals(NULL_VALUE, factory.toString(result));
  // check parsed result
  assertEquals(Data.NULL_STRING, result.value);
  String[] arr = result.arr;
  assertEquals(1, arr.length);
  assertEquals(Data.nullOf(String.class), arr[0]);
  String[][] arr2 = result.arr2;
  assertEquals(2, arr2.length);
  assertEquals(Data.nullOf(String[].class), arr2[0]);
  String[] subArr2 = arr2[1];
  assertEquals(1, subArr2.length);
  assertEquals(Data.NULL_STRING, subArr2[0]);
}
 
Example 3
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void testParser_hashmapForMapType() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(MAP_TYPE);
  parser.nextToken();
  @SuppressWarnings("unchecked")
  HashMap<String, ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>>> result =
      parser.parse(HashMap.class);
  // serialize
  assertEquals(MAP_TYPE, factory.toString(result));
  // check parsed result
  ArrayList<ArrayMap<String, ArrayMap<String, BigDecimal>>> value = result.get("value");
  ArrayMap<String, ArrayMap<String, BigDecimal>> firstMap = value.get(0);
  ArrayMap<String, BigDecimal> map1 = firstMap.get("map1");
  BigDecimal integer = map1.get("k1");
  assertEquals(1, integer.intValue());
}
 
Example 4
Source File: GooglePublicKeysManager.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Forces a refresh of the public certificates downloaded from {@link #getPublicCertsEncodedUrl}.
 *
 * <p>
 * This method is automatically called from {@link #getPublicKeys()} if the public keys have not
 * yet been initialized or if the expiration time is very close, so normally this doesn't need to
 * be called. Only call this method to explicitly force the public keys to be updated.
 * </p>
 */
public GooglePublicKeysManager refresh() throws GeneralSecurityException, IOException {
  lock.lock();
  try {
    publicKeys = new ArrayList<PublicKey>();
    // HTTP request to public endpoint
    CertificateFactory factory = SecurityUtils.getX509CertificateFactory();
    HttpResponse certsResponse = transport.createRequestFactory()
        .buildGetRequest(new GenericUrl(publicCertsEncodedUrl)).execute();
    expirationTimeMilliseconds =
        clock.currentTimeMillis() + getCacheTimeInSec(certsResponse.getHeaders()) * 1000;
    // parse each public key in the JSON response
    JsonParser parser = jsonFactory.createJsonParser(certsResponse.getContent());
    JsonToken currentToken = parser.getCurrentToken();
    // token is null at start, so get next token
    if (currentToken == null) {
      currentToken = parser.nextToken();
    }
    Preconditions.checkArgument(currentToken == JsonToken.START_OBJECT);
    try {
      while (parser.nextToken() != JsonToken.END_OBJECT) {
        parser.nextToken();
        String certValue = parser.getText();
        X509Certificate x509Cert = (X509Certificate) factory.generateCertificate(
            new ByteArrayInputStream(StringUtils.getBytesUtf8(certValue)));
        publicKeys.add(x509Cert.getPublicKey());
      }
      publicKeys = Collections.unmodifiableList(publicKeys);
    } finally {
      parser.close();
    }
    return this;
  } finally {
    lock.unlock();
  }
}
 
Example 5
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_anyType() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(ANY_TYPE);
  parser.nextToken();
  AnyType result = parser.parse(AnyType.class);
  assertEquals(ANY_TYPE, factory.toString(result));
}
 
Example 6
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testCurrentToken() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_FEED);
  assertNull(parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_ARRAY, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.FIELD_NAME, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.VALUE_STRING, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
  parser.nextToken();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
  parser.nextToken();
  assertNull(parser.getCurrentToken());
}
 
Example 7
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_typeVariablesPassAround() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(TYPE_VARS);
  parser.nextToken();
  TypeVariablesPassedAround result = parser.parse(TypeVariablesPassedAround.class);
  // serialize
  assertEquals(TYPE_VARS, factory.toString(result));
  // check parsed result
  LinkedList<String> f = result.y.z.f;
  assertEquals("abc", f.get(0));
}
 
Example 8
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 9
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 10
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 11
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_extendsGenericJson() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser;
  // number types
  parser = factory.createJsonParser(EXTENDS_JSON);
  parser.nextToken();
  ExtendsGenericJson result = parser.parse(ExtendsGenericJson.class);
  assertEquals(EXTENDS_JSON, factory.toString(result));
}
 
Example 12
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 13
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_integerTypeVariableType() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(INTEGER_TYPE_VARIABLE_TYPE);
  parser.nextToken();
  IntegerTypeVariableType result = parser.parse(IntegerTypeVariableType.class);
  // serialize
  assertEquals(INTEGER_TYPE_VARIABLE_TYPE, factory.toString(result));
  // check parsed result
  // array
  Integer[][] arr = result.arr;
  assertEquals(2, arr.length);
  assertEquals(Data.nullOf(Integer[].class), arr[0]);
  Integer[] subArr = arr[1];
  assertEquals(2, subArr.length);
  assertEquals(Data.NULL_INTEGER, subArr[0]);
  Integer arrValue = subArr[1];
  assertEquals(1, arrValue.intValue());
  // collection
  LinkedList<LinkedList<Integer>> list = result.list;
  assertEquals(2, list.size());
  assertEquals(Data.nullOf(LinkedList.class), list.get(0));
  LinkedList<Integer> subList = list.get(1);
  assertEquals(2, subList.size());
  assertEquals(Data.NULL_INTEGER, subList.get(0));
  arrValue = subList.get(1);
  assertEquals(1, arrValue.intValue());
  // null value
  Integer nullValue = result.nullValue;
  assertEquals(Data.NULL_INTEGER, nullValue);
  // value
  Integer value = result.value;
  assertEquals(1, value.intValue());
}
 
Example 14
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 15
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_intArray() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(INT_ARRAY);
  parser.nextToken();
  int[] result = parser.parse(int[].class);
  assertEquals(INT_ARRAY, factory.toString(result));
  // check types and values
  assertTrue(Arrays.equals(new int[] {1, 2, 3}, result));
}
 
Example 16
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 17
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 18
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testSkipChildren_object() throws Exception {
  JsonParser parser = newFactory().createJsonParser(JSON_ENTRY);
  parser.nextToken();
  parser.skipChildren();
  assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
}
 
Example 19
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testParse() throws Exception {
  JsonParser parser = newFactory().createJsonParser(CONTAINED_MAP);
  parser.nextToken();
  A a = parser.parse(A.class);
  assertEquals(ImmutableMap.of("title", "foo"), a.map);
}
 
Example 20
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());
}