本文整理了Java中com.google.api.client.json.JsonParser.parse()
方法的一些代码示例,展示了JsonParser.parse()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonParser.parse()
方法的具体详情如下:
包路径:com.google.api.client.json.JsonParser
类名称:JsonParser
方法名:parse
暂无
代码示例来源:origin: com.google.http-client/google-http-client-test
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);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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());
}
代码示例来源:origin: com.google.http-client/google-http-client-test
public final void testParse_array() throws Exception {
byte[] jsonData = Charsets.UTF_8.encode("[ 123, 456 ]").array();
JsonParser jp =
newFactory().createJsonParser(new ByteArrayInputStream(jsonData), Charsets.UTF_8);
Type myType = Integer[].class;
Integer[] array = (Integer[]) jp.parse(myType, true);
assertNotNull(array);
assertEquals((Integer) 123, array[0]);
assertEquals((Integer) 456, array[1]);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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");
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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");
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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.");
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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");
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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");
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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));
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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));
}
代码示例来源:origin: com.google.http-client/google-http-client-test
public void testParser_string() throws Exception {
JsonFactory factory = newFactory();
String result = factory.createJsonParser(STRING_TOP_VALUE).parse(String.class);
assertEquals(STRING_TOP_VALUE, factory.toString(result));
// check types and values
assertEquals("a", result);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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));
}
代码示例来源:origin: com.google.http-client/google-http-client-test
public void testParser_emptyArray() throws Exception {
JsonFactory factory = newFactory();
String[] result = factory.createJsonParser(EMPTY_ARRAY).parse(String[].class);
assertEquals(EMPTY_ARRAY, factory.toString(result));
// check types and values
assertEquals(0, result.length);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
public void testParser_num() throws Exception {
JsonFactory factory = newFactory();
int result = factory.createJsonParser(NUMBER_TOP_VALUE).parse(int.class);
assertEquals(NUMBER_TOP_VALUE, factory.toString(result));
// check types and values
assertEquals(1, result);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
public void testParser_bool() throws Exception {
JsonFactory factory = newFactory();
boolean result = factory.createJsonParser(BOOL_TOP_VALUE).parse(boolean.class);
assertEquals(BOOL_TOP_VALUE, factory.toString(result));
// check types and values
assertTrue(result);
}
代码示例来源:origin: com.google.http-client/google-http-client-test
public void testParser_null() throws Exception {
JsonFactory factory = newFactory();
String result = factory.createJsonParser(NULL_TOP_VALUE).parse(String.class);
assertEquals(NULL_TOP_VALUE, factory.toString(result));
// check types and values
assertTrue(Data.isNull(result));
}
代码示例来源:origin: com.google.http-client/google-http-client-test
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);
}
内容来源于网络,如有侵权,请联系作者删除!