本文整理汇总了Java中com.alibaba.fastjson.util.TypeUtils类的典型用法代码示例。如果您正苦于以下问题:Java TypeUtils类的具体用法?Java TypeUtils怎么用?Java TypeUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeUtils类属于com.alibaba.fastjson.util包,在下文中一共展示了TypeUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deserialze
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.getLexer();
Long longObject;
if (lexer.token() == JSONToken.LITERAL_INT) {
long longValue = lexer.longValue();
lexer.nextToken(JSONToken.COMMA);
longObject = Long.valueOf(longValue);
} else {
Object value = parser.parse();
if (value == null) {
return null;
}
longObject = TypeUtils.castToLong(value);
}
if (clazz == AtomicLong.class) {
return (T) new AtomicLong(longObject.longValue());
}
return (T) longObject;
}
示例2: deserialze
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
Long longObject;
JSONLexer lexer = parser.getLexer();
if (lexer.token() == 2) {
long longValue = lexer.longValue();
lexer.nextToken(16);
longObject = Long.valueOf(longValue);
} else {
Object value = parser.parse();
if (value == null) {
return null;
}
longObject = TypeUtils.castToLong(value);
}
if (clazz == AtomicLong.class) {
return new AtomicLong(longObject.longValue());
}
return longObject;
}
示例3: deserialze
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (lexer.token() != JSONToken.LITERAL_STRING) {
throw new JSONException("expect className");
}
String className = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
return (T) TypeUtils.loadClass(className);
}
示例4: test_bean_2
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public void test_bean_2() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
PO vo = TypeUtils.castToJavaBean(map, PO.class);
Assert.assertEquals(123, vo.id);
SerializeWriter out = new SerializeWriter();
try {
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", "ID")));
serializer.write(vo);
Assert.assertEquals("{\"ID\":123}", out.toString());
} finally {
out.close();
}
}
示例5: test_bean_3
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public void test_bean_3() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
PO vo = TypeUtils.castToJavaBean(map, PO.class);
Assert.assertEquals(123, vo.id);
SerializeWriter out = new SerializeWriter();
try {
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", (String) null)));
serializer.write(vo);
Assert.assertEquals("{}", out.toString());
} finally {
out.close();
}
}
示例6: deserialze
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.LITERAL_INT) {
String val = lexer.numberString();
lexer.nextToken(JSONToken.COMMA);
return (T) new BigInteger(val);
}
Object value = parser.parse();
if (value == null) {
return null;
}
return (T) TypeUtils.castToBigInteger(value);
}
示例7: deserialze
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.getLexer();
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
Integer intObj;
if (lexer.token() == JSONToken.LITERAL_INT) {
int val = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);
intObj = Integer.valueOf(val);
} else if (lexer.token() == JSONToken.LITERAL_FLOAT) {
BigDecimal decimalValue = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
intObj = Integer.valueOf(decimalValue.intValue());
} else {
Object value = parser.parse();
intObj = TypeUtils.castToInt(value);
}
if (clazz == AtomicInteger.class) {
return (T) new AtomicInteger(intObj.intValue());
}
return (T) intObj;
}
示例8: _newCollection
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
private void _newCollection(MethodVisitor mw, Class<?> fieldClass, int i, boolean set) {
if (fieldClass.isAssignableFrom(ArrayList.class) && !set) {
mw.visitTypeInsn(NEW, "java/util/ArrayList");
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
} else if (fieldClass.isAssignableFrom(LinkedList.class) && !set) {
mw.visitTypeInsn(NEW, type(LinkedList.class));
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, type(LinkedList.class), "<init>", "()V");
} else if (fieldClass.isAssignableFrom(HashSet.class)) {
mw.visitTypeInsn(NEW, type(HashSet.class));
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, type(HashSet.class), "<init>", "()V");
} else if (fieldClass.isAssignableFrom(TreeSet.class)) {
mw.visitTypeInsn(NEW, type(TreeSet.class));
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, type(TreeSet.class), "<init>", "()V");
} else if (fieldClass.isAssignableFrom(LinkedHashSet.class)) {
mw.visitTypeInsn(NEW, type(LinkedHashSet.class));
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, type(LinkedHashSet.class), "<init>", "()V");
} else if (set) {
mw.visitTypeInsn(NEW, type(HashSet.class));
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, type(HashSet.class), "<init>", "()V");
} else {
mw.visitVarInsn(ALOAD, 0);
mw.visitLdcInsn(i);
mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "getFieldType",
"(I)Ljava/lang/reflect/Type;");
mw.visitMethodInsn(INVOKESTATIC, type(TypeUtils.class), "createCollection",
"(Ljava/lang/reflect/Type;)Ljava/util/Collection;");
}
mw.visitTypeInsn(CHECKCAST, type(fieldClass)); // cast
}
示例9: getBytes
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public byte[] getBytes(String key) {
Object value = get(key);
if (value == null) {
return null;
}
return TypeUtils.castToBytes(value);
}
示例10: getObject
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public <T> T getObject(String key, TypeReference typeReference) {
Object obj = map.get(key);
if (typeReference == null) {
return (T) obj;
}
return TypeUtils.cast(obj, typeReference.getType(), ParserConfig.getGlobalInstance());
}
示例11: readInteger
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public Integer readInteger() {
Object object;
if (context == null) {
object = parser.parse();
} else {
readBefore();
object = parser.parse();
readAfter();
}
return TypeUtils.castToInt(object);
}
示例12: readLong
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public Long readLong() {
Object object;
if (context == null) {
object = parser.parse();
} else {
readBefore();
object = parser.parse();
readAfter();
}
return TypeUtils.castToLong(object);
}
示例13: deserialze
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_INT) {
String val = lexer.numberString();
lexer.nextToken(JSONToken.COMMA);
return (T) new BigInteger(val);
}
Object value = parser.parse();
return value == null //
? null //
: (T) TypeUtils.castToBigInteger(value);
}
示例14: getPropertyValueDirect
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public Object getPropertyValueDirect(Object object) throws InvocationTargetException, IllegalAccessException {
Object fieldValue = fieldInfo.get(object);
if (persistenceXToMany && !TypeUtils.isHibernateInitialized(fieldValue)) {
return null;
}
return fieldValue;
}
示例15: test_error_1
import com.alibaba.fastjson.util.TypeUtils; //导入依赖的package包/类
public void test_error_1() throws Exception {
Exception error = null;
try {
TypeUtils.castToChar(true);
} catch (JSONException e) {
error = e;
}
Assert.assertNotNull(error);
}