-
Notifications
You must be signed in to change notification settings - Fork 20
/
json.h
224 lines (189 loc) · 4.68 KB
/
json.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <map>
#include <string>
#include <vector>
namespace jt {
class Json
{
public:
enum Type
{
Null,
Bool,
Long,
Float,
Double,
String,
Array,
Object
};
enum Status
{
success,
bad_double,
absent_value,
bad_negative,
bad_exponent,
missing_comma,
missing_colon,
malformed_utf8,
depth_exceeded,
stack_overflow,
unexpected_eof,
overlong_ascii,
unexpected_comma,
unexpected_colon,
unexpected_octal,
trailing_content,
illegal_character,
invalid_hex_escape,
overlong_utf8_0x7ff,
overlong_utf8_0xffff,
object_missing_value,
illegal_utf8_character,
invalid_unicode_escape,
utf16_surrogate_in_utf8,
unexpected_end_of_array,
hex_escape_not_printable,
invalid_escape_character,
utf8_exceeds_utf16_range,
unexpected_end_of_string,
unexpected_end_of_object,
object_key_must_be_string,
c1_control_code_in_string,
non_del_c0_control_code_in_string,
};
private:
Type type_;
union
{
bool bool_value;
float float_value;
double double_value;
long long long_value;
std::string string_value;
std::vector<Json> array_value;
std::map<std::string, Json> object_value;
};
public:
static const char* StatusToString(Status);
static std::pair<Status, Json> parse(const std::string&);
Json(const Json&);
Json(Json&&) noexcept;
Json(unsigned long);
Json(unsigned long long);
Json(const char*);
Json(const std::string&);
~Json();
Json(const std::nullptr_t = nullptr) : type_(Null)
{
}
Json(bool value) : type_(Bool), bool_value(value)
{
}
Json(int value) : type_(Long), long_value(value)
{
}
Json(float value) : type_(Float), float_value(value)
{
}
Json(unsigned value) : type_(Long), long_value(value)
{
}
Json(long value) : type_(Long), long_value(value)
{
}
Json(long long value) : type_(Long), long_value(value)
{
}
Json(double value) : type_(Double), double_value(value)
{
}
Json(std::string&& value) : type_(String), string_value(std::move(value))
{
}
Type getType() const
{
return type_;
}
bool isNull() const
{
return type_ == Null;
}
bool isBool() const
{
return type_ == Bool;
}
bool isNumber() const
{
return isFloat() || isDouble() || isLong();
}
bool isLong() const
{
return type_ == Long;
}
bool isFloat() const
{
return type_ == Float;
}
bool isDouble() const
{
return type_ == Double;
}
bool isString() const
{
return type_ == String;
}
bool isArray() const
{
return type_ == Array;
}
bool isObject() const
{
return type_ == Object;
}
bool getBool() const;
float getFloat() const;
double getDouble() const;
double getNumber() const;
long long getLong() const;
std::string& getString();
std::vector<Json>& getArray();
std::map<std::string, Json>& getObject();
bool contains(const std::string&) const;
void setArray();
void setObject();
std::string toString() const;
std::string toStringPretty() const;
Json& operator=(const Json&);
Json& operator=(Json&&) noexcept;
Json& operator[](size_t);
Json& operator[](const std::string&);
operator std::string() const
{
return toString();
}
private:
void clear();
void marshal(std::string&, bool, int) const;
static void stringify(std::string&, const std::string&);
static void serialize(std::string&, const std::string&);
static Status parse(Json&, const char*&, const char*, int, int);
};
} // namespace jt