-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONParser.hpp
157 lines (114 loc) · 2.93 KB
/
JSONParser.hpp
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
#include <iostream>
#include <vector>
#include <map>
#include <string.h>
#include <string>
#include <algorithm>
#include <stack>
#include <fstream>
#include <deque>
#include <memory>
// JSON RFC --> https://www.rfc-editor.org/rfc/rfc8259
// O coração do parser é tranformar o JSON em uma árvore N-ária e a partir dai ir extraindo os dados..
using namespace std;
enum ValueType {
OBJECT,
LIST,
STRING,
BOOLEAN,
NUMBER,
NUL
};
struct Node {
void* ptr = nullptr;
template<typename T>
T& get() {
return *(T*)ptr;
}
~Node();
ValueType type;
};
class Index {
public:
Index(bool arg);
// Essa versão do overload é também utilizada pra setar um índice que
// é usado na etapa de conversão JSON -> OBJECT
Index(const char* arg);
string operator()();
// Essa versão do overload é também utilizada pra setar um índice que
// é usado na etapa de conversão JSON -> OBJECT
Index(int32_t arg, int nullFlag = 0);
Index(double arg);
Index(shared_ptr<Node> node);
Index operator, (Index rhs);
shared_ptr<Node> getNode() const;
private:
string index;
shared_ptr<Node> node;
};
void parserObject(string& JSON, map<string, shared_ptr<Node>>& object);
void parserList(string& JSON, vector<shared_ptr<Node>>& list);
class List {
public:
vector<shared_ptr<Node>> array;
Node* operator[](int32_t index);
shared_ptr<Node> operator[](deque<Index> Index);
};
class KeyValue {
public:
KeyValue(string key, Index value);
string getKey() const;
Index getVal() const;
private:
string key;
Index index;
};
class Object {
public:
map<string, shared_ptr<Node>> obj;
Node* operator[](string index);
shared_ptr<Node> operator()(deque<KeyValue> pairs);
};
class pathJSON {
private:
class pathIndex {
public:
pathIndex(const char* index) {
this->index = index;
}
pathIndex(int index) {
this->index = to_string(index);
}
string getIndex() {
return this->index;
}
private:
string index;
};
deque<pathIndex> pathTree;
public:
pathJSON(deque<pathIndex> pathTree);
string getIndex();
bool isEmpty();
};
string getValue(pathJSON pathTree, Node* node);
// Way Down We Go.
Node* getNode(pathJSON pathTree, Node* node);
// partido da assumição de que o JSON é sempre válido
ValueType getTypeFromJSON(string& JSON);
shared_ptr<Node> JSONParse(string& JSON);
// https://www.rfc-editor.org/rfc/rfc8259#page-5
// JSON Lista de simbolos que podem ser ignorados
bool skipSymbols(char c);
void erasePrefixSeparators(string& JSON);
// espera apenas espaços
string getKeyFromJSON(string& JSON);
// espera apenas espaços ou vírgulas precedendo o value
string getValueFromJSON(string& JSON);
#define path pathJSON
#define list List()
#define object Object()
#define literal(a) Index(a)
#define number(a) Index(a)
#define null Index(0, 1)
string JSONStringify(shared_ptr<Node> node);