Java Code Examples for com.google.api.client.json.JsonFactory#createJsonParser()

The following examples show how to use com.google.api.client.json.JsonFactory#createJsonParser() . 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_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 3
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 4
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void testParser_heterogeneousSchema_numericValueType() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(POLYMORPHIC_NUMERIC_TYPE_1);
  PolymorphicWithNumericValueType t1 = parser.parse(PolymorphicWithNumericValueType.class);
  assertEquals(NumericValueTypedSubclass1.class, t1.getClass());

  factory = newFactory();
  parser = factory.createJsonParser(POLYMORPHIC_NUMERIC_TYPE_2);
  PolymorphicWithNumericValueType t2 = parser.parse(PolymorphicWithNumericValueType.class);
  assertEquals(NumericValueTypedSubclass2.class, t2.getClass());

  factory = newFactory();
  parser = factory.createJsonParser(POLYMORPHIC_NUMERIC_UNSPECIFIED_TYPE);
  try {
    parser.parse(PolymorphicWithNumericValueType.class);
  } catch (IllegalArgumentException e) {
    return; // expected
  }
  fail("IllegalArgumentException expected on heterogeneous schema without type field specified");
}
 
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_partialEmptyArray() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(EMPTY_ARRAY);
  parser.nextToken();
  parser.nextToken();
  // token is now end_array
  String[] result = parser.parse(String[].class);
  assertEquals(EMPTY_ARRAY, factory.toString(result));
  // check types and values
  assertEquals(0, result.length);
}
 
Example 6
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_heterogeneousSchema_numericType() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(POLYMORPHIC_NUMERIC_TYPE_1);
  PolymorphicWithNumericType t1 = parser.parse(PolymorphicWithNumericType.class);
  assertEquals(NumericTypedSubclass1.class, t1.getClass());

  factory = newFactory();
  parser = factory.createJsonParser(POLYMORPHIC_NUMERIC_TYPE_2);
  PolymorphicWithNumericType t2 = parser.parse(PolymorphicWithNumericType.class);
  assertEquals(NumericTypedSubclass2.class, t2.getClass());
}
 
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_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 8
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_stringArray() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(STRING_ARRAY);
  parser.nextToken();
  String[] result = parser.parse(String[].class);
  assertEquals(STRING_ARRAY, factory.toString(result));
  // check types and values
  assertTrue(Arrays.equals(new String[] {"a", "b", "c"}, result));
}
 
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_polymorphicClass_duplicateTypeKeys() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(EMPTY_OBJECT);
  try {
    parser.parse(PolymorphicWithDuplicateTypeKeys.class);
  } catch (IllegalArgumentException e) {
    return; // expected
  }
  fail("Expected IllegalArgumentException on class with duplicate typeDef keys");
}
 
Example 10
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_polymorphicClass_selfReferencing() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(POLYMORPHIC_SELF_REFERENCING);
  PolymorphicSelfReferencing p = parser.parse(PolymorphicSelfReferencing.class);
  assertEquals(PolymorphicSelfReferencing.class, p.getClass());
  assertEquals(POLYMORPHIC_SELF_REFERENCING, factory.toString(p));
  assertEquals("self", p.type);
  assertEquals("blah", p.info);
}
 
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 testParser_treemapForTypeVariableType() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(INTEGER_TYPE_VARIABLE_TYPE);
  parser.nextToken();
  TreeMap<String, Object> result = parser.parse(TreeMap.class);
  // serialize
  assertEquals(INTEGER_TYPE_VARIABLE_TYPE, factory.toString(result));
  // check parsed result
  // array
  ArrayList<Object> arr = (ArrayList<Object>) result.get("arr");
  assertEquals(2, arr.size());
  assertEquals(Data.nullOf(Object.class), arr.get(0));
  ArrayList<BigDecimal> subArr = (ArrayList<BigDecimal>) arr.get(1);
  assertEquals(2, subArr.size());
  assertEquals(Data.nullOf(Object.class), subArr.get(0));
  BigDecimal arrValue = subArr.get(1);
  assertEquals(1, arrValue.intValue());
  // null value
  Object nullValue = result.get("nullValue");
  assertEquals(Data.nullOf(Object.class), nullValue);
  // value
  BigDecimal value = (BigDecimal) result.get("value");
  assertEquals(1, value.intValue());
}
 
Example 12
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_heterogeneousSchema_illegalValueType() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(POLYMORPHIC_NUMERIC_TYPE_1);
  try {
    parser.parse(PolymorphicWithIllegalValueType.class);
  } catch (IllegalArgumentException e) {
    return; // expected
  }
  fail("Expected IllegalArgumentException on class with illegal @JsonPolymorphicTypeMap type");
}
 
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_doubleListTypeVariableType() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(DOUBLE_LIST_TYPE_VARIABLE_TYPE);
  parser.nextToken();
  DoubleListTypeVariableType result = parser.parse(DoubleListTypeVariableType.class);
  // serialize
  assertEquals(DOUBLE_LIST_TYPE_VARIABLE_TYPE, factory.toString(result));
  // check parsed result
  // array
  List<Double>[][] arr = result.arr;
  assertEquals(2, arr.length);
  assertEquals(Data.nullOf(List[].class), arr[0]);
  List<Double>[] subArr = arr[1];
  assertEquals(2, subArr.length);
  assertEquals(Data.nullOf(ArrayList.class), subArr[0]);
  List<Double> arrValue = subArr[1];
  assertEquals(1, arrValue.size());
  Double dValue = arrValue.get(0);
  assertEquals(1.0, dValue);
  // collection
  LinkedList<LinkedList<List<Double>>> list = result.list;
  assertEquals(2, list.size());
  assertEquals(Data.nullOf(LinkedList.class), list.get(0));
  LinkedList<List<Double>> subList = list.get(1);
  assertEquals(2, subList.size());
  assertEquals(Data.nullOf(ArrayList.class), subList.get(0));
  arrValue = subList.get(1);
  assertEquals(ImmutableList.of(Double.valueOf(1)), arrValue);
  // null value
  List<Double> nullValue = result.nullValue;
  assertEquals(Data.nullOf(ArrayList.class), nullValue);
  // value
  List<Double> value = result.value;
  assertEquals(ImmutableList.of(Double.valueOf(1)), value);
}
 
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_polymorphicClass_tooManyAnnotations() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(MULTIPLE_ANNOTATIONS_JSON);
  try {
    parser.parse(PolymorphicWithMultipleAnnotations.class);
  } catch (IllegalArgumentException e) {
    return; // expected
  }
  fail(
      "Expected IllegalArgumentException on class with multiple @JsonPolymorphicTypeMap"
          + " annotations.");
}
 
Example 15
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testParser_wildCardType() throws Exception {
  // parse
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(WILDCARD_TYPE);
  parser.nextToken();
  WildCardTypes result = parser.parse(WildCardTypes.class);
  // serialize
  assertEquals(WILDCARD_TYPE, factory.toString(result));
  // check parsed result
  Collection<?>[] simple = result.simple;
  ArrayList<BigDecimal> wildcard = (ArrayList<BigDecimal>) simple[0];
  BigDecimal wildcardFirstValue = wildcard.get(0);
  assertEquals(1, wildcardFirstValue.intValue());
  Collection<? extends Integer>[] upper = result.upper;
  ArrayList<Integer> wildcardUpper = (ArrayList<Integer>) upper[0];
  Integer wildcardFirstValueUpper = wildcardUpper.get(0);
  assertEquals(1, wildcardFirstValueUpper.intValue());
  Collection<? super Integer>[] lower = result.lower;
  ArrayList<Integer> wildcardLower = (ArrayList<Integer>) lower[0];
  Integer wildcardFirstValueLower = wildcardLower.get(0);
  assertEquals(1, wildcardFirstValueLower.intValue());
  Map<String, BigDecimal> map = (Map<String, BigDecimal>) result.map;
  BigDecimal mapValue = map.get("v");
  assertEquals(1, mapValue.intValue());
  Map<String, Integer> mapUpper = (Map<String, Integer>) result.mapUpper;
  Integer mapUpperValue = mapUpper.get("v");
  assertEquals(1, mapUpperValue.intValue());
  Collection<? super TreeMap<String, ? extends Integer>> mapInWild = result.mapInWild;
  TreeMap<String, ? extends Integer> mapInWildFirst =
      (TreeMap<String, ? extends Integer>) mapInWild.toArray()[0];
  Integer mapInWildFirstValue = mapInWildFirst.get("v");
  assertEquals(1, mapInWildFirstValue.intValue());
}
 
Example 16
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testParser_heterogeneousSchema_genericJson() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(DOG_EXTRA_INFO);
  AnimalGenericJson dog = parser.parse(AnimalGenericJson.class);
  assertEquals(DOG_EXTRA_INFO_ORDERED, factory.toString(dog));
  assertEquals(DogGenericJson.class, dog.getClass());
  assertEquals("Fido", dog.name);
  assertEquals("dog", dog.type);
  assertEquals(4, dog.numberOfLegs);
  assertEquals(3, ((DogGenericJson) dog).tricksKnown);
  assertEquals("this is not being used!", dog.get("unusedInfo"));
  BigDecimal foo = ((BigDecimal) ((ArrayMap<String, Object>) dog.get("unused")).get("foo"));
  assertEquals(200, foo.intValue());
}
 
Example 17
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
private void testParser_heterogeneousSchemata_Helper(String dogJson, String centipedeJson)
    throws Exception {
  // Test for Dog
  JsonFactory factory = newFactory();
  JsonParser parser;
  parser = factory.createJsonParser(dogJson);
  Animal dog = parser.parse(Animal.class);
  // Always outputs keys in alphabetical order
  assertEquals(DOG, factory.toString(dog));
  assertEquals(Dog.class, dog.getClass());
  assertEquals("Fido", dog.name);
  assertEquals("dog", dog.type);
  assertEquals(4, dog.numberOfLegs);
  assertEquals(3, ((Dog) dog).tricksKnown);

  // Test for Centipede
  parser = factory.createJsonParser(centipedeJson);
  parser.nextToken();
  Animal centipede = parser.parse(Animal.class);
  // Always outputs keys in alphabetical order
  assertEquals(CENTIPEDE, factory.toString(centipede));
  assertEquals(Centipede.class, centipede.getClass());
  assertEquals("Mr. Icky", centipede.name);
  assertEquals("bug", centipede.type);
  assertEquals(68, centipede.numberOfLegs);
  assertEquals("green", ((Centipede) centipede).color);
}
 
Example 18
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 19
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParser_polymorphicClass_noMatchingTypeKey() throws Exception {
  JsonFactory factory = newFactory();
  JsonParser parser = factory.createJsonParser(POLYMORPHIC_WITH_UNKNOWN_KEY);
  try {
    parser.parse(Animal.class);
  } catch (IllegalArgumentException e) {
    return; // expected
  }
  fail("Expected IllegalArgumentException when provided with unknown typeDef key");
}
 
Example 20
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());
}