com.google.api.client.json.JsonParser Java Examples
The following examples show how to use
com.google.api.client.json.JsonParser.
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: InstanceIdClientImpl.java From firebase-admin-java with Apache License 2.0 | 6 votes |
private TopicManagementResponse sendInstanceIdRequest( String topic, List<String> registrationTokens, String path) throws IOException { String url = String.format("%s/%s", IID_HOST, path); Map<String, Object> payload = ImmutableMap.of( "to", getPrefixedTopic(topic), "registration_tokens", registrationTokens ); HttpResponse response = null; try { HttpRequest request = requestFactory.buildPostRequest( new GenericUrl(url), new JsonHttpContent(jsonFactory, payload)); request.getHeaders().set("access_token_auth", "true"); request.setParser(new JsonObjectParser(jsonFactory)); request.setResponseInterceptor(responseInterceptor); response = request.execute(); JsonParser parser = jsonFactory.createJsonParser(response.getContent()); InstanceIdServiceResponse parsedResponse = new InstanceIdServiceResponse(); parser.parse(parsedResponse); return new TopicManagementResponse(parsedResponse.results); } finally { ApiClientUtils.disconnectQuietly(response); } }
Example #2
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 #3
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
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: GsonParser.java From google-http-java-client with Apache License 2.0 | 6 votes |
@Override public JsonParser skipChildren() throws IOException { if (currentToken != null) { switch (currentToken) { case START_ARRAY: reader.skipValue(); currentText = "]"; currentToken = JsonToken.END_ARRAY; break; case START_OBJECT: reader.skipValue(); currentText = "}"; currentToken = JsonToken.END_OBJECT; break; default: break; } } return this; }
Example #5
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 #6
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 6 votes |
public void testParser_arrayType() throws Exception { JsonFactory factory = newFactory(); JsonParser parser; parser = factory.createJsonParser(ARRAY_TYPE); parser.nextToken(); ArrayType result = parser.parse(ArrayType.class); assertEquals(ARRAY_TYPE, factory.toString(result)); // check types and values int[] arr = result.arr; assertTrue(Arrays.equals(new int[] {4, 5}, arr)); int[][] arr2 = result.arr2; assertEquals(2, arr2.length); int[] arr20 = arr2[0]; assertEquals(2, arr20.length); int arr200 = arr20[0]; assertEquals(1, arr200); assertEquals(2, arr20[1]); int[] arr21 = arr2[1]; assertEquals(1, arr21.length); assertEquals(3, arr21[0]); Integer[] integerArr = result.integerArr; assertEquals(2, integerArr.length); assertEquals(6, integerArr[0].intValue()); }
Example #7
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 #8
Source File: AndroidJsonParser.java From google-http-java-client with Apache License 2.0 | 6 votes |
@Override public JsonParser skipChildren() throws IOException { if (currentToken != null) { switch (currentToken) { case START_ARRAY: reader.skipValue(); currentText = "]"; currentToken = JsonToken.END_ARRAY; break; case START_OBJECT: reader.skipValue(); currentText = "}"; currentToken = JsonToken.END_OBJECT; break; default: break; } } return this; }
Example #9
Source File: InstanceIdClientImplTest.java From firebase-admin-java with Apache License 2.0 | 6 votes |
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 #10
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 #11
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 #12
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 #13
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #14
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public final void testParse_class() throws Exception { byte[] jsonData = Charsets.UTF_8.encode("{ \"foo\": 123 }").array(); JsonParser jp = newFactory().createJsonParser(new ByteArrayInputStream(jsonData), Charsets.UTF_8); Type myType = TestClass.class; TestClass instance = (TestClass) jp.parse(myType, true); assertNotNull(instance); assertEquals(123, instance.foo); }
Example #15
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public final void testCreateJsonParser_nullCharset() throws Exception { byte[] jsonData = Charsets.UTF_8.encode("{ \"foo\": 123 }").array(); JsonParser jp = newFactory().createJsonParser(new ByteArrayInputStream(jsonData), null); Type myType = TestClass.class; TestClass instance = (TestClass) jp.parse(myType, true); assertNotNull(instance); assertEquals(123, instance.foo); }
Example #16
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #17
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 #18
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testParser_heterogeneousSchema_missingType() throws Exception { JsonFactory factory = newFactory(); JsonParser parser; parser = factory.createJsonParser(ANIMAL_WITHOUT_TYPE); try { parser.parse(Animal.class); } catch (IllegalArgumentException e) { return; // expected } fail("IllegalArgumentException expected on heterogeneous schema without type field specified"); }
Example #19
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 #20
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public final void testGson() throws Exception { byte[] asciiJson = Charsets.UTF_8.encode("{ \"foo\": 123 }").array(); JsonParser jp = newFactory().createJsonParser(new ByteArrayInputStream(asciiJson), Charsets.UTF_8); assertEquals(com.google.api.client.json.JsonToken.START_OBJECT, jp.nextToken()); assertEquals(com.google.api.client.json.JsonToken.FIELD_NAME, jp.nextToken()); assertEquals(com.google.api.client.json.JsonToken.VALUE_NUMBER_INT, jp.nextToken()); assertEquals(123, jp.getIntValue()); assertEquals(com.google.api.client.json.JsonToken.END_OBJECT, jp.nextToken()); }
Example #21
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #22
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
@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 #23
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #24
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void testParser_collectionType() throws Exception { JsonFactory factory = newFactory(); JsonParser parser; parser = factory.createJsonParser(COLLECTION_TYPE); parser.nextToken(); CollectionOfCollectionType result = parser.parse(CollectionOfCollectionType.class); assertEquals(COLLECTION_TYPE, factory.toString(result)); // check that it is actually a linked list LinkedList<LinkedList<String>> arr = result.arr; LinkedList<String> linkedlist = arr.get(0); assertEquals("a", linkedlist.get(0)); }
Example #25
Source File: FirebaseMessagingClientImpl.java From firebase-admin-java with Apache License 2.0 | 5 votes |
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 #26
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 #27
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #28
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #29
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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 #30
Source File: AbstractJsonFactoryTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
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()); }