Skip to content

Commit cececd4

Browse files
dependencies: replace json-simple with gson (iluwatar#2358)
1 parent 7a3d617 commit cececd4

File tree

7 files changed

+33
-40
lines changed

7 files changed

+33
-40
lines changed

event-sourcing/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@
3939
<artifactId>junit-jupiter-engine</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42-
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
4342
<dependency>
4443
<groupId>com.google.code.gson</groupId>
4544
<artifactId>gson</artifactId>
46-
<version>2.8.9</version>
4745
</dependency>
4846
</dependencies>
4947
<build>

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@
233233
<artifactId>htmlunit</artifactId>
234234
<version>${htmlunit.version}</version>
235235
</dependency>
236+
<dependency>
237+
<groupId>com.google.code.gson</groupId>
238+
<artifactId>gson</artifactId>
239+
<version>2.10</version>
240+
</dependency>
236241
<dependency>
237242
<groupId>com.google.inject</groupId>
238243
<artifactId>guice</artifactId>

typeobjectpattern/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
<artifactId>typeobjectpattern</artifactId>
3636
<dependencies>
3737
<dependency>
38-
<groupId>com.googlecode.json-simple</groupId>
39-
<artifactId>json-simple</artifactId>
40-
<version>1.1.1</version>
38+
<groupId>com.google.code.gson</groupId>
39+
<artifactId>gson</artifactId>
4140
</dependency>
4241
<dependency>
4342
<groupId>org.junit.jupiter</groupId>

typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
*/
2525
package com.iluwatar.typeobject;
2626

27-
import java.io.IOException;
2827
import lombok.extern.slf4j.Slf4j;
29-
import org.json.simple.parser.ParseException;
3028

3129
/**
3230
* <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 {
5452
*
5553
* @param args command line args
5654
*/
57-
public static void main(String[] args) throws IOException, ParseException {
55+
public static void main(String[] args) {
5856
var givenTime = 50; //50ms
5957
var toWin = 500; //points
6058
var pointsWon = 0;

typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
*/
2525
package com.iluwatar.typeobject;
2626

27+
import com.google.gson.JsonParseException;
2728
import com.iluwatar.typeobject.Candy.Type;
2829

29-
import java.io.IOException;
3030
import java.security.SecureRandom;
3131
import java.util.ArrayList;
3232
import java.util.List;
33-
import org.json.simple.parser.ParseException;
3433

3534
/**
3635
* 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) {
8079
pointer++;
8180
}
8281

83-
Candy[] assignRandomCandytypes() throws IOException, ParseException {
82+
Candy[] assignRandomCandytypes() throws JsonParseException {
8483
var jp = new JsonParser();
8584
jp.parse();
8685
var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'

typeobjectpattern/src/main/java/com/iluwatar/typeobject/JsonParser.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@
2424
*/
2525
package com.iluwatar.typeobject;
2626

27+
import com.google.gson.JsonArray;
28+
import com.google.gson.JsonObject;
29+
import com.google.gson.JsonParseException;
2730
import com.iluwatar.typeobject.Candy.Type;
28-
import java.io.File;
29-
import java.io.FileReader;
30-
import java.io.IOException;
31-
import java.util.Hashtable;
32-
import java.util.List;
3331

34-
import org.json.simple.JSONArray;
35-
import org.json.simple.JSONObject;
36-
import org.json.simple.parser.JSONParser;
37-
import org.json.simple.parser.ParseException;
32+
import java.io.InputStreamReader;
33+
import java.util.Hashtable;
3834

3935
/**
4036
* The JsonParser class helps parse the json file candy.json to get all the different candies.
@@ -47,23 +43,21 @@ public class JsonParser {
4743
this.candies = new Hashtable<>();
4844
}
4945

50-
void parse() throws IOException, ParseException {
51-
var parser = new JSONParser();
52-
var workingDirectory = new File("").getAbsolutePath();
53-
var filePath = List.of("src", "main", "java", "com", "iluwatar", "typeobject", "candy.json");
54-
var absolutePath = workingDirectory + File.separator + String.join(File.separator, filePath);
55-
var jo = (JSONObject) parser.parse(new FileReader(absolutePath));
56-
var a = (JSONArray) jo.get("candies");
57-
for (var o : a) {
58-
var candy = (JSONObject) o;
59-
var name = (String) candy.get("name");
60-
var parentName = (String) candy.get("parent");
61-
var t = (String) candy.get("type");
46+
void parse() throws JsonParseException {
47+
var is = this.getClass().getClassLoader().getResourceAsStream("candy.json");
48+
var reader = new InputStreamReader(is);
49+
var json = (JsonObject) com.google.gson.JsonParser.parseReader(reader);
50+
var array = (JsonArray) json.get("candies");
51+
for (var item : array) {
52+
var candy = (JsonObject) item;
53+
var name = candy.get("name").getAsString();
54+
var parentName = candy.get("parent").getAsString();
55+
var t = candy.get("type").getAsString();
6256
var type = Type.CRUSHABLE_CANDY;
6357
if (t.equals("rewardFruit")) {
6458
type = Type.REWARD_FRUIT;
6559
}
66-
var points = Integer.parseInt((String) candy.get("points"));
60+
var points = candy.get("points").getAsInt();
6761
var c = new Candy(name, parentName, type, points);
6862
this.candies.put(name, c);
6963
}

typeobjectpattern/src/main/java/com/iluwatar/typeobject/candy.json renamed to typeobjectpattern/src/main/resources/candy.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,43 @@
33
"name" : "fruit",
44
"parent" : "null",
55
"type" : "rewardFruit",
6-
"points" : "20"
6+
"points" : 20
77
},
88
{
99
"name" : "candy",
1010
"parent" : "null",
1111
"type" : "crushableCandy",
12-
"points" : "10"
12+
"points" : 10
1313
},
1414
{
1515
"name" : "cherry",
1616
"parent" : "fruit",
1717
"type" : "rewardFruit",
18-
"points" : "0"
18+
"points" : 0
1919
},
2020
{
2121
"name" : "mango",
2222
"parent" : "fruit",
2323
"type" : "rewardFruit",
24-
"points" : "0"
24+
"points" : 0
2525
},
2626
{
2727
"name" : "purple popsicle",
2828
"parent" : "candy",
2929
"type" : "crushableCandy",
30-
"points" : "0"
30+
"points" : 0
3131
},
3232
{
3333
"name" : "green jellybean",
3434
"parent" : "candy",
3535
"type" : "crushableCandy",
36-
"points" : "0"
36+
"points" : 0
3737
},
3838
{
3939
"name" : "orange gum",
4040
"parent" : "candy",
4141
"type" : "crushableCandy",
42-
"points" : "0"
42+
"points" : 0
4343
}
4444
]
4545
}

0 commit comments

Comments
 (0)