-
Notifications
You must be signed in to change notification settings - Fork 0
/
tjson.h
106 lines (78 loc) · 2.1 KB
/
tjson.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
#ifndef TJSON_H
#define TJSON_H
#include <iosfwd>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace tjson {
class TokenGen;
class Val;
std::unique_ptr<Val> read(const char* begin, const char* end,
std::string* out_err);
std::unique_ptr<Val> read(TokenGen* tok_gen,
std::string* out_err);
void write(const Val& root, std::ostream* stream, const std::string& indent);
// -
std::string escape(const std::string& in);
bool unescape(const std::string& in, std::string* out);
// -
class Val
{
public:
static const Val INVALID;
private:
std::unordered_map<std::string, std::unique_ptr<Val>> dict_;
std::vector<std::unique_ptr<Val>> list_;
std::string val_;
bool is_dict_ = false;
bool is_list_ = false;
private:
void reset();
public:
// const
bool operator!() const { return !is_dict_ && !is_list_ && !val_.size(); }
bool is_dict() const { return is_dict_; }
bool is_list() const { return is_list_; }
bool is_val() const { return bool(val_.size()); }
const auto& dict() const { return dict_; }
const auto& list() const { return list_; }
const auto& val() const { return val_; }
const Val& operator[](const std::string& x) const;
const Val& operator[](size_t i) const;
void write(std::ostream* stream, const std::string& indent) const {
tjson::write(*this, stream, indent);
}
bool as_string(std::string* const out) const {
return unescape(val_, &*out);
}
bool as_number(double* out) const;
// non-const
void set_dict() {
if (!is_dict_) {
reset();
is_dict_ = true;
}
}
void set_list() {
if (!is_list_) {
reset();
is_list_ = true;
}
}
std::unique_ptr<Val>& operator[](const std::string& x);
std::unique_ptr<Val>& operator[](size_t i);
// -
auto& val() {
if (!val_.size()) {
reset();
}
return val_;
}
void val(const std::string& x) {
val() = std::move(escape(x));
}
void val(double x);
};
} // namespace tjson
#endif // TJSON_H