private < T > String serializeToCsv ( T obj ) { if ( obj == null ) { return "" ; } for ( Method method : obj . getClass ( ) . getMethods ( ) ) { if ( "java.util.List" . equals ( method . getReturnType ( ) . getName ( ) ) ) { try { @ SuppressWarnings ( "rawtypes" ) java . util . List itemList = ( java . util . List ) method . invoke ( obj ) ; Object entry = itemList . get ( 0 ) ; List < String > stringList = new ArrayList < String > ( ) ; char delimiter = ' ' ; String lineSep = "\n" ; CsvMapper mapper = new CsvMapper ( ) ; mapper . enable ( JsonGenerator . Feature . IGNORE_UNKNOWN ) ; CsvSchema schema = mapper . schemaFor ( entry . getClass ( ) ) ; for ( int i = 0 ; i < itemList . size ( ) ; i ++ ) { if ( i == 0 ) { schema = schema . withHeader ( ) ; } else { schema = schema . withoutHeader ( ) ; } String csv = mapper . writer ( schema . withColumnSeparator ( delimiter ) . withoutQuoteChar ( ) . withLineSeparator ( lineSep ) ) . writeValueAsString ( itemList . get ( i ) ) ; stringList . add ( csv ) ; } return StringUtil . join ( stringList . toArray ( new String [ 0 ] ) , "" ) ; } catch ( JsonProcessingException e ) { System . out . println ( e ) ; } catch ( IllegalAccessException e ) { System . out . println ( e ) ; } catch ( IllegalArgumentException e ) { System . out . println ( e ) ; } catch ( InvocationTargetException e ) { System . out . println ( e ) ; } } } return "" ; }
Encode the given request object in CSV format .