Skip to content

Instantly share code, notes, and snippets.

@dmitric
Created September 29, 2011 07:00
Show Gist options
  • Save dmitric/1250145 to your computer and use it in GitHub Desktop.
Save dmitric/1250145 to your computer and use it in GitHub Desktop.
CSVBeanBuilder Example Usage
//The class we need
class Transaction {
String name
int itemCount
double cost
Date purchaseDate
}
//our conversion closures
def stringToDouble = {
token ->
return Double.parseDouble(token)
}
def stringToInt = {
token ->
return Integer.parseInt(token)
}
def stringToDate = {
token ->
return new Date(java.sql.Date.valueOf(token).time)
}
def mappings = [
[csvProperty: "item name", classProperty: "name"],
[csvProperty: "cost in $", classProperty: "cost", conversion: stringToDouble],
[csvProperty: "count", classProperty: "itemCount", conversion: stringToInt],
[csvProperty: "date", classProperty: "purchaseDate", conversion: stringToDate],
]
List<Transaction> list = CSVBeanBuilder.loadAs(Transaction.class, csvText, mappings)
//And now we have a list of our objects!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment