Repository: iluwatar/java-design-patterns
Issue: Upgrade dependencies to support Java 17
Labels: info: good first issue
epic: build issue
epic: dependencies
type: feature

This issue is a part of the transition plan at https://github.com/iluwatar/java-design-patterns/wiki/19.-Java-17-transition. Here we upgrade the dependencies so that they support Java 17.

Acceptance criteria

- Project dependencies have been upgraded so that each of them supports Java 17


Issue comments:
Hi I am interested in your project 
Please Hire me
I would like to work on this issue . Please assign it to me

I can work on this issue. Assign it to me
To be fair, @akhilpamwani expressed interest first
Hey, its been 9 days since... do we have any upgrade ? . If not then I would like to work on this one. 
Hello Sir,
I would like to contribute to this project,
I am new to hacktoctoberfest and this would be a great beginning.
Thank you...
@iluwatar could you assign it to me?
Assigned to you @robertvolkmann 

Pull request review comments:
Need to import this package com.google.gson.JsonParser.parseReader(reader)and can be used 
 JsonParser.parseReader(reader);
Works only if I rename the class `JsonParser` otherwise we have a name conflict.
Is there any other solution to fix it. Because, it is not looks good as per code readability.

Patch that resolved the issue:
diff --git a/event-sourcing/pom.xml b/event-sourcing/pom.xml
index 047c1ea0d293..ae609d67e76b 100644
--- a/event-sourcing/pom.xml
+++ b/event-sourcing/pom.xml
@@ -39,11 +39,9 @@
       <artifactId>junit-jupiter-engine</artifactId>
       <scope>test</scope>
     </dependency>
-    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
     <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
-      <version>2.8.9</version>
     </dependency>
   </dependencies>
   <build>
diff --git a/pom.xml b/pom.xml
index 95247640c84f..ad1bd20560de 100644
--- a/pom.xml
+++ b/pom.xml
@@ -254,6 +254,11 @@
         <artifactId>htmlunit</artifactId>
         <version>${htmlunit.version}</version>
       </dependency>
+      <dependency>
+        <groupId>com.google.code.gson</groupId>
+        <artifactId>gson</artifactId>
+        <version>2.10</version>
+      </dependency>
       <dependency>
         <groupId>com.google.inject</groupId>
         <artifactId>guice</artifactId>
diff --git a/typeobjectpattern/pom.xml b/typeobjectpattern/pom.xml
index 00aedd506bc7..6fe65fee84a9 100644
--- a/typeobjectpattern/pom.xml
+++ b/typeobjectpattern/pom.xml
@@ -35,9 +35,8 @@
   <artifactId>typeobjectpattern</artifactId>
   <dependencies>
     <dependency>
-      <groupId>com.googlecode.json-simple</groupId>
-      <artifactId>json-simple</artifactId>
-      <version>1.1.1</version>
+      <groupId>com.google.code.gson</groupId>
+      <artifactId>gson</artifactId>
     </dependency>
     <dependency>
       <groupId>org.junit.jupiter</groupId>
diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java
index de2837b71885..d120baca0f2c 100644
--- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java
+++ b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java
@@ -24,9 +24,7 @@
  */
 package com.iluwatar.typeobject;
 
-import java.io.IOException;
 import lombok.extern.slf4j.Slf4j;
-import org.json.simple.parser.ParseException;
 
 /**
  * <p>Type object pattern is the pattern we use when the OOP concept of creating a base class and
@@ -54,7 +52,7 @@ public class App {
    *
    * @param args command line args
    */
-  public static void main(String[] args) throws IOException, ParseException {
+  public static void main(String[] args) {
     var givenTime = 50; //50ms
     var toWin = 500; //points
     var pointsWon = 0;
diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java
index 7dd423af67a8..94f8ec6632b1 100644
--- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java
+++ b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java
@@ -24,13 +24,12 @@
  */
 package com.iluwatar.typeobject;
 
+import com.google.gson.JsonParseException;
 import com.iluwatar.typeobject.Candy.Type;
 
-import java.io.IOException;
 import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.List;
-import org.json.simple.parser.ParseException;
 
 /**
  * The CellPool class allows the reuse of crushed cells instead of creation of new cells each time.
@@ -80,7 +79,7 @@ void addNewCell(Cell c) {
     pointer++;
   }
 
-  Candy[] assignRandomCandytypes() throws IOException, ParseException {
+  Candy[] assignRandomCandytypes() throws JsonParseException {
     var jp = new JsonParser();
     jp.parse();
     var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/JsonParser.java b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/JsonParser.java
index b9db69eb716d..da0588aa2607 100644
--- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/JsonParser.java
+++ b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/JsonParser.java
@@ -24,17 +24,13 @@
  */
 package com.iluwatar.typeobject;
 
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
 import com.iluwatar.typeobject.Candy.Type;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.Hashtable;
-import java.util.List;
 
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
-import org.json.simple.parser.JSONParser;
-import org.json.simple.parser.ParseException;
+import java.io.InputStreamReader;
+import java.util.Hashtable;
 
 /**
  * The JsonParser class helps parse the json file candy.json to get all the different candies.
@@ -47,23 +43,21 @@ public class JsonParser {
     this.candies = new Hashtable<>();
   }
 
-  void parse() throws IOException, ParseException {
-    var parser = new JSONParser();
-    var workingDirectory = new File("").getAbsolutePath();
-    var filePath = List.of("src", "main", "java", "com", "iluwatar", "typeobject", "candy.json");
-    var absolutePath = workingDirectory + File.separator + String.join(File.separator, filePath);
-    var jo = (JSONObject) parser.parse(new FileReader(absolutePath));
-    var a = (JSONArray) jo.get("candies");
-    for (var o : a) {
-      var candy = (JSONObject) o;
-      var name = (String) candy.get("name");
-      var parentName = (String) candy.get("parent");
-      var t = (String) candy.get("type");
+  void parse() throws JsonParseException {
+    var is = this.getClass().getClassLoader().getResourceAsStream("candy.json");
+    var reader = new InputStreamReader(is);
+    var json = (JsonObject) com.google.gson.JsonParser.parseReader(reader);
+    var array = (JsonArray) json.get("candies");
+    for (var item : array) {
+      var candy = (JsonObject) item;
+      var name = candy.get("name").getAsString();
+      var parentName = candy.get("parent").getAsString();
+      var t = candy.get("type").getAsString();
       var type = Type.CRUSHABLE_CANDY;
       if (t.equals("rewardFruit")) {
         type = Type.REWARD_FRUIT;
       }
-      var points = Integer.parseInt((String) candy.get("points"));
+      var points = candy.get("points").getAsInt();
       var c = new Candy(name, parentName, type, points);
       this.candies.put(name, c);
     }
diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/candy.json b/typeobjectpattern/src/main/resources/candy.json
similarity index 82%
rename from typeobjectpattern/src/main/java/com/iluwatar/typeobject/candy.json
rename to typeobjectpattern/src/main/resources/candy.json
index 74430c318d72..61c5fd4c3eb3 100644
--- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/candy.json
+++ b/typeobjectpattern/src/main/resources/candy.json
@@ -3,43 +3,43 @@
     "name" : "fruit",
     "parent" : "null",
     "type" : "rewardFruit",
-    "points" : "20"
+    "points" : 20
   },
   {
     "name" : "candy",
     "parent" : "null",
     "type" : "crushableCandy",
-    "points" : "10"
+    "points" : 10
   },
   {
     "name" : "cherry",
     "parent" : "fruit",
     "type" : "rewardFruit",
-    "points" : "0"
+    "points" : 0
   },
   {
     "name" : "mango",
     "parent" : "fruit",
     "type" : "rewardFruit",
-    "points" : "0"
+    "points" : 0
   },
   {
     "name" : "purple popsicle",
     "parent" : "candy",
     "type" : "crushableCandy",
-    "points" : "0"
+    "points" : 0
   },
   {
     "name" : "green jellybean",
     "parent" : "candy",
     "type" : "crushableCandy",
-    "points" : "0"
+    "points" : 0
   },
   {
     "name" : "orange gum",
     "parent" : "candy",
     "type" : "crushableCandy",
-    "points" : "0"
+    "points" : 0
   }
   ]
 }


Test patch: