Skip to content

Commit 9ce2a42

Browse files
Refactor
1 parent 2016466 commit 9ce2a42

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/main/java/com/howtodoinjava/json/JsonArrayToList.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,41 @@ public class JsonArrayToList {
2525

2626
public static void main(String[] args) throws IOException, URISyntaxException {
2727

28+
//Avoid declaring static methods
29+
JsonArrayToList instance = new JsonArrayToList();
30+
List<Person> personList;
31+
2832
URL fileUrl = JsonArrayToList.class.getClassLoader().getResource("data.json");
2933
Path filePath = Paths.get(fileUrl.toURI());
3034
String jsonArray = Files.readString(filePath);
3135

32-
System.out.println(jsonArray);
36+
//System.out.println(jsonArray);
37+
38+
personList = instance.usingJson(jsonArray);
39+
40+
Assertions.assertEquals(3, personList.size());
41+
Assertions.assertEquals(1, personList.get(0).getId());
42+
Assertions.assertEquals("Alex", personList.get(0).getName());
43+
Assertions.assertEquals(41, personList.get(0).getAge());
44+
45+
personList = instance.usingGson(jsonArray);
3346

34-
usingJson(jsonArray);
35-
usingGson(jsonArray);
36-
usingJackson(jsonArray);
47+
Assertions.assertEquals(3, personList.size());
48+
Assertions.assertEquals(1, personList.get(0).getId());
49+
Assertions.assertEquals("Alex", personList.get(0).getName());
50+
Assertions.assertEquals(41, personList.get(0).getAge());
51+
52+
personList = instance.usingJackson(jsonArray);
53+
54+
Assertions.assertEquals(3, personList.size());
55+
Assertions.assertEquals(1, personList.get(0).getId());
56+
Assertions.assertEquals("Alex", personList.get(0).getName());
57+
Assertions.assertEquals(41, personList.get(0).getAge());
3758
}
3859

39-
static void usingJson(String json) {
60+
List<Person> usingJson(String json) {
4061
JSONArray jsonArray = new JSONArray(json);
41-
List<Person> arrayList = new ArrayList<>();
62+
List<Person> personList = new ArrayList<>();
4263

4364
for (int i = 0; i < jsonArray.length(); i++) {
4465

@@ -49,35 +70,25 @@ static void usingJson(String json) {
4970
int age = jsonPerson.getInt("age");
5071

5172
Person person = new Person(id, name, age);
52-
arrayList.add(person);
73+
personList.add(person);
5374
}
54-
55-
Assertions.assertEquals(3, arrayList.size());
56-
Assertions.assertEquals(1, arrayList.get(0).getId());
57-
Assertions.assertEquals("Alex", arrayList.get(0).getName());
58-
Assertions.assertEquals(41, arrayList.get(0).getAge());
75+
return personList;
5976
}
6077

61-
static void usingGson(String jsonArray) {
78+
List<Person> usingGson(String jsonArray) {
6279
Gson gson = new Gson();
6380
Type listType = new TypeToken<List<Person>>() {}.getType();
6481

65-
List<Person> gsonList = gson.fromJson(jsonArray, listType);
66-
Assertions.assertEquals(3, gsonList.size());
67-
Assertions.assertEquals(1, gsonList.get(0).getId());
68-
Assertions.assertEquals("Alex", gsonList.get(0).getName());
69-
Assertions.assertEquals(41, gsonList.get(0).getAge());
82+
List<Person> personList = gson.fromJson(jsonArray, listType);
83+
return personList;
7084
}
7185

72-
static void usingJackson(String jsonArray) throws JsonProcessingException {
86+
List<Person> usingJackson(String jsonArray) throws JsonProcessingException {
7387
ObjectMapper objectMapper = new ObjectMapper();
7488
TypeReference<List<Person>> jacksonTypeReference = new TypeReference<>() {};
7589

76-
List<Person> jacksonList = objectMapper.readValue(jsonArray, jacksonTypeReference);
77-
Assertions.assertEquals(3, jacksonList.size());
78-
Assertions.assertEquals(1, jacksonList.get(0).getId());
79-
Assertions.assertEquals("Alex", jacksonList.get(0).getName());
80-
Assertions.assertEquals(41, jacksonList.get(0).getAge());
90+
List<Person> personList = objectMapper.readValue(jsonArray, jacksonTypeReference);
91+
return personList;
8192
}
8293
}
8394

0 commit comments

Comments
 (0)