-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.cpp
97 lines (77 loc) · 2.91 KB
/
JSON.cpp
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
94
95
96
97
#include "JSON.h"
#include <fstream>
using json = nlohmann::json;
std::string JSON::DefaultAccountFile = "accounts.json";
void JSON::AddAccountToFile(std::string account_id, std::string username, std::string deviceId, std::string secret, JSON::File file) {
json jsonData;
std::ifstream inFile(file == JSON::File::ACCOUNTS ? "accounts.json" : "");
if (inFile.good()) {
try { inFile >> jsonData; }
catch (const json::parse_error& e) { jsonData = json::object(); }
inFile.close();
}
json newEntry = {
{"username", username},
{"deviceId", deviceId},
{"secret", secret}
};
json newArray = json::array();
newArray.push_back(newEntry);
jsonData[account_id] = newArray;
std::ofstream outFile(file == JSON::File::ACCOUNTS ? "accounts.json" : "");
outFile << std::setw(4) << jsonData << std::endl;
outFile.close();
}
void JSON::WriteDebugToFile(std::string fullFilePath, nlohmann::json jsonData) {
std::ifstream inFile(fullFilePath);
if (inFile.good()) {
try { inFile >> jsonData; }
catch (const json::parse_error& e) { jsonData = json::object(); }
inFile.close();
}
std::ofstream outFile(fullFilePath);
outFile << std::setw(4) << jsonData << std::endl;
outFile.close();
}
nlohmann::json JSON::GetAccountInformation(std::string account_id, JSON::File file) {
json jsonData;
std::ifstream inFile(file == JSON::File::ACCOUNTS ? "accounts.json" : "");
if (!inFile.good()) { return nullptr; }
try { inFile >> jsonData; }
catch (const json::parse_error& e) { return nullptr; }
if (!jsonData.contains(account_id) || jsonData[account_id].empty() || !jsonData[account_id].is_array()) { return nullptr; }
const auto& entry = jsonData[account_id][0];
if (!entry.contains("username") || !entry.contains("deviceId") || !entry.contains("secret")) { return nullptr; }
return entry;
}
std::string JSON::GetAccountUsername(std::string account_id, JSON::File file) {
return GetAccountInformation(account_id, file)["username"];
}
std::string JSON::GetIdFromUsername(std::string username, JSON::File file) {
json jsonData;
std::ifstream inFile(file == JSON::File::ACCOUNTS ? "accounts.json" : "");
if (!inFile.good()) { return "nullptr"; }
try { inFile >> jsonData; }
catch (const json::parse_error& e) { return "nullptr"; }
for (auto it = jsonData.begin(); it != jsonData.end(); ++it) {
for (const auto& user : it.value()) {
if (user["username"] == username) {
return it.key();
break;
}
}
}
}
std::vector<std::string> JSON::GetAllUsers() {
nlohmann::json j;
std::ifstream inFile("accounts.json");
if (!inFile.good()) {
return {};
}
inFile >> j;
std::vector<std::string> users;
for (auto it = j.begin(); it != j.end(); ++it) {
users.push_back(it.key());
}
return users;
}