-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppjson.hpp
More file actions
101 lines (78 loc) · 2.07 KB
/
Copy pathcppjson.hpp
File metadata and controls
101 lines (78 loc) · 2.07 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
#ifndef CPP_JSON
#define CPP_JSON
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <functional>
#include <stdexcept>
class SyntaxError : std::logic_error
{
public:
SyntaxError() : std::logic_error("JSON syntax error") {}
};
class JSON
{
public:
enum Type
{
Bool,
Number,
String,
Null,
Object,
Array,
};
private:
Type _type;
std::string valString;
double valNumber;
bool valBoolean;
std::vector<JSON> valArray;
std::map<std::string, JSON> valObject;
std::unique_ptr<JSON> absenceNode;
std::function<void()> setParentNodeFn;
JSON(std::function<void()> setParentCallback) : setParentNodeFn(setParentCallback){};
public:
JSON();
JSON(std::string val);
JSON(std::map<std::string, JSON> val);
JSON(std::vector<JSON> val);
JSON(const char *str);
JSON(const JSON &val);
JSON(nullptr_t val);
JSON(bool val);
JSON(double val);
JSON(long val);
JSON(JSON &&rhs)
noexcept;
bool isBoolean() const;
bool isNumber() const;
bool isString() const;
bool isNull() const;
bool isObject() const;
bool isArray() const;
Type type() const;
JSON &operator[](const std::string &s);
JSON &operator[](size_t idx);
JSON &operator=(const JSON val);
bool &getBool();
const bool &getBool() const;
double &getNumber();
const double &getNumber() const;
nullptr_t getNull() const;
std::string &getString();
const std::string &getString() const;
std::vector<JSON> &getArray();
const std::vector<JSON> &getArray() const;
std::map<std::string, JSON> &getObject();
const std::map<std::string, JSON> &getObject() const;
size_t size() const;
static JSON array();
static JSON array(size_t sz);
friend void swap(JSON &first, JSON &second);
};
std::string toString(const JSON &json);
JSON parse(const std::string &str);
void swap(JSON &first, JSON &second);
#endif