Coder Social home page Coder Social logo

csv4j's Introduction

csv4j

Csv4j is a dynamic tool for mapping csv data into objects of a predefined domain type.

The first (header) line of the CSV input is assumed to contain CSV fields.

Optionally custom annotations are used to match a java field into one or more CSV header fields.

In the absence of annotations, csv4j matches java fields to CSV fields of the same name.

Then, csv4j makes extensive use of reflection in order to set the proper values to proper fields.

For example, a domain type could look like this:

public class DomainType {
 
  // no annotation, so it will be matched with "field0" csv field
  private int field0;
 
  // match att1 with both "field1" and "field3" csv fields
  @CsvFields({ "field1", "field3" })
  private String att1;
 
  // match att2 with "field2"
  @CsvFields({ "field2" })
  private double att2;
 
  public DomainType() {
  }
 
  public void setField0(int field0) {
      this.field0 = field0;
  }
 
  public void setAtt1(String att1) {
      this.att1 = att1;
  }
 
  public void setAtt2(double att2) {
      this.att2 = att2;
  }
    
  // any other method of your wish here
  ...
}

Then mapping a CSV file to the corresponding list of DomainType instances would be as easy as:

  Path p = // path to csv file
  Hydrator<DomainType> hydrator = Hydrator.of(DomainType.class);
  List<DomainType> objects = hydrator.fromCSV(p);

where a CSV input file could be

field0,field1,field2
0,csv,3.14
1,4,2.71
2,j,1.61
3,is awesome,1.41

or

field0,field3,field2
0,csv,3.14
1,4,2.71
2,j,1.61
3,is awesome,1.41

For more details, read Csv4j - Deserialize CSV Files into Java Objects

Build and Dependencies

Csv4j is written in java 8 and depends on Guava v18.0. For testing it uses TestNG.

License

Cs4j is licensed under the Apache License, Version 2.0

csv4j's People

Contributors

ytheohar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.