-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSON.h
More file actions
169 lines (155 loc) · 4.35 KB
/
JSON.h
File metadata and controls
169 lines (155 loc) · 4.35 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
168
169
/* A simple recursive descent JSON parser.
*
* Copyright (C) 2011 Panagiotis Koutsourakis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef JSON_H_
#define JSON_H_
#include <list>
#include <map>
#include <string>
#include "global.h"
namespace json_parser {
/// Represents a simple JSON value (number, string, literal) using a
/// string.
class Value {
public:
Value(): value_() {}
void SetValue(const std::string &val) {
value_ = val;
}
/**
* Performs pretty printing.
* @param spc How many spaces to put before the data.
* @param skip_initial If true, it does not print the initial spaces
* before the data.
*/
virtual std::string toJSON(int spc, bool skip_initial) const;
virtual ~Value() {}
private:
std::string value_;
DISALLOW_COPY_AND_ASSIGN(Value);
};
/// A linked list of JSON values for use in a JSON Array.
class Elements {
public:
Elements(): values_() {}
void insert(Value *val) {
values_.push_front(val);
}
/**
* Performs pretty printing.
* @param spc How many spaces to put before the data.
* @param skip_initial If true, it does not print the initial spaces
* before the data.
*/
std::string toJSON(int spc, bool skip_initial) const;
~Elements() {
for(std::list<Value *>::iterator it = values_.begin();
it != values_.end(); it++) {
delete *it;
}
}
private:
std::list<Value *> values_;
DISALLOW_COPY_AND_ASSIGN(Elements);
};
/// A JSON array (inherits from Value). It just holds a pointer to the
/// elements.
class Array : public Value{
public:
Array(): elems_(NULL) {}
explicit Array(Elements *elems): elems_(elems) {}
/**
* Performs pretty printing.
* @param spc How many spaces to put before the data.
* @param skip_initial If true, it does not print the initial spaces
* before the data.
*/
std::string toJSON(int spc, bool skip_initial) const;
~Array() {
delete elems_;
}
private:
Elements *elems_;
DISALLOW_COPY_AND_ASSIGN(Array);
};
/// A JSON pair. Holds two JSON values (key and value).
class Pair {
public:
Pair(Value *str, Value *val): key_(str), value_(val) {}
/**
* Performs pretty printing.
* @param spc How many spaces to put before the data.
* @param skip_initial If true, it does not print the initial spaces
* before the data.
*/
std::string toJSON(int spc, bool skip_initial) const;
~Pair() {
delete key_;
delete value_;
}
private:
Value *key_;
Value *value_;
DISALLOW_COPY_AND_ASSIGN(Pair);
};
/// A linked list of elements for use in JSON object.
class Members {
public:
Members(): pairs_() {}
void insert(std::pair<std::string, Value *> p) {
pairs_.insert(p);
}
/**
* Performs pretty printing.
* @param spc How many spaces to put before the data.
* @param skip_initial If true, it does not print the initial spaces
* before the data.
*/
std::string toJSON(int spc, bool skip_initial) const;
~Members() {
typedef std::map<std::string, Value*>::iterator map_iterator;
for (map_iterator it = pairs_.begin();
it != pairs_.end(); it++) {
delete it->second;
}
}
private:
std::map<std::string, Value*> pairs_;
DISALLOW_COPY_AND_ASSIGN(Members);
};
/// A JSON object (inherits from Value) that just holds a pointer to
/// its members.
class Object : public Value {
public:
Object(): members_() {}
explicit Object(Members *mem): members_(mem) {}
/**
* Performs pretty printing.
* @param spc How many spaces to put before the data.
* @param skip_initial If true, it does not print the initial spaces
* before the data.
*/
std::string toJSON(int spc, bool skip_initial) const;
~Object() {
delete members_;
}
private:
Members *members_;
DISALLOW_COPY_AND_ASSIGN(Object);
};
} // namespace json
#endif // JSON_H_