From ad939d477d83c6497c8cfd9b568bb7d9a11944cb Mon Sep 17 00:00:00 2001 From: BHARATH GANESH Date: Sun, 6 Aug 2023 18:10:47 +0530 Subject: [PATCH] BAEL-6504: Convert Json Array to Java list (#14049) --- json-modules/json-conversion/README.md | 5 ++ json-modules/json-conversion/pom.xml | 39 ++++++++++++ .../ConvertJsonArrayToList.java | 31 ++++++++++ .../src/main/java/entities/Product.java | 29 +++++++++ .../src/main/resources/logback.xml | 13 ++++ .../ConvertJsonArrayToListUnitTest.java | 60 +++++++++++++++++++ .../src/test/resources/.gitignore | 13 ++++ .../src/test/resources/logback-test.xml | 19 ++++++ json-modules/pom.xml | 1 + 9 files changed, 210 insertions(+) create mode 100644 json-modules/json-conversion/README.md create mode 100644 json-modules/json-conversion/pom.xml create mode 100644 json-modules/json-conversion/src/main/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToList.java create mode 100644 json-modules/json-conversion/src/main/java/entities/Product.java create mode 100644 json-modules/json-conversion/src/main/resources/logback.xml create mode 100644 json-modules/json-conversion/src/test/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToListUnitTest.java create mode 100644 json-modules/json-conversion/src/test/resources/.gitignore create mode 100644 json-modules/json-conversion/src/test/resources/logback-test.xml diff --git a/json-modules/json-conversion/README.md b/json-modules/json-conversion/README.md new file mode 100644 index 0000000000..7094486afe --- /dev/null +++ b/json-modules/json-conversion/README.md @@ -0,0 +1,5 @@ +## JSON-CONVERSIONS + +This module contains articles about JSON Conversions + +### Relevant Articles: \ No newline at end of file diff --git a/json-modules/json-conversion/pom.xml b/json-modules/json-conversion/pom.xml new file mode 100644 index 0000000000..1af13cbbcb --- /dev/null +++ b/json-modules/json-conversion/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + org.baeldung + json-conversion + json-conversion + + + json-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + org.json + json + ${json.version} + + + com.google.code.gson + gson + ${gson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + 20211205 + 2.10.1 + + + diff --git a/json-modules/json-conversion/src/main/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToList.java b/json-modules/json-conversion/src/main/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToList.java new file mode 100644 index 0000000000..88701e1955 --- /dev/null +++ b/json-modules/json-conversion/src/main/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToList.java @@ -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 convertJsonArrayUsingGsonLibrary(String jsonArray) { + Gson gson = new Gson(); + + Type listType = new TypeToken>() {}.getType(); + + List gsonList = gson.fromJson(jsonArray, listType); + return gsonList; + } + + public List convertJsonArrayUsingJacksonLibrary(String jsonArray) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + + List typeReferenceList = objectMapper.readValue(jsonArray, new TypeReference>() {}); + return typeReferenceList; + } +} diff --git a/json-modules/json-conversion/src/main/java/entities/Product.java b/json-modules/json-conversion/src/main/java/entities/Product.java new file mode 100644 index 0000000000..64ab5064cc --- /dev/null +++ b/json-modules/json-conversion/src/main/java/entities/Product.java @@ -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; + } + +} diff --git a/json-modules/json-conversion/src/main/resources/logback.xml b/json-modules/json-conversion/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/json-modules/json-conversion/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/json-modules/json-conversion/src/test/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToListUnitTest.java b/json-modules/json-conversion/src/test/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToListUnitTest.java new file mode 100644 index 0000000000..d88b75f0ce --- /dev/null +++ b/json-modules/json-conversion/src/test/java/com/baeldung/jsonarraytolist/ConvertJsonArrayToListUnitTest.java @@ -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 productList; + private ConvertJsonArrayToList obj; + + @Before + public void setup() { + obj = new ConvertJsonArrayToList(); + productList = getProducts(); + objectMapper = new ObjectMapper(); + } + + private List getProducts() { + List 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 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 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()); + } + +} diff --git a/json-modules/json-conversion/src/test/resources/.gitignore b/json-modules/json-conversion/src/test/resources/.gitignore new file mode 100644 index 0000000000..df6ca255ed --- /dev/null +++ b/json-modules/json-conversion/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear diff --git a/json-modules/json-conversion/src/test/resources/logback-test.xml b/json-modules/json-conversion/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..b64f069594 --- /dev/null +++ b/json-modules/json-conversion/src/test/resources/logback-test.xml @@ -0,0 +1,19 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + diff --git a/json-modules/pom.xml b/json-modules/pom.xml index 15a066daa4..82314e0edf 100644 --- a/json-modules/pom.xml +++ b/json-modules/pom.xml @@ -16,6 +16,7 @@ json json-2 + json-conversion json-path gson gson-2