Java 类com.alibaba.fastjson.util.TypeUtils 实例源码

项目:uavstack    文件:LongCodec.java   
@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;
}
项目:boohee_v5.6    文件:LongCodec.java   
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;
}
项目:uavstack    文件:ClassDerializer.java   
@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);
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
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();
    }


}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
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();
    }


}
项目:uavstack    文件:BigIntegerCodec.java   
@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);
}
项目:uavstack    文件:IntegerCodec.java   
@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;
}
项目:GitHub    文件:ASMDeserializerFactory.java   
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
}
项目:boohee_v5.6    文件:JSONObject.java   
public byte[] getBytes(String key) {
    Object value = get(key);
    if (value == null) {
        return null;
    }
    return TypeUtils.castToBytes(value);
}
项目:GitHub    文件:JSONObject.java   
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());
}
项目:GitHub    文件:JSONReader.java   
public Integer readInteger() {
    Object object;
    if (context == null) {
        object = parser.parse();
    } else {
        readBefore();
        object = parser.parse();
        readAfter();
    }

    return TypeUtils.castToInt(object);
}
项目:GitHub    文件:JSONReader.java   
public Long readLong() {
    Object object;
    if (context == null) {
        object = parser.parse();
    } else {
        readBefore();
        object = parser.parse();
        readAfter();
    }

    return TypeUtils.castToLong(object);
}
项目:GitHub    文件:BigIntegerCodec.java   
@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);
}
项目:GitHub    文件:FieldSerializer.java   
public Object getPropertyValueDirect(Object object) throws InvocationTargetException, IllegalAccessException {
    Object fieldValue =  fieldInfo.get(object);
    if (persistenceXToMany && !TypeUtils.isHibernateInitialized(fieldValue)) {
        return null;
    }
    return fieldValue;
}
项目:GitHub    文件:TypeUtilsTest2.java   
public void test_error_1() throws Exception {
    Exception error = null;
    try {
        TypeUtils.castToChar(true);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:boohee_v5.6    文件:JSONObject.java   
public float getFloatValue(String key) {
    Object value = get(key);
    if (value == null) {
        return 0.0f;
    }
    return TypeUtils.castToFloat(value).floatValue();
}
项目:boohee_v5.6    文件:JSONArray.java   
public boolean getBooleanValue(int index) {
    Object value = get(index);
    if (value == null) {
        return false;
    }
    return TypeUtils.castToBoolean(value).booleanValue();
}
项目:uavstack    文件:JSONReader.java   
public Integer readInteger() {
    Object object;
    if (context == null) {
        object = parser.parse();
    } else {
        readBefore();
        object = parser.parse();
        readAfter();
    }

    return TypeUtils.castToInt(object);
}
项目:GitHub    文件:JSONArray.java   
/**
 * @since  1.2.23
 */
public <T> List<T> toJavaList(Class<T> clazz) {
    List<T> list = new ArrayList<T>(this.size());

    ParserConfig config = ParserConfig.getGlobalInstance();

    for (Object item : this) {
        T classItem = (T) TypeUtils.cast(item, clazz, config);
        list.add(classItem);
    }

    return list;
}
项目:uavstack    文件:CharacterCodec.java   
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    Object value = parser.parse();

    if (value == null) {
        return null;
    }

    return (T) TypeUtils.castToChar(value);
}
项目:boohee_v5.6    文件:JSONObject.java   
public long getLongValue(String key) {
    Object value = get(key);
    if (value == null) {
        return 0;
    }
    return TypeUtils.castToLong(value).longValue();
}
项目:uavstack    文件:JavaObjectDeserializer.java   
@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);
}
项目:boohee_v5.6    文件:JSONArray.java   
public byte getByteValue(int index) {
    Object value = get(index);
    if (value == null) {
        return (byte) 0;
    }
    return TypeUtils.castToByte(value).byteValue();
}
项目:GitHub    文件:TypeUtilsTest3.java   
public void test_error() throws Exception {
    Exception error = null;
    try {
        TypeUtils.castToEnum("\"A1\"", Type.class, null);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:TypeUtilsTest3.java   
public void test_error_1() throws Exception {
    Exception error = null;
    try {
        TypeUtils.castToEnum(Boolean.TRUE, Type.class, null);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:boohee_v5.6    文件:JSONObject.java   
public double getDoubleValue(String key) {
    Object value = get(key);
    if (value == null) {
        return 0.0d;
    }
    return TypeUtils.castToDouble(value).doubleValue();
}
项目:boohee_v5.6    文件:CharacterCodec.java   
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    Object value = parser.parse();
    if (value == null) {
        return null;
    }
    return TypeUtils.castToChar(value);
}
项目:GitHub    文件:TypeUtilsTest3.java   
public void test_error_3() throws Exception {
    Exception error = null;
    try {
        TypeUtils.cast("xxx", new TypeReference<Object[]>() {
        }.getType(), null);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_castToJavaBean_StackTraceElement() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("className", "java.lang.Object");
    map.put("methodName", "hashCode");
    StackTraceElement element = TypeUtils.castToJavaBean(map, StackTraceElement.class, null);
    Assert.assertEquals("java.lang.Object", element.getClassName());
    Assert.assertEquals("hashCode", element.getMethodName());
    Assert.assertEquals(null, element.getFileName());
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_castToJavaBean_StackTraceElement_1() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("className", "java.lang.Object");
    map.put("methodName", "hashCode");
    map.put("lineNumber", 12);
    StackTraceElement element = TypeUtils.castToJavaBean(map, StackTraceElement.class, null);
    Assert.assertEquals("java.lang.Object", element.getClassName());
    Assert.assertEquals("hashCode", element.getMethodName());
    Assert.assertEquals(null, element.getFileName());
    Assert.assertEquals(12, element.getLineNumber());
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_error() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("@type", "xxx");
    Exception error = null;
    try {
        TypeUtils.castToJavaBean(map, Object.class, null);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public static void addClassMapping(String className, Class<?> clazz) throws Exception {
    Field field = TypeUtils.class.getDeclaredField("mappings");
    field.setAccessible(true);
    field.get(null);

    ConcurrentMap<String, Class<?>> mappings = (ConcurrentMap<String, Class<?>>) field.get(null);

    if (className == null) {
        className = clazz.getName();
    }

    mappings.put(className, clazz);
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_bean() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 123);
    Entity vo = TypeUtils.castToJavaBean(map, Entity.class);
    Assert.assertEquals(123, vo.getId());

    Assert.assertEquals("{\"id\":123}", JSON.toJSONString(vo));
}
项目:GitHub    文件:MaterializedInterfaceTest2.java   
public void test_parse() throws Exception {
    String text = "{\"id\":123, \"name\":\"chris\"}";
    JSONObject object = JSON.parseObject(text);

    Bean bean = TypeUtils.cast(object, Bean.class, null);

    Assert.assertEquals(123, bean.getId());
    Assert.assertEquals("chris", bean.getName());

    String text2 = JSON.toJSONString(bean);
    System.out.println(text2);
}
项目:GitHub    文件:TypeUtilsTest2.java   
public void test_0() throws Exception {
    Assert.assertNull(TypeUtils.cast("", Entity.class, null));
    Assert.assertNull(TypeUtils.cast("", Type.class, null));
    Assert.assertNull(TypeUtils.cast("", Byte.class, null));
    Assert.assertNull(TypeUtils.cast("", Short.class, null));
    Assert.assertNull(TypeUtils.cast("", Integer.class, null));
    Assert.assertNull(TypeUtils.cast("", Long.class, null));
    Assert.assertNull(TypeUtils.cast("", Float.class, null));
    Assert.assertNull(TypeUtils.cast("", Double.class, null));
    Assert.assertNull(TypeUtils.cast("", Character.class, null));
    Assert.assertNull(TypeUtils.cast("", java.util.Date.class, null));
    Assert.assertNull(TypeUtils.cast("", java.sql.Date.class, null));
    Assert.assertNull(TypeUtils.cast("", java.sql.Timestamp.class, null));

    Assert.assertNull(TypeUtils.castToChar(""));
    Assert.assertNull(TypeUtils.castToChar(null));
    Assert.assertEquals('A', TypeUtils.castToChar('A').charValue());
    Assert.assertEquals('A', TypeUtils.castToChar("A").charValue());

    Assert.assertNull(TypeUtils.castToBigDecimal(""));
    Assert.assertNull(TypeUtils.castToBigInteger(""));
    Assert.assertNull(TypeUtils.castToBoolean(""));
    Assert.assertNull(TypeUtils.castToEnum("", Type.class, null));

    Assert.assertEquals(null, TypeUtils.cast("", new TypeReference<Pair<Object, Object>>() {

    }.getType(), null));
}
项目:boohee_v5.6    文件:JavaObjectDeserializer.java   
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;
}
项目:GitHub    文件:DenyTest16.java   
public void test_deny() throws Exception {
    JSONObject object = new JSONObject();
    object.put("@type", "com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase");

    Throwable error = null;
    try {
        TypeUtils.castToJavaBean(object, Object.class);
    } catch (Exception ex) {
        error = ex;
    }
    assertNotNull(error);
}
项目:GitHub    文件:TypeUtilsTest_loadClass.java   
public void test_loadClass() throws Exception {
    Assert.assertSame(Entity.class,
                      TypeUtils.loadClass("com.alibaba.json.bvt.parser.TypeUtilsTest_loadClass$Entity",
                                          Entity.class.getClassLoader()));

    Assert.assertSame(Entity.class,
                      TypeUtils.loadClass("com.alibaba.json.bvt.parser.TypeUtilsTest_loadClass$Entity", null));
}
项目:boohee_v5.6    文件:JSONArray.java   
public short getShortValue(int index) {
    Object value = get(index);
    if (value == null) {
        return (short) 0;
    }
    return TypeUtils.castToShort(value).shortValue();
}
项目:GitHub    文件:TypeUtilsTest2.java   
public void test_error_2() throws Exception {
    Exception error = null;
    try {
        Assert.assertEquals(null, TypeUtils.cast("a", new TypeReference<List<Object>>() {

        }.getType(), null));
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}