Created
September 29, 2011 07:00
-
-
Save dmitric/1250145 to your computer and use it in GitHub Desktop.
CSVBeanBuilder Example Usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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