-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonInput.java
More file actions
93 lines (84 loc) · 4.54 KB
/
Copy pathJsonInput.java
File metadata and controls
93 lines (84 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import org.example.entities.Account;
import org.example.entities.Credentials;
import org.example.entities.characters.Character;
import org.example.entities.characters.Mage;
import org.example.entities.characters.Rogue;
import org.example.entities.characters.Warrior;
import org.example.entities.characters.*;
import org.json.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;
public class JsonInput {
public static ArrayList<Account> deserializeAccounts() {
String accountPath = "D:\\POO\\Tema2\\PROIECT\\accounts.json";
try {
String content = new String((Files.readAllBytes(Paths.get(accountPath))));
JSONObject obj = new JSONObject(content);
JSONArray accountsArray = (JSONArray) obj.get("accounts");
ArrayList<Account> accounts = new ArrayList<>();
for (int i=0; i < accountsArray.length(); i++) {
JSONObject accountJson = (JSONObject) accountsArray.get(i);
// name, country, games_number
String name = (String) accountJson.get("name");
String country = (String) accountJson.get("country");
int gamesNumber = Integer.parseInt((String)accountJson.get("maps_completed"));
// Credentials
Credentials credentials = null;
try {
JSONObject credentialsJson = (JSONObject) accountJson.get("credentials");
String email = (String) credentialsJson.get("email");
String password = (String) credentialsJson.get("password");
credentials = new Credentials(email, password);
} catch (JSONException e) {
System.out.println("! This account doesn't have all credentials !");
}
// Favorite games
SortedSet<String> favoriteGames = new TreeSet();
try {
JSONArray games = (JSONArray) accountJson.get("favorite_games");
for (int j = 0; j < games.length(); j++) {
favoriteGames.add((String) games.get(j));
}
} catch (JSONException e) {
System.out.println("! This account doesn't have favorite games !");
}
// Characters
ArrayList<Character> characters = new ArrayList<>();
try {
JSONArray charactersListJson = (JSONArray) accountJson.get("characters");
for (int j = 0; j < charactersListJson.length(); j++) {
JSONObject charJson = (JSONObject) charactersListJson.get(j);
String cname = (String) charJson.get("name");
String profession = (String) charJson.get("profession");
String level = (String) charJson.get("level");
int lvl = Integer.parseInt(level);
Integer experience = (Integer) charJson.get("experience");
Character newCharacter = CharacterFactory.createCharacter(profession,cname,experience,lvl);
// Character newCharacter = null;
// if (profession.equals("Warrior"))
// newCharacter = new Warrior(cname, experience, lvl);
// if (profession.equals("Rogue"))
// newCharacter = new Rogue(cname, experience, lvl);
// if (profession.equals("Mage"))
// newCharacter = new Mage(cname, experience, lvl);
characters.add(newCharacter);
}
} catch (JSONException e) {
System.out.println("! This account doesn't have characters !");
}
//Account.Information information = new Account.Information(credentials, favoriteGames, name, country);
Account.Information information = new Account.Information.InformationBuilder().creds(credentials).favGames(favoriteGames).name(name).country(country).build();
Account account = new Account(characters, gamesNumber, information);
accounts.add(account);
}
return accounts;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}