Menu

#125 How to apply custom converter when you can't annotate the bean?

open
nobody
None
5
2022-10-21
2020-04-28
Andrew M
No

I don't want my bean to have a dependency on OpenCSV or the custom converter that will populate it. Can I somehow apply custom converters on a per column basis? thinking something roughly like this:

class Person {
String name;
LocalDate birthday
}

Map<string, string=""> colsToFields = new HashMap<>();
colsToFields.put("User Name", "name");
colsToFields.put("User Birthday", "birthday");</string,>

Map<string, string=""> colsToConverter = new HashMap<>();
colsToConverter.put("User Name", NameConverter.class);
colsToConverter.put("User Birthday", MyDateConverter.class);</string,>

HeaderColumnNameTranslateMappingStrategy<person> strategy = new HeaderColumnNameTranslateMappingStrategy<person>();
strategy.setType(Person.class);
strategy.setColumnMapping(colsToFields);
strategy.setConverterMapping(colsToConverter);</person></person>

Is there already some way to do this? Thanks

Discussion

  • Andrew M

    Andrew M - 2020-04-28

    Sorry it lookslike some weird text was inserted in my code by the editor. You get the idea.

     
  • Andrew Rucker Jones

    There is currently no way to do that. I personally am not very interested in implementing it. Perhaps someone else is. However, for anyone wishing to submit a patch or pull request, I would expect a full API that would make it possible to do everything opencsv currently does without the use of annotations.

     
  • Andrew Rucker Jones

    I'm curious: would an external XML configuration file that tells opencsv how to bind to a bean be good for you?

     
  • Andrew M

    Andrew M - 2020-10-25

    I think it would be cool if users had the choice of defining the bindgings in XML or in a Java class. Personally, I like to keep everything in Java.

     
  • Elliot Korte

    Elliot Korte - 2020-12-22

    +1 would love to have this feature where you could avoid annotations and specify the same values on column mappers

     
  • Andrew Rucker Jones

    • assigned_to: Andrew Rucker Jones
     
  • Andrew Rucker Jones

    • assigned_to: Andrew Rucker Jones --> nobody
     
  • Andrew Rucker Jones

    I am no longer actively developing in the project.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.