-
Notifications
You must be signed in to change notification settings - Fork 0
/
gjson.h
196 lines (159 loc) · 4.76 KB
/
gjson.h
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
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
namespace gjson
{
enum class Type{
Unknown = 0,
Bool = 1,
String = 2,
Integer = 3,
Float = 4,
Object = 5,
Array = 6,
Null = 7,
} ;
struct Value
{
using value_ptr = std::unique_ptr<Value>;
virtual ~Value() = default;
virtual Type GetType() const {return Type::Unknown;}
virtual std::wostream& print(std::wostream& os, unsigned tab = 0) const = 0;
std::wostream& print_tab(std::wostream& os, unsigned count) const
{
for(auto n=0; n < count; ++n){os << "\t";}
return os;
}
};
Value::value_ptr ParseJson(std::istream& is);
struct Null : Value
{
using value_t = std::nullptr_t;
enum Type GetType() const override {return Type::Null;}
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
os << "null";
return os;
}
};
struct Bool : Value
{
using value_t = bool;
Bool(value_t value) : value(value){};
virtual ~Bool() = default;
enum Type GetType() const override {return Type::Bool;}
value_t value;
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
os << (value ? "true" : "false");
return os;
}
};
struct String : Value
{
using value_t = std::wstring;
String(const value_t& value) : value(value){};
virtual ~String() = default;
enum Type GetType() const override {return Type::String;}
value_t value;
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
os << "\"" << value << "\"";
return os;
}
};
struct Integer : Value
{
using value_t = long long;
Integer(const value_t& value) : value(value){};
virtual ~Integer() = default;
enum Type GetType() const override {return Type::Integer;}
value_t value;
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
os << value;
return os;
}
};
struct Float : Value
{
using value_t = double;
Float(const value_t& value) : value(value){};
virtual ~Float() = default;
enum Type GetType() const override {return Type::Float;}
value_t value;
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
os << value;
return os;
}
};
struct Object : Value
{
virtual ~Object() = default;
using value_t = std::unordered_map<std::wstring, Value::value_ptr>;
enum Type GetType() const override {return Type::Object;}
value_t value;
value_t::iterator begin(){return value.begin();}
value_t::iterator end(){return value.end();}
value_t::const_iterator begin() const{return value.cbegin();}
value_t::const_iterator end() const{return value.cend();}
value_t::const_iterator cbegin() const{return value.cbegin();}
value_t::const_iterator cend() const{return value.cend();}
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
//os << std::endl;
//print_tab(os, tab);
os << "{" << std::endl;
if(!value.empty())
{
auto it = value.cbegin();
for(auto n=0; n < value.size() -1 ; ++n, ++it)
{
print_tab(os, tab + 1);
os << "\"" << it->first << "\""<< ": ";
it->second->print(os, tab + 1) << "," << std::endl;
}
print_tab(os, tab + 1);
os << "\"" << it->first << "\""<< ": ";
it->second->print(os, tab + 1) << std::endl;
}
print_tab(os, tab) << "}";
return os;
}
};
struct Array : Value
{
virtual ~Array() = default;
using value_t = std::vector<Value::value_ptr>;
enum Type GetType() const override {return Type::Array;}
value_t value;
value_t::iterator begin(){return value.begin();}
value_t::iterator end(){return value.end();}
value_t::const_iterator begin() const{return value.cbegin();}
value_t::const_iterator end() const{return value.cend();}
value_t::const_iterator cbegin() const{return value.cbegin();}
value_t::const_iterator cend() const{return value.cend();}
std::wostream& print(std::wostream& os, unsigned tab = 0) const override
{
//os << std::endl;
//print_tab(os, tab) << "[" << std::endl;
os << "[" << std::endl;
if(!value.empty())
{
auto it = value.cbegin();
for(auto n=0; n < value.size() -1 ; ++n, ++it)
{
print_tab(os, tab + 1);
(*it)->print(os, tab + 1) << "," << std::endl;
}
print_tab(os, tab + 1);
(*it)->print(os, tab + 1) << std::endl;
}
print_tab(os, tab) << "]";
return os;
}
};
}