BAEL-6504: Convert Json Array to Java list (#14049)
This commit is contained in:
parent
b9dd050af8
commit
ad939d477d
5
json-modules/json-conversion/README.md
Normal file
5
json-modules/json-conversion/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
## JSON-CONVERSIONS
|
||||||
|
|
||||||
|
This module contains articles about JSON Conversions
|
||||||
|
|
||||||
|
### Relevant Articles:
|
39
json-modules/json-conversion/pom.xml
Normal file
39
json-modules/json-conversion/pom.xml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.baeldung</groupId>
|
||||||
|
<artifactId>json-conversion</artifactId>
|
||||||
|
<name>json-conversion</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>json-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>${json.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>${gson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<json.version>20211205</json.version>
|
||||||
|
<gson.version>2.10.1</gson.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.jsonarraytolist;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
|
import entities.Product;
|
||||||
|
|
||||||
|
public class ConvertJsonArrayToList {
|
||||||
|
|
||||||
|
public List<Product> convertJsonArrayUsingGsonLibrary(String jsonArray) {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
|
||||||
|
Type listType = new TypeToken<List<Product>>() {}.getType();
|
||||||
|
|
||||||
|
List<Product> gsonList = gson.fromJson(jsonArray, listType);
|
||||||
|
return gsonList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Product> convertJsonArrayUsingJacksonLibrary(String jsonArray) throws JsonProcessingException {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
List<Product> typeReferenceList = objectMapper.readValue(jsonArray, new TypeReference<List<Product>>() {});
|
||||||
|
return typeReferenceList;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package entities;
|
||||||
|
|
||||||
|
public class Product {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
public Product() {}
|
||||||
|
|
||||||
|
public Product(int id, String name, String description) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
json-modules/json-conversion/src/main/resources/logback.xml
Normal file
13
json-modules/json-conversion/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.baeldung.jsonarraytolist;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import entities.Product;
|
||||||
|
|
||||||
|
public class ConvertJsonArrayToListUnitTest {
|
||||||
|
|
||||||
|
private static ObjectMapper objectMapper;
|
||||||
|
private static List<Product> productList;
|
||||||
|
private ConvertJsonArrayToList obj;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
obj = new ConvertJsonArrayToList();
|
||||||
|
productList = getProducts();
|
||||||
|
objectMapper = new ObjectMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Product> getProducts() {
|
||||||
|
List<Product> productList = new ArrayList<>();
|
||||||
|
Product prod1 = new Product(1, "Icecream", "Sweet and cold");
|
||||||
|
Product prod2 = new Product(2, "Apple", "Red and sweet");
|
||||||
|
Product prod3 = new Product(3, "Carrot", "Good for eyes");
|
||||||
|
productList.add(prod1);
|
||||||
|
productList.add(prod2);
|
||||||
|
productList.add(prod3);
|
||||||
|
return productList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingGsonLibrary_thenCompareTwoProducts() {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String jsonArray = gson.toJson(productList);
|
||||||
|
List<Product> arrList = obj.convertJsonArrayUsingGsonLibrary(jsonArray);
|
||||||
|
Assert.assertEquals(productList.get(0).getId(), arrList.get(0).getId());
|
||||||
|
Assert.assertEquals(productList.get(1).getDescription(), arrList.get(1).getDescription());
|
||||||
|
Assert.assertEquals(productList.get(2).getName(), arrList.get(2).getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingJacksonLibrary_thenCompareTwoProducts() throws JsonProcessingException {
|
||||||
|
String jsonArray = objectMapper.writeValueAsString(productList);
|
||||||
|
List<Product> arrList = obj.convertJsonArrayUsingJacksonLibrary(jsonArray);
|
||||||
|
|
||||||
|
Assert.assertEquals(productList.get(0).getId(), arrList.get(0).getId());
|
||||||
|
Assert.assertEquals(productList.get(1).getDescription(), arrList.get(1).getDescription());
|
||||||
|
Assert.assertEquals(productList.get(2).getName(), arrList.get(2).getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
json-modules/json-conversion/src/test/resources/.gitignore
vendored
Normal file
13
json-modules/json-conversion/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.springframework" level="WARN"/>
|
||||||
|
<logger name="org.springframework.transaction" level="WARN"/>
|
||||||
|
|
||||||
|
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||||
|
<logger name="org.springframework.web.servlet.mvc" level="WARN"/>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -16,6 +16,7 @@
|
|||||||
<modules>
|
<modules>
|
||||||
<module>json</module>
|
<module>json</module>
|
||||||
<module>json-2</module>
|
<module>json-2</module>
|
||||||
|
<module>json-conversion</module>
|
||||||
<module>json-path</module>
|
<module>json-path</module>
|
||||||
<module>gson</module>
|
<module>gson</module>
|
||||||
<module>gson-2</module>
|
<module>gson-2</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user