[BAEL-19675] - Move articles out of jackson part 2
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
package com.baeldung.jackson.csv;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.OrderLine;
|
||||
import com.baeldung.jackson.mixin.OrderLineForCsv;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.MappingIterator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema.Builder;
|
||||
|
||||
public class JsonCsvConverter {
|
||||
|
||||
public static void JsonToCsv(File jsonFile, File csvFile) throws IOException {
|
||||
JsonNode jsonTree = new ObjectMapper().readTree(jsonFile);
|
||||
|
||||
Builder csvSchemaBuilder = CsvSchema.builder();
|
||||
JsonNode firstObject = jsonTree.elements().next();
|
||||
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
|
||||
CsvSchema csvSchema = csvSchemaBuilder
|
||||
.build()
|
||||
.withHeader();
|
||||
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
csvMapper.writerFor(JsonNode.class)
|
||||
.with(csvSchema)
|
||||
.writeValue(csvFile, jsonTree);
|
||||
}
|
||||
|
||||
public static void csvToJson(File csvFile, File jsonFile) throws IOException {
|
||||
CsvSchema orderLineSchema = CsvSchema.emptySchema().withHeader();
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
MappingIterator<OrderLine> orderLines = csvMapper.readerFor(OrderLine.class)
|
||||
.with(orderLineSchema)
|
||||
.readValues(csvFile);
|
||||
|
||||
new ObjectMapper()
|
||||
.configure(SerializationFeature.INDENT_OUTPUT, true)
|
||||
.writeValue(jsonFile, orderLines.readAll());
|
||||
}
|
||||
|
||||
public static void JsonToFormattedCsv(File jsonFile, File csvFile) throws IOException {
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
CsvSchema csvSchema = csvMapper
|
||||
.schemaFor(OrderLineForCsv.class)
|
||||
.withHeader();
|
||||
csvMapper.addMixIn(OrderLine.class, OrderLineForCsv.class);
|
||||
|
||||
OrderLine[] orderLines = new ObjectMapper()
|
||||
.readValue(jsonFile, OrderLine[].class);
|
||||
csvMapper.writerFor(OrderLine[].class)
|
||||
.with(csvSchema)
|
||||
.writeValue(csvFile, orderLines);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package com.baeldung.jackson.entities;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Order {
|
||||
private String orderNo;
|
||||
private LocalDate date;
|
||||
private String customerName;
|
||||
private List<OrderLine> orderLines;
|
||||
|
||||
public Order() {
|
||||
|
||||
}
|
||||
|
||||
public Order(String orderNo, LocalDate date, String customerName, List<OrderLine> orderLines) {
|
||||
super();
|
||||
this.orderNo = orderNo;
|
||||
this.date = date;
|
||||
this.customerName = customerName;
|
||||
this.orderLines = orderLines;
|
||||
}
|
||||
|
||||
public String getOrderNo() {
|
||||
return orderNo;
|
||||
}
|
||||
|
||||
public void setOrderNo(String orderNo) {
|
||||
this.orderNo = orderNo;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public List<OrderLine> getOrderLines() {
|
||||
if (orderLines == null) {
|
||||
orderLines = new ArrayList<>();
|
||||
}
|
||||
return orderLines;
|
||||
}
|
||||
|
||||
public void setOrderLines(List<OrderLine> orderLines) {
|
||||
if (orderLines == null) {
|
||||
orderLines = new ArrayList<>();
|
||||
}
|
||||
this.orderLines = orderLines;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order [orderNo=" + orderNo + ", date=" + date + ", customerName=" + customerName + ", orderLines=" + orderLines + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.jackson.entities;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderLine {
|
||||
private String item;
|
||||
private int quantity;
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
public OrderLine() {
|
||||
|
||||
}
|
||||
|
||||
public OrderLine(String item, int quantity, BigDecimal unitPrice) {
|
||||
super();
|
||||
this.item = item;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
public String getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setItem(String item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public BigDecimal getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
|
||||
public void setUnitPrice(BigDecimal unitPrice) {
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.jackson.entities;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Weather {
|
||||
|
||||
@JsonProperty("location")
|
||||
@JsonAlias("place")
|
||||
private String location;
|
||||
|
||||
@JsonProperty("temp")
|
||||
@JsonAlias("temperature")
|
||||
private int temp;
|
||||
|
||||
@JsonProperty("outlook")
|
||||
@JsonAlias("weather")
|
||||
private String outlook;
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public int getTemp() {
|
||||
return temp;
|
||||
}
|
||||
|
||||
public void setTemp(int temp) {
|
||||
this.temp = temp;
|
||||
}
|
||||
|
||||
public String getOutlook() {
|
||||
return outlook;
|
||||
}
|
||||
|
||||
public void setOutlook(String outlook) {
|
||||
this.outlook = outlook;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.jackson.mixin;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonPropertyOrder({
|
||||
"count",
|
||||
"name"
|
||||
})
|
||||
public abstract class OrderLineForCsv {
|
||||
|
||||
@JsonProperty("name")
|
||||
private String item;
|
||||
|
||||
@JsonProperty("count")
|
||||
private int quantity;
|
||||
|
||||
@JsonIgnore
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
orderNo: A001
|
||||
date: 2019-04-17
|
||||
customerName: Customer, Joe
|
||||
orderLines:
|
||||
- item: No. 9 Sprockets
|
||||
quantity: 12
|
||||
unitPrice: 1.23
|
||||
- item: Widget (10mm)
|
||||
quantity: 4
|
||||
unitPrice: 3.45
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
item,quantity,unitPrice
|
||||
"No. 9 Sprockets",12,1.23
|
||||
"Widget (10mm)",4,3.45
|
||||
|
@@ -1,9 +0,0 @@
|
||||
[ {
|
||||
"item" : "No. 9 Sprockets",
|
||||
"quantity" : 12,
|
||||
"unitPrice" : 1.23
|
||||
}, {
|
||||
"item" : "Widget (10mm)",
|
||||
"quantity" : 4,
|
||||
"unitPrice" : 3.45
|
||||
} ]
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.jackson.csv;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
|
||||
public class CsvUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToCsv(new File("src/main/resources/orderLines.json"),
|
||||
new File("src/main/resources/csvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csvFromJson.csv"),
|
||||
readFile("src/test/resources/expectedCsvFromJson.csv"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.csvToJson(new File("src/main/resources/orderLines.csv"),
|
||||
new File("src/main/resources/jsonFromCsv.json"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/jsonFromCsv.json"),
|
||||
readFile("src/test/resources/expectedJsonFromCsv.json"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/orderLines.json"),
|
||||
new File("src/main/resources/formattedCsvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/formattedCsvFromJson.csv"),
|
||||
readFile("src/test/resources/expectedFormattedCsvFromJson.csv"));
|
||||
|
||||
}
|
||||
|
||||
private List<String> readFile(String filename) throws IOException {
|
||||
return Files.readLines(new File(filename), Charset.forName("utf-8"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
;
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.jackson.deserialization.jsonalias;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.entities.Weather;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonAliasUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoJsonFormats_whenDeserialized_thenWeatherObjectsCreated() throws Exception {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
Weather weather = mapper.readValue("{" +
|
||||
"\"location\": \"London\"," +
|
||||
"\"temp\": 15," +
|
||||
"\"weather\": \"Cloudy\"" +
|
||||
"}", Weather.class);
|
||||
|
||||
assertEquals("London", weather.getLocation());
|
||||
assertEquals("Cloudy", weather.getOutlook());
|
||||
assertEquals(15, weather.getTemp());
|
||||
|
||||
weather = mapper.readValue("{" +
|
||||
"\"place\": \"Lisbon\"," +
|
||||
"\"temperature\": 35," +
|
||||
"\"outlook\": \"Sunny\"" +
|
||||
"}", Weather.class);
|
||||
|
||||
assertEquals("Lisbon", weather.getLocation());
|
||||
assertEquals("Sunny", weather.getOutlook());
|
||||
assertEquals(35, weather.getTemp());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.jackson.yaml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.entities.Order;
|
||||
import com.baeldung.jackson.entities.OrderLine;
|
||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
|
||||
|
||||
public class YamlUnitTest {
|
||||
private ObjectMapper mapper;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
|
||||
mapper.findAndRegisterModules();
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYamlInput_ObjectCreated() throws JsonParseException, JsonMappingException, IOException {
|
||||
Order order = mapper.readValue(new File("src/main/resources/orderInput.yaml"), Order.class);
|
||||
assertEquals("A001", order.getOrderNo());
|
||||
assertEquals(LocalDate.parse("2019-04-17", DateTimeFormatter.ISO_DATE), order.getDate());
|
||||
assertEquals("Customer, Joe", order.getCustomerName());
|
||||
assertEquals(2, order.getOrderLines()
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYamlObject_FileWritten() throws JsonGenerationException, JsonMappingException, IOException {
|
||||
List<OrderLine> lines = new ArrayList<>();
|
||||
lines.add(new OrderLine("Copper Wire (200ft)", 1, new BigDecimal(50.67).setScale(2, RoundingMode.HALF_UP)));
|
||||
lines.add(new OrderLine("Washers (1/4\")", 24, new BigDecimal(.15).setScale(2, RoundingMode.HALF_UP)));
|
||||
Order order = new Order(
|
||||
"B-9910",
|
||||
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
|
||||
"Customer, Jane",
|
||||
lines);
|
||||
mapper.writeValue(new File("src/main/resources/orderOutput.yaml"), order);
|
||||
|
||||
File outputYaml = new File("src/main/resources/orderOutput.yaml");
|
||||
assertTrue(outputYaml.exists());
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
item,quantity,unitPrice
|
||||
"No. 9 Sprockets",12,1.23
|
||||
"Widget (10mm)",4,3.45
|
||||
|
@@ -1,3 +0,0 @@
|
||||
count,name
|
||||
12,"No. 9 Sprockets"
|
||||
4,"Widget (10mm)"
|
||||
|
@@ -1,9 +0,0 @@
|
||||
[ {
|
||||
"item" : "No. 9 Sprockets",
|
||||
"quantity" : 12,
|
||||
"unitPrice" : 1.23
|
||||
}, {
|
||||
"item" : "Widget (10mm)",
|
||||
"quantity" : 4,
|
||||
"unitPrice" : 3.45
|
||||
} ]
|
||||
Reference in New Issue
Block a user