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

18
jackson/build.gradle.kts Normal file
View File

@@ -0,0 +1,18 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-java")
}
dependencies {
implementation ("com.fasterxml.jackson.core:jackson-databind:2.12.1")
implementation ("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.12.1")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.12.1")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.1")
}

20
jackson/logback.xml Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Examples for Java
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="CONSOLE" />
</root>
</configuration>

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

4
jackson/test.csv Normal file
View File

@@ -0,0 +1,4 @@
name,age,male
Charlie,11,true
Steve,34,true
Jane,22,false
1 name age male
2 Charlie 11 true
3 Steve 34 true
4 Jane 22 false

5
jackson/test.xml Normal file
View File

@@ -0,0 +1,5 @@
<Person>
<name>Charlie</name>
<age>11</age>
<male>true</male>
</Person>

4
jackson/test.yaml Normal file
View File

@@ -0,0 +1,4 @@
---
name: "Charlie"
age: 11
male: true