com.github.lahahana:csv

annotation based csv serialize/deserialization tool

License

License

Categories

Categories

CSV Data Data Formats
GroupId

GroupId

com.github.lahahana
ArtifactId

ArtifactId

csv
Last Version

Last Version

2.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

annotation based csv serialize/deserialization tool
Source Code Management

Source Code Management

https://github.com/lahahana/csv

Download csv

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.lahahana/csv/ -->
<dependency>
    <groupId>com.github.lahahana</groupId>
    <artifactId>csv</artifactId>
    <version>2.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lahahana/csv/
implementation 'com.github.lahahana:csv:2.0.1'
// https://jarcasting.com/artifacts/com.github.lahahana/csv/
implementation ("com.github.lahahana:csv:2.0.1")
'com.github.lahahana:csv:jar:2.0.1'
<dependency org="com.github.lahahana" name="csv" rev="2.0.1">
  <artifact name="csv" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lahahana', module='csv', version='2.0.1')
)
libraryDependencies += "com.github.lahahana" % "csv" % "2.0.1"
[com.github.lahahana/csv "2.0.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

csv

Java Annotation based CSV serialization/deserialization tool.It provide batch style api for small scale data, also, provide streaming style api for large scale data.

# 这是一个根据Java Class Field进行CSV序列化/反序列化的工具,基于注解的形式,只需要在相关类上添加注解,就可以实现自定义的CSV序列化/反序列化方案

通过调用CsvSerializer.serialize(*)来进行CSV序列化

相关注解:

  • @CsvIgnore :此注解为标识性注解,@Target({ElementType.FIELD}), 表示该字段在序列化时会被忽略
  • @CsvIgnoreProperties:此注解@Target({ElementType.TYPE}),在类级别指定当前类所拥有字段的哪些在序列化时忽略
  • @CsvProperty:此注解@Target({ElementType.TYPE}),通过指定此注解的headerorder,分别可以实现在序列化时指定指定的字段的csv header的值,字段的序列化顺序。最重要的一个属性是serializationConvertor/deserializationConvertor,如果需要对特定字段在序列化/反序列化时进行一些数据转换的操作,可以通过继承com.alpha.csv.convertor.SerializationConvertor/DeserializationConvertor接口,实现自定义的Convertor。

Demo:

@CsvIgnoreProperties({"country"})    
public class Address {

    private String country = "CN";

    @CsvIgnore
    public String state = "ZJ";

    private String city = "Hangzhou";

}

public class Person {

    @CsvProperty(header="Name", order=1)
    private String name = "Lahahana";

    @CsvProperty(header="ID", order=0)
    private int id = 10001;

    @CsvProperty(header = "Birthday", order=2, convertor = TimeToDateConvertor.class)
    public long birthday= System.currentTimeMillis();

    private Address address = new Address();

    public static void main(String []args) throws CsvException, IOException {
        List persons = new ArrayList();
        Person person = new Person();
        for(int i=0;i<10;i++)
            list.add(person);    
        FileWriter out = new FileWriter("result.csv");
        CsvSerializer serializer = new CsvSerializer.Builder(out).build();
        serializer.serialize(persons, Person.class);
        serializer.close();
    }
}

Result:
ID,Name,Birthday,city
10001,Lahahana,Tue Mar 28 21:14:59 GMT+08:00 2017,Hangzhou
10001,Lahahana,Tue Mar 28 21:14:59 GMT+08:00 2017,Hangzhou

Maven Dependency:

 <dependency>  
     <groupId>com.github.lahahana</groupId>  
     <artifactId>csv</artifactId>  
     <version>2.0.1</version>  
 </dependency>

Versions

Version
2.0.1
1.0.2
1.0.1
1.0.0