Last active
April 30, 2024 11:36
-
-
Save asafary/9675488 to your computer and use it in GitHub Desktop.
Example of how to read a Pojo from a CSV file using Jackson JSON Processor
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
| apply plugin: 'java' | |
| apply plugin: 'eclipse' | |
| repositories { | |
| mavenCentral() | |
| } | |
| dependencies { | |
| compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.3.0' | |
| testCompile 'junit:junit:4.11' | |
| } |
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
| Tom | Tommy | 32 | m | |
|---|---|---|---|---|
| Anna | Anny | 27 | f |
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
| package org.asafary.csv; | |
| import com.fasterxml.jackson.annotation.JsonPropertyOrder; | |
| @JsonPropertyOrder({ "name", "surname", "shoesize", "gender" }) | |
| public class Person { | |
| public String name; | |
| public String surname; | |
| public int shoesize; | |
| public String gender; | |
| } |
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
| package org.asafary.csv; | |
| import java.io.File; | |
| import java.util.List; | |
| import com.fasterxml.jackson.databind.MappingIterator; | |
| import com.fasterxml.jackson.dataformat.csv.CsvMapper; | |
| /** | |
| * Reads Person objects from a CSV file | |
| */ | |
| public class PersonReader { | |
| public static List<Person> readFile(File csvFile) throws Exception { | |
| MappingIterator<Person> personIter = new CsvMapper().readerWithTypedSchemaFor(Person.class).readValues(csvFile); | |
| return personIter.readAll(); | |
| } | |
| } |
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
| package org.asafary.csv; | |
| import static org.junit.Assert.*; | |
| import java.io.File; | |
| import java.util.List; | |
| import org.junit.Test; | |
| public class PersonReaderTest { | |
| @Test | |
| public void testReadingPersonObjectsFromCsvData() throws Exception { | |
| File testFile = new File("people.csv"); | |
| List<Person> people = PersonReader.readFile(testFile); | |
| assertEquals(2, people.size()); | |
| Person person1 = people.get(0); | |
| assertEquals("Tom", person1.getName()); | |
| assertEquals("Tommy", person1.getSurname()); | |
| assertEquals(32, person1.getShoesize()); | |
| assertEquals("m", person1.getGender()); | |
| Person person2 = people.get(1); | |
| assertEquals("Anna", person2.getName()); | |
| assertEquals("Anny", person2.getSurname()); | |
| assertEquals(27, person2.getShoesize()); | |
| assertEquals("f", person2.getGender()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parsing csv file and mapping it with Java classes is sometimes a very hectic work. There are many csv parser libraries available which makes our lives easy but still they can create complexity in implementing them in our existing codebase. To resolve all these issues and make the parsing process more simpler we can use a new csv parsing library called csv4pojo. csv4pojo makes its implementation in our existing code simpler and its a very lightweight Java library and processes the parsing very fast. It is capable of parsing millions of rows of data and can write same amount data in the csv file. It supports 15 different datatypes. Integer,String,Float,Double,Boolean,Long,Character,Class,Integer[],String[],Float[],Double[],Boolean[],Character[]Long[].
org.csv4pojoparser csv4pojo 2.0.0 Add this maven dependencies in your pom.xml and read the docs - https://github.com/Kazitanvirazad/CSV4POJO/blob/master/README.md