Thanks to visit codestin.com
Credit goes to github.com

Skip to content

jedcua/Parseux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parseux

Build Status Coverage Status Code Climate

(NOTE: This project is WIP, its API will change in the future)

Easily parse CSV/Excel files to DTOs

Usage

Suppose you have a csv file

value1,value2
value1,value2
value1,value2
...

Easily parse that to a DTO by annotating your DTO class with @JsonPropertyOrder, which defines the order of field values to be used for parsing

@JsonPropertyOrder({"field1", "field2"})
class MyDTO {
    private String field1;
    private String field2;
    
    // Getters and setters...
}

Instantiate CsvAsDTO<>() and retrieve parsed csv as DTO

List<MyDTO> dtos = new CsvAsDTO<>(
    csv,
    MyDTO.class
).asDTOs();

csv can be:

  • Stream<String> where each String is 1 row (i.e "value1,value2")
  • Iterator<String> where each String also 1 row
  • BufferedReader
  • InputStreamReader
  • ByteArrayStreamReader
  • byte[]

Parsing Excel

Parsing excel files to DTO is the same with CSV:

List<MyDTO> dtos = new CsvAsDTO<>(
    new ExcelIterator(
        workbook
    )
    MyDTO.class
).asDTOs()

ExcelIterator in an implementation of Iterator<String> that run through all sheets and rows, and iteration of concatenated cells per row as String.

workbook is an instance of XSSFWorkbook from Apache POI

Column Separator

Parsing is comma separated by default. However you can annotate your DTO class with @ColumnSeparator and provide the delimiter

// Tab separated
@ColumnSeparator(value = '\t')
@JsonPropertyOrder({"field1", "field2"})
class MyDTO { }

// Colon separated
@ColumnSeparator(value = ':')
@JsonPropertyOrder({"field1", "field2"})
class MyDTO2 { }

Installation (Maven)

Add the following on your pom.xml

<dependencies>
    <dependency>
        <groupId>com.dragonfruit</groupId>
        <artifactId>Parseux</artifactId>
        <version>0.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
        <version>2.9.0.pr3</version>
    </dependency>
    ...
</dependencies>

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

License

Apache Version 2

About

Parse CSV or Excel files to DTO

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages