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")));
}
Aggregations