Skip to content

Commit dc285bd

Browse files
authored
[JAVA-47201] Created new module json-conversion-2 and moved code from json-conversion (#18613)
1 parent 2bcb6e3 commit dc285bd

File tree

19 files changed

+307
-236
lines changed

19 files changed

+307
-236
lines changed

json-modules/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>org.baeldung</groupId>
7+
<artifactId>json-conversion-2</artifactId>
8+
<name>json-conversion</name>
9+
10+
<parent>
11+
<artifactId>json-modules</artifactId>
12+
<groupId>com.baeldung</groupId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.json</groupId>
19+
<artifactId>json</artifactId>
20+
<version>${json.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.fasterxml.jackson.core</groupId>
24+
<artifactId>jackson-databind</artifactId>
25+
<version>${jackson.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.google.code.gson</groupId>
29+
<artifactId>gson</artifactId>
30+
<version>${gson.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.poi</groupId>
34+
<artifactId>poi-ooxml</artifactId>
35+
<version>${poi-ooxml.version}</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<properties>
40+
<poi-ooxml.version>5.2.5</poi-ooxml.version>
41+
</properties>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package com.baeldung.includenullinjson;
2-
3-
import com.fasterxml.jackson.annotation.JsonProperty;
4-
5-
public class Customer {
6-
@JsonProperty
7-
private final String name;
8-
@JsonProperty
9-
private final String address;
10-
@JsonProperty
11-
private final int age;
12-
13-
public Customer(String name, String address, int age) {
14-
this.name = name;
15-
this.address = address;
16-
this.age = age;
17-
}
18-
19-
@Override
20-
public String toString() {
21-
return "Customer{" +
22-
"name='" + name + '\'' +
23-
", address='" + address + '\'' +
24-
", age=" + age +
25-
'}';
26-
}
1+
package com.baeldung.includenullinjson;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class Customer {
6+
@JsonProperty
7+
private final String name;
8+
@JsonProperty
9+
private final String address;
10+
@JsonProperty
11+
private final int age;
12+
13+
public Customer(String name, String address, int age) {
14+
this.name = name;
15+
this.address = address;
16+
this.age = age;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "Customer{" +
22+
"name='" + name + '\'' +
23+
", address='" + address + '\'' +
24+
", age=" + age +
25+
'}';
26+
}
2727
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.jsonnodetocollection;
22

3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
37
import com.baeldung.jsonnodetocollection.dto.Person;
48
import com.fasterxml.jackson.databind.JsonDeserializer;
59
import com.fasterxml.jackson.databind.JsonNode;
610
import com.fasterxml.jackson.databind.ObjectMapper;
711

8-
import java.io.IOException;
9-
import java.util.ArrayList;
10-
import java.util.List;
11-
1212
public class CustomPersonListDeserializer extends JsonDeserializer<List<Person>> {
1313
@Override
1414
public List<Person> deserialize(com.fasterxml.jackson.core.JsonParser p, com.fasterxml.jackson.databind.DeserializationContext ctxt) throws IOException {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.baeldung.jsonnodetocollection;
22

3-
import com.baeldung.jsonnodetocollection.dto.Person;
4-
import com.fasterxml.jackson.core.type.TypeReference;
5-
import com.fasterxml.jackson.databind.JsonNode;
6-
import com.fasterxml.jackson.databind.ObjectMapper;
7-
83
import java.io.IOException;
94
import java.util.ArrayList;
105
import java.util.HashMap;
116
import java.util.List;
127
import java.util.Map;
138

9+
import com.baeldung.jsonnodetocollection.dto.Person;
10+
import com.fasterxml.jackson.core.type.TypeReference;
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
1414
public class JsonNodeConversionUtil {
1515

1616
static List<Person> manualJsonNodeToList(JsonNode personsNode) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,88 @@
1-
package com.baeldung.exceltojson;
2-
3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import org.apache.poi.ss.usermodel.*;
6-
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
7-
import org.json.JSONArray;
8-
import org.junit.Test;
9-
10-
import java.io.FileInputStream;
11-
import java.io.IOException;
12-
import java.io.InputStream;
13-
import java.util.ArrayList;
14-
import java.util.List;
15-
import java.util.Objects;
16-
17-
import static org.junit.Assert.assertEquals;
18-
19-
public class ExcelToJsonUnitTest {
20-
public static String filePath = Objects.requireNonNull(ExcelToJsonUnitTest.class.getClassLoader().getResource("Book1.xlsx")).getFile();
21-
public String expectedJson = "[[\"C1\",\"C2\",\"C3\",\"C4\",\"C5\"]," +
22-
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
23-
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
24-
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
25-
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]]";
26-
private Workbook workbook;
27-
private Sheet sheet;
28-
private InputStream inputStream;
29-
30-
public ExcelToJsonUnitTest() throws IOException {
31-
inputStream = new FileInputStream(filePath);
32-
workbook = new XSSFWorkbook(inputStream);
33-
sheet = workbook.getSheetAt(0);
34-
}
35-
36-
@Test
37-
public void givenExcelFile_whenUsingApachePOIConversion_thenConvertToJson() {
38-
JSONArray jsonArray = new JSONArray();
39-
40-
Row headerRow = sheet.getRow(0);
41-
List<String> headers = new ArrayList<>();
42-
for (Cell cell : headerRow) {
43-
headers.add(cell.toString());
44-
}
45-
jsonArray.put(headers);
46-
47-
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
48-
Row row = sheet.getRow(i);
49-
List<String> rowData = new ArrayList<>();
50-
for (Cell cell : row) {
51-
rowData.add(cell.toString());
52-
}
53-
jsonArray.put(rowData);
54-
}
55-
56-
assertEquals(expectedJson, jsonArray.toString());
57-
}
58-
59-
@Test
60-
public void givenExcelFile_whenUsingJacksonConversion_thenConvertToJson() throws JsonProcessingException {
61-
List<List<String>> data = new ArrayList<>();
62-
63-
Row headerRow = sheet.getRow(0);
64-
List<String> headers = new ArrayList<>();
65-
for (Cell cell : headerRow) {
66-
headers.add(cell.toString());
67-
}
68-
data.add(headers);
69-
70-
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
71-
Row row = sheet.getRow(i);
72-
List<String> rowData = new ArrayList<>();
73-
for (Cell cell : row) {
74-
rowData.add(cell.toString());
75-
}
76-
data.add(rowData);
77-
}
78-
79-
ObjectMapper objectMapper = new ObjectMapper();
80-
String json = objectMapper.writeValueAsString(data);
81-
82-
assertEquals(expectedJson, json);
83-
}
84-
}
1+
package com.baeldung.exceltojson;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
import org.apache.poi.ss.usermodel.Cell;
13+
import org.apache.poi.ss.usermodel.Row;
14+
import org.apache.poi.ss.usermodel.Sheet;
15+
import org.apache.poi.ss.usermodel.Workbook;
16+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
17+
import org.json.JSONArray;
18+
import org.junit.Test;
19+
20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
23+
public class ExcelToJsonUnitTest {
24+
public static String filePath = Objects.requireNonNull(ExcelToJsonUnitTest.class.getClassLoader().getResource("Book1.xlsx")).getFile();
25+
public String expectedJson = "[[\"C1\",\"C2\",\"C3\",\"C4\",\"C5\"]," +
26+
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
27+
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
28+
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]," +
29+
"[\"1.0\",\"2.0\",\"3.0\",\"4.0\",\"5.0\"]]";
30+
private Workbook workbook;
31+
private Sheet sheet;
32+
private InputStream inputStream;
33+
34+
public ExcelToJsonUnitTest() throws IOException {
35+
inputStream = new FileInputStream(filePath);
36+
workbook = new XSSFWorkbook(inputStream);
37+
sheet = workbook.getSheetAt(0);
38+
}
39+
40+
@Test
41+
public void givenExcelFile_whenUsingApachePOIConversion_thenConvertToJson() {
42+
JSONArray jsonArray = new JSONArray();
43+
44+
Row headerRow = sheet.getRow(0);
45+
List<String> headers = new ArrayList<>();
46+
for (Cell cell : headerRow) {
47+
headers.add(cell.toString());
48+
}
49+
jsonArray.put(headers);
50+
51+
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
52+
Row row = sheet.getRow(i);
53+
List<String> rowData = new ArrayList<>();
54+
for (Cell cell : row) {
55+
rowData.add(cell.toString());
56+
}
57+
jsonArray.put(rowData);
58+
}
59+
60+
assertEquals(expectedJson, jsonArray.toString());
61+
}
62+
63+
@Test
64+
public void givenExcelFile_whenUsingJacksonConversion_thenConvertToJson() throws JsonProcessingException {
65+
List<List<String>> data = new ArrayList<>();
66+
67+
Row headerRow = sheet.getRow(0);
68+
List<String> headers = new ArrayList<>();
69+
for (Cell cell : headerRow) {
70+
headers.add(cell.toString());
71+
}
72+
data.add(headers);
73+
74+
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
75+
Row row = sheet.getRow(i);
76+
List<String> rowData = new ArrayList<>();
77+
for (Cell cell : row) {
78+
rowData.add(cell.toString());
79+
}
80+
data.add(rowData);
81+
}
82+
83+
ObjectMapper objectMapper = new ObjectMapper();
84+
String json = objectMapper.writeValueAsString(data);
85+
86+
assertEquals(expectedJson, json);
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
package com.baeldung.includenullinjson;
2-
3-
import com.fasterxml.jackson.annotation.JsonInclude;
4-
import com.fasterxml.jackson.core.JsonProcessingException;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.google.gson.Gson;
7-
import com.google.gson.GsonBuilder;
8-
import org.junit.Test;
9-
10-
import static org.junit.Assert.assertEquals;
11-
12-
public class IncludeNullValuesInJsonUnitTest {
13-
String expectedJson = "{\"name\":\"John\",\"address\":null,\"age\":25}";
14-
Customer obj = new Customer("John", null, 25);
15-
16-
@Test
17-
public void givenObjectWithNullField_whenJacksonUsed_thenIncludesNullValue() throws JsonProcessingException {
18-
ObjectMapper mapper = new ObjectMapper();
19-
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
20-
String json = mapper.writeValueAsString(obj);
21-
assertEquals(expectedJson, json);
22-
}
23-
24-
@Test
25-
public void givenObjectWithNullField_whenGsonUsed_thenIncludesNullValue() {
26-
Gson gson = new GsonBuilder().serializeNulls().create();
27-
String json = gson.toJson(obj);
28-
assertEquals(expectedJson, json);
29-
}
30-
}
1+
package com.baeldung.includenullinjson;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.fasterxml.jackson.annotation.JsonInclude;
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.google.gson.Gson;
11+
import com.google.gson.GsonBuilder;
12+
13+
public class IncludeNullValuesInJsonUnitTest {
14+
String expectedJson = "{\"name\":\"John\",\"address\":null,\"age\":25}";
15+
Customer obj = new Customer("John", null, 25);
16+
17+
@Test
18+
public void givenObjectWithNullField_whenJacksonUsed_thenIncludesNullValue() throws JsonProcessingException {
19+
ObjectMapper mapper = new ObjectMapper();
20+
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
21+
String json = mapper.writeValueAsString(obj);
22+
assertEquals(expectedJson, json);
23+
}
24+
25+
@Test
26+
public void givenObjectWithNullField_whenGsonUsed_thenIncludesNullValue() {
27+
Gson gson = new GsonBuilder().serializeNulls().create();
28+
String json = gson.toJson(obj);
29+
assertEquals(expectedJson, json);
30+
}
31+
}

0 commit comments

Comments
 (0)