Search in sources :

Example 1 with TypeUtils

use of com.alibaba.fastjson.util.TypeUtils in project fastjson by alibaba.

the class DefaultExtJSONParserTest method testCast.

public void testCast() throws Exception {
    new TypeUtils();
    DefaultJSONParser parser = new DefaultJSONParser("");
    Assert.assertNull(castToByte(null));
    Assert.assertNull(castToShort(null));
    Assert.assertNull(castToInt(null));
    Assert.assertNull(castToLong(null));
    Assert.assertNull(castToBigInteger(null));
    Assert.assertNull(castToBigDecimal(null));
    Assert.assertNull(castToFloat(null));
    Assert.assertNull(castToDouble(null));
    Assert.assertNull(castToBoolean(null));
    Assert.assertNull(castToDate(null));
    Assert.assertNull(castToString(null));
    Assert.assertEquals(12, castToByte("12").intValue());
    Assert.assertEquals(1234, castToShort("1234").intValue());
    Assert.assertEquals(1234, castToInt("1234").intValue());
    Assert.assertEquals(1234, castToLong("1234").intValue());
    Assert.assertEquals(1234, castToBigInteger("1234").intValue());
    Assert.assertEquals(1234, castToBigDecimal("1234").intValue());
    Assert.assertEquals(1234, castToFloat("1234").intValue());
    Assert.assertEquals(1234, castToDouble("1234").intValue());
    Assert.assertEquals(12, castToByte(12).intValue());
    Assert.assertEquals(1234, castToShort(1234).intValue());
    Assert.assertEquals(1234, castToInt(1234).intValue());
    Assert.assertEquals(1234, castToLong(1234).intValue());
    Assert.assertEquals(1234, castToBigInteger(1234).intValue());
    Assert.assertEquals(1234, castToBigDecimal(1234).intValue());
    Assert.assertEquals(1234, castToFloat(1234).intValue());
    Assert.assertEquals(1234, castToDouble(1234).intValue());
    Assert.assertEquals(Boolean.TRUE, castToBoolean(true));
    Assert.assertEquals(Boolean.FALSE, castToBoolean(false));
    Assert.assertEquals(Boolean.TRUE, castToBoolean(1));
    Assert.assertEquals(Boolean.FALSE, castToBoolean(0));
    Assert.assertEquals(Boolean.TRUE, castToBoolean("true"));
    Assert.assertEquals(Boolean.FALSE, castToBoolean("false"));
    long time = System.currentTimeMillis();
    Assert.assertEquals(time, castToDate(new Date(time)).getTime());
    Assert.assertEquals(time, castToDate(time).getTime());
    Assert.assertEquals(time, castToDate(Long.toString(time)).getTime());
    Assert.assertEquals("true", castToString("true"));
    Assert.assertEquals("true", castToString(true));
    Assert.assertEquals("123", castToString(123));
    Assert.assertEquals(new BigDecimal("2"), castToBigDecimal("2"));
    Assert.assertEquals(new BigDecimal("2"), castToBigDecimal(new BigInteger("2")));
}
Also used : BigInteger(java.math.BigInteger) TypeUtils.castToBigInteger(com.alibaba.fastjson.util.TypeUtils.castToBigInteger) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) TypeUtils(com.alibaba.fastjson.util.TypeUtils) Date(java.util.Date) TypeUtils.castToDate(com.alibaba.fastjson.util.TypeUtils.castToDate) TypeUtils.castToBigDecimal(com.alibaba.fastjson.util.TypeUtils.castToBigDecimal) BigDecimal(java.math.BigDecimal)

Aggregations

DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)1 TypeUtils (com.alibaba.fastjson.util.TypeUtils)1 TypeUtils.castToBigDecimal (com.alibaba.fastjson.util.TypeUtils.castToBigDecimal)1 TypeUtils.castToBigInteger (com.alibaba.fastjson.util.TypeUtils.castToBigInteger)1 TypeUtils.castToDate (com.alibaba.fastjson.util.TypeUtils.castToDate)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Date (java.util.Date)1