@@ -25,20 +25,41 @@ public class JsonArrayToList {
25
25
26
26
public static void main (String [] args ) throws IOException , URISyntaxException {
27
27
28
+ //Avoid declaring static methods
29
+ JsonArrayToList instance = new JsonArrayToList ();
30
+ List <Person > personList ;
31
+
28
32
URL fileUrl = JsonArrayToList .class .getClassLoader ().getResource ("data.json" );
29
33
Path filePath = Paths .get (fileUrl .toURI ());
30
34
String jsonArray = Files .readString (filePath );
31
35
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 );
33
46
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 ());
37
58
}
38
59
39
- static void usingJson (String json ) {
60
+ List < Person > usingJson (String json ) {
40
61
JSONArray jsonArray = new JSONArray (json );
41
- List <Person > arrayList = new ArrayList <>();
62
+ List <Person > personList = new ArrayList <>();
42
63
43
64
for (int i = 0 ; i < jsonArray .length (); i ++) {
44
65
@@ -49,35 +70,25 @@ static void usingJson(String json) {
49
70
int age = jsonPerson .getInt ("age" );
50
71
51
72
Person person = new Person (id , name , age );
52
- arrayList .add (person );
73
+ personList .add (person );
53
74
}
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 ;
59
76
}
60
77
61
- static void usingGson (String jsonArray ) {
78
+ List < Person > usingGson (String jsonArray ) {
62
79
Gson gson = new Gson ();
63
80
Type listType = new TypeToken <List <Person >>() {}.getType ();
64
81
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 ;
70
84
}
71
85
72
- static void usingJackson (String jsonArray ) throws JsonProcessingException {
86
+ List < Person > usingJackson (String jsonArray ) throws JsonProcessingException {
73
87
ObjectMapper objectMapper = new ObjectMapper ();
74
88
TypeReference <List <Person >> jacksonTypeReference = new TypeReference <>() {};
75
89
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 ;
81
92
}
82
93
}
83
94
0 commit comments