2021-08-05
This commit is contained in:
27
jackson/src/main/java/kr/pe/elex/examples/Person.java
Normal file
27
jackson/src/main/java/kr/pe/elex/examples/Person.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@JsonPropertyOrder({"name", "age", "male"})
|
||||
public class Person {
|
||||
@JsonProperty
|
||||
private String name;
|
||||
@JsonProperty
|
||||
private int age;
|
||||
@JsonProperty
|
||||
private boolean male;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import com.fasterxml.jackson.databind.MappingIterator;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class JacksonCsvTest {
|
||||
private static List<Person> list;
|
||||
|
||||
@BeforeAll
|
||||
private static void beforeAll() {
|
||||
list = new ArrayList<>();
|
||||
list.add(new Person("Charlie", 11, true));
|
||||
list.add(new Person("Steve", 34, true));
|
||||
list.add(new Person("Jane", 22, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeToCsv() throws IOException {
|
||||
CsvMapper mapper = new CsvMapper();
|
||||
CsvSchema schema = mapper
|
||||
.schemaFor(Person.class)
|
||||
.withHeader();
|
||||
|
||||
//String csvOut = mapper.writer(schema).writeValueAsString(list);
|
||||
//System.out.println(csvOut);
|
||||
|
||||
mapper.writer(schema)
|
||||
.writeValue(new File("test.csv"), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
void readFromCsv() throws IOException {
|
||||
CsvMapper mapper = new CsvMapper();
|
||||
CsvSchema schema = mapper
|
||||
.schemaFor(Person.class)
|
||||
.withHeader();
|
||||
|
||||
MappingIterator<Person> iterator = mapper.readerFor(Person.class)
|
||||
.with(schema)
|
||||
.readValues(new File("test.csv"));
|
||||
while(iterator.hasNext()){
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
class JacksonXmlTest {
|
||||
|
||||
|
||||
@Test
|
||||
void write() throws IOException {
|
||||
XmlMapper mapper = XmlMapper.builder()
|
||||
.defaultUseWrapper(false)
|
||||
.enable(SerializationFeature.INDENT_OUTPUT)
|
||||
.build();
|
||||
mapper
|
||||
.writeValue(new File("test.xml"), new Person("Charlie", 11, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void read() throws IOException {
|
||||
|
||||
ObjectMapper mapper = new XmlMapper();
|
||||
Person person = mapper.readValue(new File("test.xml"), Person.class);
|
||||
|
||||
System.out.println(person);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
class JacksonYamlTest {
|
||||
|
||||
|
||||
@Test
|
||||
void write() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
|
||||
mapper.writeValue(new File("test.yaml"), new Person("Charlie", 11, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void read() throws IOException {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
|
||||
Person person = mapper.readValue(new File("test.yaml"), Person.class);
|
||||
|
||||
System.out.println(person);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user