-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSON.cpp
More file actions
125 lines (108 loc) · 2.83 KB
/
JSON.cpp
File metadata and controls
125 lines (108 loc) · 2.83 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
/* 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/>.
*/
#include <list>
#include <string>
#include "JSON.h"
using std::string;
namespace json_parser {
std::string Object::toJSON(int spc, bool skip_initial) const {
std::string str;
std::string spcs;
if (members_ != NULL) {
for (int i = 0; i < spc; i++) {
spcs += " ";
}
if (skip_initial) {
str += "{\n";
} else {
str += spcs + "{\n";
}
str += members_->toJSON(spc + 1, false);
str += spcs + "}";
} else {
str = "{}";
}
return str;
}
std::string Members::toJSON(int spc, bool skip_initial) const {
std::string str;
std::string spcs;
for (int i = 0; i < spc; i++) {
spcs += " ";
}
unsigned int sz = pairs_.size();
unsigned int i = 0;
typedef std::map<std::string, Value*>::const_iterator map_iterator;
for (map_iterator it = pairs_.begin();
it != pairs_.end(); it++) {
str += spcs + "\"" + it->first + "\"" + " : " + it->second->toJSON(spc, true);
if (i < sz - 1) {
str += ",\n";
i++;
} else {
str += "\n";
}
}
return str;
}
std::string Pair::toJSON(int spc, bool skip_initial) const {
std::string str = key_->toJSON(spc, false) + " : " + value_->toJSON(spc, true);
return str;
}
std::string Array::toJSON(int spc, bool skip_initial) const {
std::string spcs;
for (int i = 0; i < spc; i++) {
spcs += " ";
}
std::string str;
if (skip_initial) {
str += "[\n";
} else {
str += spcs + "[\n";
}
str += elems_->toJSON(spc + 1, false);
str += spcs + "]";
return str;
}
std::string Elements::toJSON(int spc, bool skip_initial) const {
std::string str;
unsigned int sz = values_.size();
unsigned int i = 0;
for (std::list<Value *>::const_iterator it = values_.begin();
it != values_.end(); it++) {
str += (*it)->toJSON(spc, false);
if (i < sz - 1) {
str += ",\n";
i++;
} else {
str += "\n";
}
}
return str;
}
std::string Value::toJSON(int spc, bool skip_initial) const {
string spcs;
for (int i = 0; i < spc; i++) {
spcs += " ";
}
if (skip_initial)
return value_;
else
return spcs + value_;
}
} // namespace json