2021-08-05
This commit is contained in:
18
jackson/build.gradle.kts
Normal file
18
jackson/build.gradle.kts
Normal 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
20
jackson/logback.xml
Normal 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>
|
||||
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);
|
||||
}
|
||||
}
|
||||
4
jackson/test.csv
Normal file
4
jackson/test.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
name,age,male
|
||||
Charlie,11,true
|
||||
Steve,34,true
|
||||
Jane,22,false
|
||||
|
5
jackson/test.xml
Normal file
5
jackson/test.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<Person>
|
||||
<name>Charlie</name>
|
||||
<age>11</age>
|
||||
<male>true</male>
|
||||
</Person>
|
||||
4
jackson/test.yaml
Normal file
4
jackson/test.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
---
|
||||
name: "Charlie"
|
||||
age: 11
|
||||
male: true
|
||||
Reference in New Issue
Block a user