-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJacksonCsvPojoConvertTests.java
138 lines (116 loc) · 3.97 KB
/
JacksonCsvPojoConvertTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package coding.toast.bread.converting;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import lombok.Builder;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Description;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
/**
* Read, Write CSV Using Jackson CSV Library
*/
public class JacksonCsvPojoConvertTests {
@Test
@Description("Reading CSV File and convert to Iterator which contains POJO")
void readTest() {
// read file
var csvFile = new ClassPathResource("sample_csv/customers-100.csv");
// there is header row in our csv!
CsvSchema schema = CsvSchema.emptySchema().withHeader();
// create Reader For Csv File
ObjectReader objectReader = new CsvMapper().readerFor(Customer.class).with(schema);
try (InputStream inputStream = csvFile.getInputStream();
MappingIterator<Customer> iterator = objectReader.readValues(inputStream)) {
while (iterator.hasNext()) {
System.out.println("next = " + iterator.next());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
@Description("Reading CSV File and convert to Iterator which contains Map")
void readTestWithEncoding() {
// read file
var csvFile = new ClassPathResource("sample_csv/customers-100.csv");
// there is header row in our csv!
CsvSchema schema = CsvSchema.emptySchema().withHeader();
// create Reader For Csv File which will eventually create Map instance
ObjectReader objectReader = new CsvMapper().readerFor(new TypeReference<Map<String, String>>() {}).with(schema);
// use Reader for encode
try (InputStreamReader isr = new InputStreamReader(csvFile.getInputStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
MappingIterator<Map<String, String>> iterator = objectReader.readValues(br)) {
while (iterator.hasNext()) {
System.out.println("next = " + iterator.next());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
@Description("Writing list of pojo to CSV")
void writeTest() {
// need to create schema for csv file's first row definition
CsvSchema schema = CsvSchema.builder().
addColumn("firstName").addColumn("lastName").build()
.withHeader();
// create writer
ObjectWriter writer =
new CsvMapper()
.writerFor(new TypeReference<List<Name>>() {})
.with(schema);
// create List of Pojo to Insert into CSV File (or outputStream, Writer, etc...)
List<Name> names = List.of(
Name.builder().firstName("daily").lastName("code").build(),
Name.builder().firstName("coding").lastName("toast").build(),
Name.builder().firstName("hello").lastName("world").build()
);
// Write Csv Data
try (StringWriter stringWriter = new StringWriter()) {
writer.writeValue(stringWriter, names);
System.out.println(stringWriter);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
record Customer(
@JsonProperty("Index")
String index,
@JsonProperty("Customer Id")
String customerId,
@JsonProperty("First Name")
String firstName,
@JsonProperty("Last Name")
String lastName,
@JsonProperty("Company")
String company,
@JsonProperty("City")
String city,
@JsonProperty("Country")
String country,
@JsonProperty("Phone 1")
String phone1,
@JsonProperty("Phone 2")
String phone2,
@JsonProperty("Email")
String email,
@JsonProperty("Subscription Date")
String subscriptionDate,
@JsonProperty("Website")
String website
) {}
@Builder
record Name(
@JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName
) {}
}