本文整理汇总了Java中com.alibaba.fastjson.parser.DefaultJSONParser.parseArray方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultJSONParser.parseArray方法的具体用法?Java DefaultJSONParser.parseArray怎么用?Java DefaultJSONParser.parseArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.parser.DefaultJSONParser
的用法示例。
在下文中一共展示了DefaultJSONParser.parseArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deserialze
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
if (parser.lexer.token() == JSONToken.NULL) {
parser.lexer.nextToken(JSONToken.COMMA);
return null;
}
if (type == JSONArray.class) {
JSONArray array = new JSONArray();
parser.parseArray(array);
return (T) array;
}
Collection list = TypeUtils.createCollection(type);
Type itemType = TypeUtils.getCollectionItemType(type);
parser.parseArray(itemType, list, fieldName);
return (T) list;
}
示例2: parseArray
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public static final <T> List<T> parseArray(String text, Class<T> clazz) {
if (text == null) {
return null;
}
List<T> list;
DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
JSONLexer lexer = parser.getLexer();
if (lexer.token() == 8) {
lexer.nextToken();
list = null;
} else {
Collection list2 = new ArrayList();
parser.parseArray((Class) clazz, list2);
parser.handleResovleTask(list2);
}
parser.close();
return list;
}
示例3: test_scanInt
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_scanInt() throws Exception {
StringBuffer buf = new StringBuffer();
buf.append('[');
for (int i = 0; i < 1024; ++i) {
if (i != 0) {
buf.append(',');
}
buf.append("{\"id\":" + i + "}");
}
buf.append(']');
Reader reader = new StringReader(buf.toString());
JSONReaderScanner scanner = new JSONReaderScanner(reader);
DefaultJSONParser parser = new DefaultJSONParser(scanner);
List<VO> array = parser.parseArray(VO.class);
for (int i = 0; i < array.size(); ++i) {
Assert.assertEquals(i, array.get(i).getId());
}
}
示例4: deserialze
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
if (parser.getLexer().token() == JSONToken.NULL) {
parser.getLexer().nextToken(JSONToken.COMMA);
return null;
}
JSONArray array = new JSONArray();
parser.parseArray(array);
AtomicLongArray atomicArray = new AtomicLongArray(array.size());
for (int i = 0; i < array.size(); ++i) {
atomicArray.set(i, array.getLong(i));
}
return (T) atomicArray;
}
示例5: test_scanInt
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_scanInt() throws Exception {
StringBuffer buf = new StringBuffer();
buf.append('[');
for (int i = 0; i < 10; ++i) {
if (i != 0) {
buf.append(',');
}
//1000000000000
//
buf.append("{\"id\":\"" + i + "\"}");
}
buf.append(']');
Reader reader = new StringReader(buf.toString());
JSONReaderScanner scanner = new JSONReaderScanner(reader);
DefaultJSONParser parser = new DefaultJSONParser(scanner);
List<VO> array = parser.parseArray(VO.class);
for (int i = 0; i < array.size(); ++i) {
Assert.assertEquals(Integer.toString(i), array.get(i).getId());
}
}
示例6: deserialze
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
if (parser.getLexer().token() == JSONToken.NULL) {
parser.getLexer().nextToken(JSONToken.COMMA);
return null;
}
JSONArray array = new JSONArray();
parser.parseArray(array);
AtomicIntegerArray atomicArray = new AtomicIntegerArray(array.size());
for (int i = 0; i < array.size(); ++i) {
atomicArray.set(i, array.getInteger(i));
}
return (T) atomicArray;
}
示例7: test_error_var
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_error_var() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("[1,2,null }");
parser.config(Feature.AllowISO8601DateFormat, false);
Exception error = null;
try {
Object[] array = parser.parseArray(new Type[] { Integer[].class });
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
示例8: test_error_4
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_error_4() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("[1,null }");
parser.config(Feature.AllowISO8601DateFormat, false);
Exception error = null;
try {
parser.parseArray(new Type[] { String.class });
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
示例9: deserialze
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
if (!(type instanceof GenericArrayType)) {
return parser.parse(fieldName);
}
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType instanceof TypeVariable) {
componentType = ((TypeVariable) componentType).getBounds()[0];
}
Object list = new ArrayList();
parser.parseArray(componentType, (Collection) list);
if (!(componentType instanceof Class)) {
return list.toArray();
}
Class<?> componentClass = (Class) componentType;
if (componentClass == Boolean.TYPE) {
return TypeUtils.cast(list, boolean[].class, parser.getConfig());
}
if (componentClass == Short.TYPE) {
return TypeUtils.cast(list, short[].class, parser.getConfig());
}
if (componentClass == Integer.TYPE) {
return TypeUtils.cast(list, int[].class, parser.getConfig());
}
if (componentClass == Long.TYPE) {
return TypeUtils.cast(list, long[].class, parser.getConfig());
}
if (componentClass == Float.TYPE) {
return TypeUtils.cast(list, float[].class, parser.getConfig());
}
if (componentClass == Double.TYPE) {
return TypeUtils.cast(list, double[].class, parser.getConfig());
}
Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
list.toArray(array);
return array;
}
示例10: test_scanInt
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_scanInt() throws Exception {
StringBuffer buf = new StringBuffer();
buf.append('[');
for (int i = 0; i < 10; ++i) {
if (i != 0) {
buf.append(',');
}
//1000000000000
//
if (i % 2 == 0) {
buf.append("{\"id\":true}");
} else {
buf.append("{\"id\":false}");
}
}
buf.append(']');
Reader reader = new StringReader(buf.toString());
JSONReaderScanner scanner = new JSONReaderScanner(reader);
DefaultJSONParser parser = new DefaultJSONParser(scanner);
List<VO> array = parser.parseArray(VO.class);
for (int i = 0; i < array.size(); ++i) {
if (i % 2 == 0) {
Assert.assertEquals(true, array.get(i).getId());
} else {
Assert.assertEquals(false, array.get(i).getId());
}
}
}
示例11: test_error_3
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_error_3() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("[1,null }");
ArrayList list = new ArrayList();
parser.config(Feature.AllowISO8601DateFormat, false);
Exception error = null;
try {
parser.parseArray(String.class, list);
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
示例12: test_5
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_5() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("[1,2,3,null]");
Object[] array = parser.parseArray(new Type[] { Integer.class, BigDecimal.class, Long.class, String.class });
Assert.assertEquals(new Integer(1), array[0]);
Assert.assertEquals(new BigDecimal("2"), array[1]);
Assert.assertEquals(new Long(3), array[2]);
Assert.assertEquals(null, array[3]);
}
示例13: deserialze
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
componentType = componentVar.getBounds()[0];
}
List<Object> list = new ArrayList<Object>();
parser.parseArray(componentType, list);
Class<?> componentClass;
if (componentType instanceof Class) {
componentClass = (Class<?>) componentType;
if (componentClass == boolean.class) {
return (T) TypeUtils.cast(list, boolean[].class, parser.getConfig());
} else if (componentClass == short.class) {
return (T) TypeUtils.cast(list, short[].class, parser.getConfig());
} else if (componentClass == int.class) {
return (T) TypeUtils.cast(list, int[].class, parser.getConfig());
} else if (componentClass == long.class) {
return (T) TypeUtils.cast(list, long[].class, parser.getConfig());
} else if (componentClass == float.class) {
return (T) TypeUtils.cast(list, float[].class, parser.getConfig());
} else if (componentClass == double.class) {
return (T) TypeUtils.cast(list, double[].class, parser.getConfig());
}
Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
list.toArray(array);
return (T) array;
} else {
return (T) list.toArray();
}
}
return (T) parser.parse(fieldName);
}
示例14: test_error_5
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_error_5() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("[1,null }");
ArrayList list = new ArrayList();
parser.config(Feature.AllowISO8601DateFormat, false);
Exception error = null;
try {
parser.parseArray(String.class, list);
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
示例15: test_parseArray
import com.alibaba.fastjson.parser.DefaultJSONParser; //导入方法依赖的package包/类
public void test_parseArray() throws Exception {
DefaultJSONParser parser = new DefaultJSONParser("[1]");
parser.config(Feature.AllowArbitraryCommas, false);
List<String> list = new ArrayList<String>();
parser.parseArray(String.class, list);
Assert.assertEquals(1, list.size());
}