summaryrefslogtreecommitdiff
path: root/lib/jsonParse-persistence.cpp
blob: ba61849ab166d1fe031306a5c49ecd5c93f2f1c3 (plain)
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
#include "jsonParse-persistence.h"
#include <algorithm>
#include <array>
#include <iomanip>
#include <ostream>
#include <type_traits>
#include <utility>

namespace Persistence {
	void
	JsonParsePersistence::loadState(std::istream & in)
	{
		this->switch_streams(&in, nullptr);
		yy_push_state(0);
		yylex();
	}

	void
	JsonParsePersistence::beginObject()
	{
		current()->beforeValue(stk);
		current()->beginObject(stk);
	}

	void
	JsonParsePersistence::beginArray()
	{
		current()->beforeValue(stk);
		current()->beginArray(stk);
	}

	void
	JsonParsePersistence::pushBoolean(bool value)
	{
		pushValue(value);
	}

	void
	JsonParsePersistence::pushNumber(std::string_view value)
	{
		pushValue(value);
	}

	void
	JsonParsePersistence::pushNull()
	{
		pushValue(nullptr);
	}

	void
	JsonParsePersistence::pushText(std::string && value)
	{
		pushValue(std::move(value));
	}

	void
	JsonParsePersistence::pushKey(std::string && k)
	{
		stk.push(current()->select(k));
	}

	void
	JsonParsePersistence::endArray()
	{
		stk.pop();
		stk.pop();
	}

	void
	JsonParsePersistence::endObject()
	{
		current()->endObject(stk);
		current()->endObject(stk);
	}

	template<typename T>
	inline void
	JsonParsePersistence::pushValue(T && value)
	{
		current()->beforeValue(stk);
		current()->setValue(std::forward<T>(value));
		stk.pop();
	}

	inline SelectionPtr &
	JsonParsePersistence::current()
	{
		return stk.top();
	}

	static inline void
	wrv(std::ostream & strm, char ch)
	{
		strm.put(ch);
	}

	static inline void
	wrh(std::ostream & strm, char ch)
	{
		using namespace std::literals;
		strm << R"(\u)"sv << std::setw(4) << std::hex << static_cast<int>(ch) << std::setw(1);
	}

	static inline void
	wre(std::ostream & strm, char e)
	{
		strm << '\\' << e;
	}

	template<char E>
	static inline void
	wre(std::ostream & strm, char)
	{
		wre(strm, E);
	}

	using OutFunc = void (*)(std::ostream &, char);
	using OutFuncs = std::array<OutFunc, 255>;
	static constexpr OutFuncs outFuncs {[]() {
		OutFuncs outFuncs;
		outFuncs.fill(&wrv);
		for (auto x = 0U; x < 0x20U; x += 1) {
			outFuncs[x] = &wrh;
		}
		outFuncs['\"'] = &wre<'"'>;
		outFuncs['\\'] = &wre<'\\'>;
		outFuncs['\b'] = &wre<'b'>;
		outFuncs['\f'] = &wre<'f'>;
		outFuncs['\n'] = &wre<'n'>;
		outFuncs['\r'] = &wre<'r'>;
		outFuncs['\t'] = &wre<'t'>;
		return outFuncs;
	}()};

	JsonWritePersistence::JsonWritePersistence(std::ostream & s) : strm {s}
	{
		strm << std::boolalpha // for Boolean
			 << std::defaultfloat // for Number
			 << std::setfill('0'); // for String \uNNNN
	}

	void
	JsonWritePersistence::beginObject() const
	{
		strm << '{';
	}

	void
	JsonWritePersistence::beginArray() const
	{
		strm << '[';
	}

	void
	JsonWritePersistence::pushValue(bool value) const
	{
		strm << value;
	}

	void
	JsonWritePersistence::pushValue(float value) const
	{
		strm << value;
	}

	void
	JsonWritePersistence::pushValue(int value) const
	{
		strm << value;
	}

	void
	JsonWritePersistence::pushValue(std::nullptr_t) const
	{
		strm << "null";
	}

	void
	JsonWritePersistence::pushValue(const std::string_view value) const
	{
		strm << '"';
		std::for_each(value.begin(), value.end(), [this](char ch) {
			outFuncs[static_cast<unsigned char>(ch)](strm, ch);
		});
		strm << '"';
	}

	void
	JsonWritePersistence::nextValue() const
	{
		strm << ',';
	}

	void
	JsonWritePersistence::pushKey(const std::string_view k) const
	{
		pushValue(k);
		strm << ':';
	}

	void
	JsonWritePersistence::endArray() const
	{
		strm << ']';
	}

	void
	JsonWritePersistence::endObject() const
	{
		strm << '}';
	}
}