public String objectToString(Object object) { if (object == null) { return null; } Class<?> objectClass = object.getClass(); if (objectClass.isEnum()) { return ((Enum<?>) object).name(); } else if (object instanceof java.sql.Date) { return object.toString(); } else if (object instanceof java.sql.Time) { return object.toString(); } else if (object instanceof java.sql.Timestamp) { return object.toString(); } else if (object instanceof Date) { return new SimpleDateFormat(datePattern).format((Date) object); } return object.toString(); }
public Object objectFromString(String value, Class<?> objectClass) throws ParseException { if (value == null) { return null; } else if (objectClass.isAssignableFrom(value.getClass())) { return value; } // re-use the infinitely useful ParameterValue class ParameterValue pv = new ParameterValue(value); if (objectClass.isEnum()) { return pv.toEnum((Class) objectClass); } else if (java.sql.Date.class.isAssignableFrom(objectClass)) { return pv.toSqlDate(); } else if (java.sql.Time.class.isAssignableFrom(objectClass)) { return pv.toSqlTime(); } else if (java.sql.Timestamp.class.isAssignableFrom(objectClass)) { return pv.toSqlTimestamp(); } else if (Date.class.isAssignableFrom(objectClass)) { return pv.toDate(datePattern); } return pv.to(objectClass); }
private Map<String, Field> getFieldMap(Class<?> classOfT) { Map<String, Field> map = new HashMap<>(); for (Field field : classOfT.getDeclaredFields()) { field.setAccessible(true); String name; if (caseSensitiveFieldNames) { name = field.getName(); } else { name = field.getName().toLowerCase(); } map.put(name, field); } return map; }
@Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { // Collections are NOT supported for deserialization from CSV throw new RuntimeException( "Collection types are not supported. Please specify an array[] type."); } throw new RuntimeException( String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } }