2021-08-05

This commit is contained in:
2021-08-05 17:59:24 +09:00
parent 5f17645bda
commit a69e814fdb
43 changed files with 1017 additions and 28 deletions

View 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;
}

View File

@@ -0,0 +1,8 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}