forked from sAusk3/Brown_Belt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
106 lines (81 loc) · 1.74 KB
/
Copy pathjson.cpp
File metadata and controls
106 lines (81 loc) · 1.74 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
#include "json.h"
namespace Json{
Node::Node(vector<Node> array) : as_array(move(array)) {
}
Node::Node(map<string, Node> map) : as_map(move(map)){
}
Node::Node(int value) : as_int(value) {
}
Node::Node(string value) : as_string(move(value)) {
}
const vector<Node>& Node::AsArray() const {
return as_array;
}
const map<string, Node>& Node::AsMap() const {
return as_map;
}
int Node::AsInt() const {
return as_int;
}
const string& Node::AsString() const {
return as_string;
}
Document::Document(Node root) : root(move(root)) {
}
const Node& Document::GetRoot() const {
return root;
}
Node LoadNode(istream& input);
Node LoadArray(istream& input) {
vector<Node> result;
for (char c; input >> c && c != ']'; ) {
if (c != ',') {
input.putback(c);
}
result.push_back(LoadNode(input));
}
return Node(move(result));
}
Node LoadInt(istream& input) {
int result = 0;
while (isdigit(input.peek())) {
result *= 10;
result += input.get() - '0';
}
return Node(result);
}
Node LoadString(istream& input) {
string line;
getline(input, line, '"');
return Node(move(line));
}
Node LoadDict(istream& input) {
map<string, Node> result;
for (char c; input >> c && c != '}'; ) {
if (c == ',') {
input >> c;
}
string key = LoadString(input).AsString();
input >> c;
result.insert({move(key), LoadNode(input)});
}
return Node(move(result));
}
Node LoadNode(istream& input) {
char c;
input >> c;
if (c == '[') {
return LoadArray(input);
} else if (c == '{') {
return LoadDict(input);
} else if (c == '"') {
return LoadString(input);
} else {
input.putback(c);
return LoadInt(input);
}
}
Document Load(istream& input) {
return Document{LoadNode(input)};
}
}