🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
JsonValue.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/StringView.hpp"
25#include <string>
26#include <vector>
27#include <map>
28
29AST_NAMESPACE_BEGIN
30
34{
35 eNull,
36 eBool,
37 eNumber,
38 eString,
39 eArray,
40 eObject
41};
42
46class AST_UTIL_API JsonValue
47{
48public:
52 static JsonValue FromString(StringView json);
53
57 static JsonValue FromFile(const StringView filePath);
58
60 JsonValue();
61
64 explicit JsonValue(bool value);
65
68 explicit JsonValue(int value);
69
72 explicit JsonValue(double value);
73
76 explicit JsonValue(const char* value);
77
80 explicit JsonValue(const std::string& value);
81
84 explicit JsonValue(StringView value);
85
88 explicit JsonValue(const std::vector<JsonValue>& values);
89
92 explicit JsonValue(const std::map<std::string, JsonValue>& values);
93
96 JsonValue(const JsonValue& other);
97
100 JsonValue(JsonValue&& other) noexcept;
101
105 JsonValue& operator=(const JsonValue& other);
106
110 JsonValue& operator=(JsonValue&& other) noexcept;
111
115 template<typename T>
117 {
118 return this->operator=(JsonValue(value));
119 }
120
121
123 ~JsonValue();
124
127 EJsonValueType type() const;
128
131 bool isNull() const;
132
135 bool isBool() const;
136
139 bool isNumber() const;
140
143 bool isString() const;
144
147 bool isArray() const;
148
151 bool isObject() const;
152
156 bool toBool(bool defaultValue = false) const;
157
161 int toInt(int defaultValue = 0) const;
162
166 double toDouble(double defaultValue = 0.0) const;
167
171 std::string toString(StringView defaultValue = StringView()) const;
172
176 const std::vector<JsonValue>& getArray(const std::vector<JsonValue>& defaultValue = std::vector<JsonValue>()) const;
177
181 const std::map<std::string, JsonValue>& getObject(const std::map<std::string, JsonValue>& defaultValue = std::map<std::string, JsonValue>()) const;
182
184 size_t size() const;
185
188 operator bool() const;
189
192 operator int() const;
193
196 operator double() const;
197
200 operator std::string() const;
201
202
206 JsonValue& operator[](size_t index);
207 JsonValue& operator[](int index);
208
212 const JsonValue& operator[](size_t index) const;
213 const JsonValue& operator[](int index) const;
214
215
219 JsonValue& operator[](const std::string& key);
220 JsonValue& operator[](const char* key);
221
225 const JsonValue& operator[](const std::string& key) const;
226 const JsonValue& operator[](const char* key) const;
227
229 void setNull();
230
233 void setBool(bool value);
234
237 void setInt(int value);
238
241 void setDouble(double value);
242
245 void setString(StringView value);
246
249 void setArray(const std::vector<JsonValue>& values={});
250
253 void setObject(const std::map<std::string, JsonValue>& values={});
254
256 void clear();
257
261 void insert(const std::string& name, JsonValue value);
262
266 template<typename T>
267 void insert(const std::string& name, T value)
268 {
269 return insert(name, JsonValue(value));
270 }
271
274 void prepend(JsonValue value);
275
278 void append(JsonValue value);
279
282 template<typename T>
283 void append(T value)
284 {
285 return append(JsonValue(value));
286 }
287
290 std::string toJsonString(int indent = 0) const;
291
295 errc_t parseFromString(StringView json);
296
300 errc_t parseFromFile(StringView filepath);
301private:
302 EJsonValueType type_;
303
304 union
305 {
306 bool boolean_;
307 double number_;
308 std::string* string_;
309 std::vector<JsonValue>* array_;
310 std::map<std::string, JsonValue>* object_;
311 } value_;
312
314 static JsonValue& NullValue();
315
317 void destroy();
318
323 std::string formatString(int indent, int indentSize) const;
324};
325
326
331inline JsonValue operator ""_json(const char* str, size_t len)
332{
333 return JsonValue::FromString(StringView(str, len));
334}
335
336AST_NAMESPACE_END
JSON 值类
定义 JsonValue.hpp:47
void insert(const std::string &name, T value)
插入键值对(模板重载)
定义 JsonValue.hpp:267
std::vector< JsonValue > * array_
数组值
定义 JsonValue.hpp:309
double number_
数值
定义 JsonValue.hpp:307
std::map< std::string, JsonValue > * object_
对象值
定义 JsonValue.hpp:310
std::string * string_
字符串值
定义 JsonValue.hpp:308
bool boolean_
布尔值
定义 JsonValue.hpp:306
JsonValue & operator=(T value)
赋值运算符(模板重载)
定义 JsonValue.hpp:116
void append(T value)
插入元素到数组末尾
定义 JsonValue.hpp:283
EJsonValueType
JSON 值类型枚举
定义 JsonValue.hpp:34