Java Code Examples for com.google.api.client.json.JsonParser#parse()
The following examples show how to use
com.google.api.client.json.JsonParser#parse() .
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 |
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 2
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
public void testParser_heterogeneousSchema_withArrays() throws Exception { JsonFactory factory = newFactory(); JsonParser parser = factory.createJsonParser(DOG_WITH_FAMILY); Animal dog = parser.parse(DogWithFamily.class); assertEquals(DOG_WITH_FAMILY, factory.toString(dog)); assertEquals(DogWithFamily.class, dog.getClass()); assertEquals("Bob", dog.name); assertEquals("dogwithfamily", dog.type); assertEquals(4, dog.numberOfLegs); assertEquals(10, ((DogWithFamily) dog).tricksKnown); String[] nicknames = {"Fluffy", "Hey, you"}; assertTrue(Arrays.equals(nicknames, ((DogWithFamily) dog).nicknames)); Animal child = ((DogWithFamily) dog).children[0]; assertEquals("Fido", child.name); assertEquals(3, ((Dog) child).tricksKnown); Animal child2 = ((DogWithFamily) dog).children[1]; assertEquals("Mr. Icky", child2.name); assertEquals(68, ((Centipede) child2).numberOfLegs); }
Example 3
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
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 4
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
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 5
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
public void testParser_heterogeneousSchema_withObject() throws Exception { JsonFactory factory = newFactory(); JsonParser parser = factory.createJsonParser(HUMAN); Animal human = parser.parse(Animal.class); assertEquals(HUMAN, factory.toString(human)); Dog dog = ((Human) human).bestFriend; 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.tricksKnown); assertEquals("Joe", human.name); assertEquals(2, human.numberOfLegs); assertEquals("human", human.type); }
Example 6
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
public void testParser_polymorphicClass_mapOfPolymorphicClasses() throws Exception { JsonFactory factory = newFactory(); JsonParser parser = factory.createJsonParser(HUMAN_WITH_PETS); Animal human = parser.parse(Animal.class); assertEquals(HumanWithPets.class, human.getClass()); assertEquals(HUMAN_WITH_PETS_PARSED, factory.toString(human)); assertEquals(2, human.numberOfLegs); assertEquals("human with pets", human.type); HumanWithPets humanWithPets = (HumanWithPets) human; assertEquals("Fido", humanWithPets.bestFriend.name); assertEquals(3, humanWithPets.bestFriend.tricksKnown); assertEquals("Mr. Icky", humanWithPets.pets.get("first").name); assertEquals("bug", humanWithPets.pets.get("first").type); assertEquals(68, humanWithPets.pets.get("first").numberOfLegs); assertEquals("green", ((Centipede) humanWithPets.pets.get("first")).color); assertEquals("dog", humanWithPets.pets.get("second").type); assertEquals(0, ((Dog) humanWithPets.pets.get("second")).tricksKnown); assertEquals(2, humanWithPets.pets.size()); }
Example 7
Source File: MessageTest.java From firebase-admin-java with Apache License 2.0 | 5 votes |
private static Map<String, Object> toMap(Object object) throws IOException { JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); String json = jsonFactory.toString(object); JsonParser parser = jsonFactory.createJsonParser(json); Map<String, Object> map = new HashMap<>(); parser.parse(map); return map; }
Example 8
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 |
public void testParser_enumValue() throws Exception { // parse JsonFactory factory = newFactory(); JsonParser parser; parser = factory.createJsonParser(ENUM_VALUE); parser.nextToken(); EnumValue result = parser.parse(EnumValue.class); // serialize assertEquals(ENUM_VALUE, factory.toString(result)); // check parsed result assertEquals(E.VALUE, result.value); assertEquals(E.OTHER_VALUE, result.otherValue); assertEquals(E.NULL, result.nullValue); }
Example 10
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
@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 11
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testParser_heterogeneousSchema_withNullArrays() throws Exception { JsonFactory factory = newFactory(); JsonParser parser = factory.createJsonParser(DOG_WITH_NO_FAMILY); Animal dog = parser.parse(DogWithFamily.class); assertEquals(DogWithFamily.class, dog.getClass()); assertEquals(DOG_WITH_NO_FAMILY_PARSED, factory.toString(dog)); assertEquals(4, dog.numberOfLegs); assertEquals(0, ((Dog) dog).tricksKnown); assertEquals(null, dog.name); assertEquals(null, ((DogWithFamily) dog).nicknames); assertEquals(null, ((DogWithFamily) dog).children); }
Example 12
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 13
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 14
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
@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 15
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 16
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 17
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 18
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 4 votes |
public void testParser_floatMapTypeVariableType() throws Exception { // parse JsonFactory factory = newFactory(); JsonParser parser; parser = factory.createJsonParser(FLOAT_MAP_TYPE_VARIABLE_TYPE); parser.nextToken(); FloatMapTypeVariableType result = parser.parse(FloatMapTypeVariableType.class); // serialize assertEquals(FLOAT_MAP_TYPE_VARIABLE_TYPE, factory.toString(result)); // check parsed result // array Map<String, Float>[][] arr = result.arr; assertEquals(2, arr.length); assertEquals(Data.nullOf(Map[].class), arr[0]); Map<String, Float>[] subArr = arr[1]; assertEquals(2, subArr.length); assertEquals(Data.nullOf(HashMap.class), subArr[0]); Map<String, Float> arrValue = subArr[1]; assertEquals(1, arrValue.size()); Float fValue = arrValue.get("a"); assertEquals(1.0f, fValue); // collection LinkedList<LinkedList<Map<String, Float>>> list = result.list; assertEquals(2, list.size()); assertEquals(Data.nullOf(LinkedList.class), list.get(0)); LinkedList<Map<String, Float>> subList = list.get(1); assertEquals(2, subList.size()); assertEquals(Data.nullOf(HashMap.class), subList.get(0)); arrValue = subList.get(1); assertEquals(1, arrValue.size()); fValue = arrValue.get("a"); assertEquals(1.0f, fValue); // null value Map<String, Float> nullValue = result.nullValue; assertEquals(Data.nullOf(HashMap.class), nullValue); // value Map<String, Float> value = result.value; assertEquals(1, value.size()); fValue = value.get("a"); assertEquals(1.0f, fValue); }
Example 19
Source File: GoogleJsonErrorContainerTest.java From google-api-java-client with Apache License 2.0 | 4 votes |
public void test_json() throws Exception { JsonParser parser = FACTORY.createJsonParser(ERROR); GoogleJsonErrorContainer e = parser.parse(GoogleJsonErrorContainer.class); assertEquals(ERROR, FACTORY.toString(e)); }
Example 20
Source File: GoogleJsonErrorTest.java From google-api-java-client with Apache License 2.0 | 4 votes |
public void test_json() throws Exception { JsonParser parser = FACTORY.createJsonParser(ERROR); parser.nextToken(); GoogleJsonError e = parser.parse(GoogleJsonError.class); assertEquals(ERROR, FACTORY.toString(e)); }