forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON.hh
206 lines (169 loc) · 6.29 KB
/
JSON.hh
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
#include <exception>
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
class JSONObject {
public:
// thrown when JSON can't be parsed
class parse_error : public std::runtime_error {
public:
parse_error(const std::string& what);
};
// thrown when an object is accessed as the wrong type
class type_error : public std::runtime_error {
public:
type_error(const std::string& what);
};
// thrown when a key doesn't exist in a dictionary
class key_error : public std::runtime_error {
public:
key_error(const std::string& what);
};
// thrown when an element doesn't exist in a list
class index_error : public std::runtime_error {
public:
index_error(const std::string& what);
};
class file_error : public std::runtime_error {
public:
file_error(const std::string& what);
};
enum Type {
Null = 0,
Bool,
String,
Integer,
Float,
List,
Dict,
};
static std::shared_ptr<JSONObject> load(const std::string& filename);
void save(const std::string& filename, bool format = false) const;
static std::shared_ptr<JSONObject> parse(const std::string& s, size_t offset = 0);
// null
JSONObject();
// true/false
explicit JSONObject(bool x);
// string
explicit JSONObject(const char* s);
JSONObject(const char* s, size_t size);
explicit JSONObject(const std::string& x);
JSONObject(std::string&& x);
// integer
explicit JSONObject(int64_t x);
// float
explicit JSONObject(double x);
// list
explicit JSONObject(const std::vector<JSONObject>& x);
JSONObject(std::vector<JSONObject>&& x);
explicit JSONObject(const std::vector<std::shared_ptr<JSONObject>>& x);
JSONObject(std::vector<std::shared_ptr<JSONObject>>&& x);
// dict
explicit JSONObject(const std::unordered_map<std::string, JSONObject>& x);
JSONObject(std::unordered_map<std::string, JSONObject>&& x);
explicit JSONObject(const std::unordered_map<std::string, std::shared_ptr<JSONObject>>& x);
JSONObject(std::unordered_map<std::string, std::shared_ptr<JSONObject>>&& x);
// copy/move
JSONObject(const JSONObject& rhs);
JSONObject(JSONObject&& rhs);
JSONObject& operator=(const JSONObject& rhs);
~JSONObject();
bool operator==(const JSONObject& other) const;
bool operator!=(const JSONObject& other) const;
std::shared_ptr<JSONObject> operator[](const std::string& key);
const std::shared_ptr<JSONObject> operator[](const std::string& key) const;
std::shared_ptr<JSONObject> operator[](size_t index);
const std::shared_ptr<JSONObject> operator[](size_t index) const;
std::shared_ptr<JSONObject> at(const std::string& key);
const std::shared_ptr<JSONObject> at(const std::string& key) const;
std::shared_ptr<JSONObject> at(size_t index);
const std::shared_ptr<JSONObject> at(size_t index) const;
std::unordered_map<std::string, std::shared_ptr<JSONObject>>& as_dict();
const std::unordered_map<std::string, std::shared_ptr<JSONObject>>& as_dict() const;
std::vector<std::shared_ptr<JSONObject>>& as_list();
const std::vector<std::shared_ptr<JSONObject>>& as_list() const;
int64_t as_int() const;
double as_float() const;
std::string& as_string();
const std::string& as_string() const;
bool as_bool() const;
bool is_dict() const;
bool is_list() const;
bool is_int() const;
bool is_float() const;
bool is_string() const;
bool is_bool() const;
bool is_null() const;
int source_length() const;
std::string serialize() const;
std::string format(size_t indent_level = 0) const;
private:
Type type;
int consumed_characters;
// TODO: really there should be a subclass for each type, but this isn't used
// in performance-critical code right now
std::unordered_map<std::string, std::shared_ptr<JSONObject>> dict_data;
std::vector<std::shared_ptr<JSONObject>> list_data;
int64_t int_data;
double float_data;
std::string string_data;
bool bool_data;
};
std::shared_ptr<JSONObject> make_json_null();
std::shared_ptr<JSONObject> make_json_bool(bool x);
std::shared_ptr<JSONObject> make_json_num(double x);
std::shared_ptr<JSONObject> make_json_int(int64_t x);
std::shared_ptr<JSONObject> make_json_str(const char* s);
std::shared_ptr<JSONObject> make_json_str(const std::string& s);
std::shared_ptr<JSONObject> make_json_str(std::string&& s);
std::shared_ptr<JSONObject> make_json_list(
std::vector<std::shared_ptr<JSONObject>>&& values);
std::shared_ptr<JSONObject> make_json_list(
std::initializer_list<std::shared_ptr<JSONObject>> values);
std::shared_ptr<JSONObject> make_json_dict(
std::initializer_list<std::pair<const char*, std::shared_ptr<JSONObject>>> values);
template <typename T>
std::shared_ptr<JSONObject> make_json_list(const std::vector<T>& values) {
std::vector<std::shared_ptr<JSONObject>> vec;
vec.reserve(values.size());
for (const auto& it : values) {
vec.emplace_back(new JSONObject(it));
}
return std::shared_ptr<JSONObject>(new JSONObject(move(vec)));
}
template <typename T>
std::shared_ptr<JSONObject> make_json_list(std::initializer_list<T> values) {
std::vector<std::shared_ptr<JSONObject>> vec;
vec.reserve(values.size());
for (const auto& it : values) {
vec.emplace_back(new JSONObject(it));
}
return std::shared_ptr<JSONObject>(new JSONObject(move(vec)));
}
template <typename V>
std::shared_ptr<JSONObject> make_json_dict(const std::unordered_map<std::string, V>& values) {
std::unordered_map<std::string, std::shared_ptr<JSONObject>> dict;
for (const auto& it : values) {
dict.emplace(it.first, new JSONObject(it.second));
}
return std::shared_ptr<JSONObject>(new JSONObject(move(dict)));
}
template <typename V>
std::shared_ptr<JSONObject> make_json_dict(std::initializer_list<std::pair<std::string, V>> values) {
std::unordered_map<std::string, std::shared_ptr<JSONObject>> dict;
for (const auto& it : values) {
dict.emplace(it.first, new JSONObject(it.second));
}
return std::shared_ptr<JSONObject>(new JSONObject(move(dict)));
}
template <typename V>
std::shared_ptr<JSONObject> make_json_dict(std::initializer_list<std::pair<const char*, V>> values) {
std::unordered_map<std::string, std::shared_ptr<JSONObject>> dict;
for (const auto& it : values) {
dict.emplace(it.first, new JSONObject(it.second));
}
return std::shared_ptr<JSONObject>(new JSONObject(move(dict)));
}