-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminejson.h
More file actions
167 lines (127 loc) · 3.97 KB
/
Copy pathminejson.h
File metadata and controls
167 lines (127 loc) · 3.97 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Created by Micael Cossa on 29/07/2025.
//
#ifndef MINECRAFTSERVER_JSON_READER_H
#define MINECRAFTSERVER_JSON_READER_H
#include <unordered_map>
#include <string>
#include <utility>
#include <vector>
enum class JSON_NODE_TYPE : unsigned short int {
JSON_OBJECT = 0,
JSON_NUMBER = 1,
JSON_STRING = 3,
JSON_ARRAY = 4,
JSON_INVALID = 100
};
// at some point in the future i will do this
class json_node {
public:
virtual ~json_node() = default;
[[nodiscard]] virtual std::string toString() const = 0;
[[nodiscard]] virtual JSON_NODE_TYPE getType() const {
return JSON_NODE_TYPE::JSON_INVALID;
}
};
class json_string : public json_node {
private:
std::string value;
public:
explicit json_string(std::string basicString) : value(std::move(basicString)) {}
~json_string() override = default;
[[nodiscard]] JSON_NODE_TYPE getType() const override {
return JSON_NODE_TYPE::JSON_STRING;
}
[[nodiscard]] std::string toString() const override {
return "\"" + value + "\"";
}
};
class json_number : public json_node {
private:
std::string originalValue;
long long number;
public:
explicit json_number(long long number, std::string originalValue) : originalValue(std::move(originalValue)) , number(number) {}
~json_number() override = default;
[[nodiscard]] JSON_NODE_TYPE getType() const override {
return JSON_NODE_TYPE::JSON_NUMBER;
}
[[nodiscard]] unsigned long long getNumber() const{
return number;
}
[[nodiscard]] double convertToDecimal() const {
// blalablala convert the originalValue to decimal
// i cba to do this
return 0.0;
}
[[nodiscard]] std::string toString() const override {
return originalValue;
}
};
class json_array : public json_node {
private:
std::vector<json_node*> data;
public:
json_array() = default;
json_array(const json_array&) = delete;
json_array& operator=(const json_array&) = delete;
~json_array() override {
for (auto item: data) {
delete item;
}
}
[[nodiscard]] std::vector<json_node*>::size_type size() const {
return data.size();
}
void addValue(json_node* value) {
data.emplace_back(value);
}
[[nodiscard]] JSON_NODE_TYPE getType() const override {
return JSON_NODE_TYPE::JSON_ARRAY;
}
[[nodiscard]] std::string toString() const override {
std::string text("[");
for (auto &item: data) {
text+=item->toString();
}
text+=']';
return text;
}
};
class json_object : public json_node {
private:
std::unordered_map<std::string, json_node*> data;
public:
json_object() = default;
json_object(const json_object&) = delete;
json_object& operator=(const json_object&) = delete;
~json_object() override {
for (const auto &item: data) {
delete item.second;
}
}
std::unordered_map<std::string, json_node*>::size_type size() const {
return data.size();
}
void insertKeyValue(const std::string& key, json_node* value) {
data[key] = value;
}
bool existsValue(const std::string& key) {
return data.find(key) != data.end();
}
JSON_NODE_TYPE getType() const override {
return JSON_NODE_TYPE::JSON_OBJECT;
}
std::string toString() const override {
std::string text("{");
for (auto &item: data) {
text+="\"" + item.first + "\" : ";
text+=item.second->toString();
}
text+='}';
return text;
}
};
json_node* readJsonString(std::string_view jsonString);
std::string_view::size_type valueIdentification(std::string_view text, std::string_view::size_type pos, json_node*& out);
#endif //MINECRAFTSERVER_JSON_READER_H