[BAEL-13511] - Create jackson-modules parent for all related modules
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
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/csv/orderLines.json"),
|
||||
new File("src/main/resources/csv/csvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csv/csvFromJson.csv"),
|
||||
readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"),
|
||||
new File("src/main/resources/csv/jsonFromCsv.json"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csv/jsonFromCsv.json"),
|
||||
readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"),
|
||||
new File("src/main/resources/csv/formattedCsvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csv/formattedCsvFromJson.csv"),
|
||||
readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv"));
|
||||
|
||||
}
|
||||
|
||||
private List<String> readFile(String filename) throws IOException {
|
||||
return Files.readLines(new File(filename), Charset.forName("utf-8"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
;
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.jackson.dynamicobject;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class DynamicObjectDeserializationUnitTest {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
private String readResource(String path) {
|
||||
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(path), "UTF-8")) {
|
||||
return scanner.useDelimiter("\\A").next();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenParsingToJsonNode_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException {
|
||||
// given
|
||||
String json = readResource("/deserialize-dynamic-object/embedded.json");
|
||||
|
||||
// when
|
||||
ProductJsonNode product = objectMapper.readValue(json, ProductJsonNode.class);
|
||||
|
||||
// then
|
||||
assertThat(product.getName()).isEqualTo("Pear yPhone 72");
|
||||
assertThat(product.getDetails().get("audioConnector").asText()).isEqualTo("none");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenParsingToMap_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException {
|
||||
// given
|
||||
String json = readResource("/deserialize-dynamic-object/embedded.json");
|
||||
|
||||
// when
|
||||
ProductMap product = objectMapper.readValue(json, ProductMap.class);
|
||||
|
||||
// then
|
||||
assertThat(product.getName()).isEqualTo("Pear yPhone 72");
|
||||
assertThat(product.getDetails().get("audioConnector")).isEqualTo("none");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenParsingWithJsonAnySetter_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException {
|
||||
// given
|
||||
String json = readResource("/deserialize-dynamic-object/flat.json");
|
||||
|
||||
// when
|
||||
Product product = objectMapper.readValue(json, Product.class);
|
||||
|
||||
// then
|
||||
assertThat(product.getName()).isEqualTo("Pear yPhone 72");
|
||||
assertThat(product.getDetails().get("audioConnector")).isEqualTo("none");
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.jackson.multiplefields;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class MapMultipleFieldsToSingleFieldUnitTest {
|
||||
|
||||
@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());
|
||||
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.baeldung.jackson.streaming;
|
||||
|
||||
import com.fasterxml.jackson.core.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class StreamingAPIUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenJsonGenerator_whenAppendJsonToIt_thenGenerateJson() throws IOException {
|
||||
// given
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
JsonGenerator jGenerator = jfactory.createGenerator(stream, JsonEncoding.UTF8);
|
||||
|
||||
// when
|
||||
jGenerator.writeStartObject();
|
||||
jGenerator.writeStringField("name", "Tom");
|
||||
jGenerator.writeNumberField("age", 25);
|
||||
jGenerator.writeFieldName("address");
|
||||
jGenerator.writeStartArray();
|
||||
jGenerator.writeString("Poland");
|
||||
jGenerator.writeString("5th avenue");
|
||||
jGenerator.writeEndArray();
|
||||
jGenerator.writeEndObject();
|
||||
jGenerator.close();
|
||||
|
||||
// then
|
||||
String json = new String(stream.toByteArray(), "UTF-8");
|
||||
assertEquals(json, "{\"name\":\"Tom\",\"age\":25,\"address\":[\"Poland\",\"5th avenue\"]}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJson_whenReadItUsingStreamAPI_thenShouldCreateProperJsonObject() throws IOException {
|
||||
// given
|
||||
String json = "{\"name\":\"Tom\",\"age\":25,\"address\":[\"Poland\",\"5th avenue\"]}";
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
JsonParser jParser = jfactory.createParser(json);
|
||||
|
||||
String parsedName = null;
|
||||
Integer parsedAge = null;
|
||||
List<String> addresses = new LinkedList<>();
|
||||
|
||||
// when
|
||||
while (jParser.nextToken() != JsonToken.END_OBJECT) {
|
||||
|
||||
String fieldname = jParser.getCurrentName();
|
||||
if ("name".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
parsedName = jParser.getText();
|
||||
|
||||
}
|
||||
|
||||
if ("age".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
parsedAge = jParser.getIntValue();
|
||||
|
||||
}
|
||||
|
||||
if ("address".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
|
||||
while (jParser.nextToken() != JsonToken.END_ARRAY) {
|
||||
addresses.add(jParser.getText());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
jParser.close();
|
||||
|
||||
// then
|
||||
assertEquals(parsedName, "Tom");
|
||||
assertEquals(parsedAge, (Integer) 25);
|
||||
assertEquals(addresses, Arrays.asList("Poland", "5th avenue"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJson_whenWantToExtractPartOfIt_thenShouldExtractOnlyNeededFieldWithoutGoingThroughWholeJSON() throws IOException {
|
||||
// given
|
||||
String json = "{\"name\":\"Tom\",\"age\":25,\"address\":[\"Poland\",\"5th avenue\"]}";
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
JsonParser jParser = jfactory.createParser(json);
|
||||
|
||||
String parsedName = null;
|
||||
Integer parsedAge = null;
|
||||
List<String> addresses = new LinkedList<>();
|
||||
|
||||
// when
|
||||
while (jParser.nextToken() != JsonToken.END_OBJECT) {
|
||||
|
||||
String fieldname = jParser.getCurrentName();
|
||||
|
||||
if ("age".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
parsedAge = jParser.getIntValue();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
jParser.close();
|
||||
|
||||
// then
|
||||
assertNull(parsedName);
|
||||
assertEquals(parsedAge, (Integer) 25);
|
||||
assertTrue(addresses.isEmpty());
|
||||
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToJsonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
|
||||
|
||||
assertEquals(poppy.getName(), "Poppy");
|
||||
assertEquals(poppy.getColor(), Color.RED);
|
||||
assertEquals(poppy.getPetals(), new Integer(9));
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = mapper.writeValueAsString(poppy);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException {
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
|
||||
|
||||
ObjectMapper jsonMapper = new ObjectMapper();
|
||||
String json = jsonMapper.writeValueAsString(node);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
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.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/test/resources/yaml/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/test/resources/yaml/orderOutput.yaml"), order);
|
||||
|
||||
File outputYaml = new File("src/test/resources/yaml/orderOutput.yaml");
|
||||
assertTrue(outputYaml.exists());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user