当前位置: 首页>>代码示例>>Java>>正文


Java TypeUtils类代码示例

本文整理汇总了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;
}
 
开发者ID:uavorg,项目名称:uavstack,代码行数:27,代码来源:LongCodec.java

示例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;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:20,代码来源:LongCodec.java

示例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);
}
 
开发者ID:uavorg,项目名称:uavstack,代码行数:18,代码来源:ClassDerializer.java

示例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();
    }

    
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:23,代码来源:TypeUtilsTest_castToJavaBean.java

示例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();
    }

    
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:23,代码来源:TypeUtilsTest_castToJavaBean.java

示例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);
}
 
开发者ID:uavorg,项目名称:uavstack,代码行数:18,代码来源:BigIntegerCodec.java

示例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;
}
 
开发者ID:uavorg,项目名称:uavstack,代码行数:31,代码来源:IntegerCodec.java

示例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
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:36,代码来源:ASMDeserializerFactory.java

示例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);
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:8,代码来源:JSONObject.java

示例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());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:JSONObject.java

示例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);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:13,代码来源:JSONReader.java

示例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);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:13,代码来源:JSONReader.java

示例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);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:15,代码来源:BigIntegerCodec.java

示例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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:FieldSerializer.java

示例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);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:10,代码来源:TypeUtilsTest2.java


注:本文中的com.alibaba.fastjson.util.TypeUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。