BAEL-6504: Convert Json Array to Java list (#14049)

This commit is contained in:
BHARATH GANESH
2023-08-06 18:10:47 +05:30
committed by GitHub
parent b9dd050af8
commit ad939d477d
9 changed files with 210 additions and 0 deletions
@@ -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;
}
}
@@ -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());
}
}
@@ -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>