BeanSerializer { public static <T> T deserializeByType(Object src, Class<T> clazz) { if (src == null) { return (T) ClassUtils.getDefaultPrimitiveValue(clazz); } else if (src instanceof Boolean) { return (T) CompatibleTypeUtils.convert(src, clazz); } else if (src instanceof Number) { return (T) CompatibleTypeUtils.convert(src, clazz); } else if (src instanceof Map) { Map srcMap = (Map) src; if (clazz == Object.class) { if (srcMap.containsKey(JSON.CLASS_KEY)) { return (T) mapToObject(srcMap, Object.class); } else { return (T) mapToMap(srcMap, srcMap.getClass()); } } else { if (Map.class.isAssignableFrom(clazz)) { return (T) mapToMap(srcMap, (Class<? extends Map>) clazz); } else { return mapToObject(srcMap, clazz); } } } else if (src instanceof Collection) { Collection list = (Collection) src; if (clazz == Object.class) { return (T) collection2Collection(list, list.getClass(), Object.class); } else if (Collection.class.isAssignableFrom(clazz)) { return (T) collection2Collection(list, (Class<? extends Collection>) clazz, Object.class); } else if (clazz.isArray()) { if (clazz.getComponentType().isPrimitive()) { return (T) CompatibleTypeUtils.convert(list, clazz); } else { return (T) collectionToArray(list, clazz.getComponentType()); } } else { return (T) list; } } else if (src.getClass().isArray()) { Class componentType = src.getClass().getComponentType(); if (componentType.isPrimitive()) { if (Collection.class.isAssignableFrom(clazz)) { return (T) arrayToCollection(src, (Class<? extends Collection>) clazz, Object.class); } else { return (T) src; } } else { Object[] array = (Object[]) src; if (clazz == Object.class) { return (T) array2Array(array, array.getClass().getComponentType()); } else if (clazz.isArray()) { return (T) array2Array(array, clazz.getComponentType()); } else if (Collection.class.isAssignableFrom(clazz)) { return (T) arrayToCollection(src, (Class<? extends Collection>) clazz, Object.class); } else { return (T) src; } } } else if (clazz.isEnum()) { if (src instanceof String) { return (T) Enum.valueOf((Class<? extends Enum>) clazz, (String) src); } else { throw new RuntimeException("Enum field must set string!"); } } else if (Date.class.isAssignableFrom(clazz)) { if (src instanceof Long) { return (T) new Date((Long) src); } else if (src instanceof String) { try { return (T) DateUtils.strToDate((String) src); } catch (Exception e) { throw new RuntimeException("Date field must set string(yyyy-MM-dd HH:mm:ss)!"); } } else { throw new RuntimeException("Date field must set long or string(yyyy-MM-dd HH:mm:ss)!"); } } else if (src instanceof String) { return (T) CompatibleTypeUtils.convert(src, clazz); } else { return (T) src; } }  static Object serialize(Object bean); static Object serialize(Object bean, boolean addType); static T deserializeByType(Object src, Class<T> clazz); static Object deserialize(Object object);  }
@Test public void testDeserializeByType() { Assert.assertTrue(0 == BeanSerializer.deserializeByType(null, int.class)); Assert.assertTrue(0 == BeanSerializer.deserializeByType(null, long.class)); Assert.assertFalse(BeanSerializer.deserializeByType(null, boolean.class)); Assert.assertArrayEquals(new int[] { 123 }, BeanSerializer.deserializeByType(Arrays.asList(123), int[].class)); Assert.assertFalse(BeanSerializer.deserializeByType(Arrays.asList(123), String.class) instanceof String); Assert.assertTrue(CommonUtils.listEquals(Arrays.asList(123), BeanSerializer.deserializeByType(new int[] { 123 }, List.class))); Assert.assertTrue(CommonUtils.listEquals(Arrays.asList("xxx"), BeanSerializer.deserializeByType(new String[] { "xxx" }, List.class))); Assert.assertEquals(TestJsonBean.Status.START, BeanSerializer.deserializeByType("START", TestJsonBean.Status.class)); try { BeanSerializer.deserializeByType(new TestJsonBean(), TestJsonBean.Status.class); Assert.fail(); } catch (Exception e) { } Date now = new Date(); Assert.assertEquals(now, BeanSerializer.deserializeByType(now.getTime(), Date.class)); Assert.assertEquals(DateUtils.dateToStr(now), DateUtils.dateToStr( BeanSerializer.deserializeByType(DateUtils.dateToStr(now), Date.class))); try { BeanSerializer.deserializeByType("xxxx", Date.class); Assert.fail(); } catch (Exception e) { } try { BeanSerializer.deserializeByType(new TestJsonBean(), Date.class); Assert.fail(); } catch (Exception e) { } }