-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.h
171 lines (153 loc) · 3.96 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
/*
Author: twoone3
Last change: 2023.5.24
Github: https://github.com/twoone-3/json
*/
#pragma once
#pragma warning(disable : 4996)
#if defined(__clang__) || defined(__GNUC__)
#define CPP_STANDARD __cplusplus
#elif defined(_MSC_VER)
#define CPP_STANDARD _MSVC_LANG
#endif
#if CPP_STANDARD < 201703L
#error json.h require std::c++17
#endif
// #include <iostream>
#include <map>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
namespace json {
class Value;
using Array = std::vector<Value>;
using Object = std::map<std::string, Value>;
using Data = std::variant<nullptr_t, bool, double, std::string, Array, Object>;
// Type of the value held by a Value object.
enum class Type : uint8_t {
kNull, // null value
kBoolean, // boolean value (bool)
kNumber, // number value (double)
kString, // UTF-8 string value (std::string)
kArray, // array value (Array)
kObject // object value (Object)
};
// JSON Reader
class Reader {
public:
Reader();
Reader& allowComments();
bool parse(std::string_view, Value&);
bool parseFile(std::string_view, Value&);
std::string getError() const;
private:
bool parseValue(Value&);
bool parseNull(Value&);
bool parseTrue(Value&);
bool parseFalse(Value&);
bool parseString(Value&);
bool parseString(std::string&);
bool parseHex4(unsigned&);
bool parseArray(Value&);
bool parseObject(Value&);
bool parseNumber(Value&);
bool skipWhiteSpace();
bool skipComment();
// always return false
bool error(const char*);
const char* cur_;
const char* begin_;
const char* end_;
const char* err_;
bool allow_comments_;
};
// JSON Reader
class Writer {
public:
Writer();
Writer& emit_utf8();
Writer& indent(std::string_view str);
void writeValueFormatted(const Value&);
void writeValue(const Value&);
const std::string& getOutput() const;
private:
void writeHex16Bit(unsigned);
void writeIndent();
void writeNull();
void writeBoolean(const Value&);
void writeNumber(const Value&);
void writeString(std::string_view);
void writeChar(const char*& cur, const char* end, char c);
void writeArray(const Value&);
void writeObject(const Value&);
void writeArrayFormatted(const Value&);
void writeObjectFormatted(const Value&);
std::string out_;
unsigned depth_of_indentation_;
std::string indent_;
bool emit_utf8_;
};
// JSON Value, can be one of enum Type
class Value {
friend class Reader;
friend class Writer;
public:
Value();
Value(nullptr_t);
Value(bool);
Value(double);
Value(const char*);
Value(const std::string&);
Value(const Value&);
Value(Value&&) noexcept;
~Value();
Value& operator=(const Value&);
Value& operator=(Value&&) noexcept;
bool operator==(const Value&) const;
Value& operator[](size_t);
Value& operator[](const std::string&);
void insert(const std::string&, Value&&);
bool asBool() const;
int asInt() const;
unsigned asUInt() const;
int64_t asInt64() const;
uint64_t asUInt64() const;
double asDouble() const;
std::string asString() const;
Array& asArray();
const Array& asArray() const;
Object& asObject();
const Object& asObject() const;
// Exchange data from other Value
void swap(Value&);
// Remove a key-value pair from object
bool remove(const std::string&);
// Remove a value from array;
bool remove(size_t);
// Add elements to the array
void append(const Value&);
// Add elements to the array
void append(Value&&);
// Get size
size_t size() const;
bool empty() const;
// Check whether a certain key is included
bool contains(const std::string&) const;
// Clear contents of value
void clear();
// Generate a beautiful string
std::string dump(bool emit_utf8 = false,
std::string_view indent = " ") const;
// Get type
constexpr Type type() const;
bool isNull() const;
bool isBool() const;
bool isNumber() const;
bool isString() const;
bool isArray() const;
bool isObject() const;
private:
Data data_;
};
} // namespace json