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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#include "scimpl.h"
#include <library/cpp/json/json_reader.h>
#include <util/stream/output.h>
#include <util/generic/maybe.h>
namespace NSc {
struct TJsonError {
size_t Offset = 0;
TMaybe<TString> Reason;
};
struct TJsonDeserializer : NJson::TJsonCallbacks {
struct TContainer {
TValue* Container = nullptr;
TValue* LastValue = nullptr;
bool ExpectKey = false;
TContainer(TValue& v)
: Container(&v)
, ExpectKey(v.IsDict())
{
}
bool Add(TStringBuf v, bool allowDuplicated) {
if (!ExpectKey || Y_UNLIKELY(!Container->IsDict()))
return false;
if (!allowDuplicated && Y_UNLIKELY(Container->Has(v)))
return false;
LastValue = &Container->GetOrAdd(v);
ExpectKey = false;
return true;
}
void Push() {
LastValue = &Container->Push();
}
TValue& NextValue() {
if (Container->IsArray()) {
Push();
} else if (Container->IsDict()) {
ExpectKey = true;
}
return *(LastValue ? LastValue : Container);
}
bool IsArray() const {
return !!Container && Container->IsArray();
}
bool IsDict() const {
return !!Container && Container->IsDict();
}
};
typedef TVector<TContainer> TStackType;
public:
TValue& Root;
TJsonError& Error;
const TJsonOpts& Cfg;
TStackType Stack;
bool Virgin = true;
public:
TJsonDeserializer(TValue& root, TJsonError& err, const TJsonOpts& cfg)
: Root(root)
, Error(err)
, Cfg(cfg)
{
Root.SetNull();
Stack.reserve(10);
}
bool HasNextValue() const {
return Virgin | !Stack.empty();
}
TValue& NextValue() {
Virgin = false;
return Stack.empty() ? Root : Stack.back().NextValue();
}
bool OnNull() override {
if (Y_UNLIKELY(!HasNextValue()))
return false;
NextValue().SetNull();
return true;
}
bool OnEnd() override {
return Stack.empty();
}
template <typename T>
bool OnValue(T v) {
if (Y_UNLIKELY(!HasNextValue()))
return false;
NextValue() = v;
return true;
}
template <typename T>
bool OnIntValue(T v) {
if (Y_UNLIKELY(!HasNextValue()))
return false;
NextValue().SetIntNumber(v);
return true;
}
bool OnBoolean(bool v) override {
if (Y_UNLIKELY(!HasNextValue()))
return false;
NextValue().SetBool(v);
return true;
}
bool OnInteger(long long v) override {
return OnIntValue(v);
}
bool OnUInteger(unsigned long long v) override {
return OnIntValue(v);
}
bool OnDouble(double v) override {
return OnValue(v);
}
bool OnString(const TStringBuf& v) override {
return OnValue(v);
}
bool OnMapKey(const TStringBuf& k) override {
if (Y_UNLIKELY(Stack.empty()))
return false;
return Stack.back().Add(k, !(Cfg.Opts & TJsonOpts::JO_PARSER_DISALLOW_DUPLICATE_KEYS));
}
bool OnOpenMap() override {
if (Y_UNLIKELY(!HasNextValue()))
return false;
Stack.push_back(TContainer(NextValue().SetDict()));
return true;
}
bool OnCloseMap() override {
if (Y_UNLIKELY(Stack.empty() || !Stack.back().IsDict()))
return false;
Stack.pop_back();
return true;
}
bool OnOpenArray() override {
if (Y_UNLIKELY(!HasNextValue()))
return false;
Stack.push_back(TContainer(NextValue().SetArray()));
return true;
}
bool OnCloseArray() override {
if (Y_UNLIKELY(Stack.empty() || !Stack.back().IsArray()))
return false;
Stack.pop_back();
return true;
}
void OnError(size_t off, TStringBuf reason) override {
Error.Offset = off;
Error.Reason = reason;
}
};
static bool DoParseFromJson(TValue& res, TJsonError& err, TStringBuf json, const TJsonOpts& cfg) {
TJsonDeserializer d(res, err, cfg);
if (cfg.RelaxedJson) {
return NJson::ReadJsonFast(json, &d);
} else {
TMemoryInput min(json.data(), json.size());
return NJson::ReadJson(&min, &cfg, &d);
}
}
static bool DoParseFromJson(TValue& res, TStringBuf json, const TJsonOpts& cfg) {
TJsonError err;
return DoParseFromJson(res, err, json, cfg);
}
TValue TValue::FromJson(TStringBuf v, const TJsonOpts& cfg) {
TValue res;
if (FromJson(res, v, cfg)) {
return res;
} else {
return DefaultValue();
}
}
TValue TValue::FromJsonThrow(TStringBuf json, const TJsonOpts& cfg) {
TValue res;
TJsonError err;
if (DoParseFromJson(res, err, json, cfg)) {
return res;
}
TString reason = err.Reason.Empty() ? "NULL" : *err.Reason;
ythrow TSchemeParseException(err.Offset, reason) << "JSON error at offset " << err.Offset << " (" << reason << ")";
}
bool TValue::FromJson(TValue& res, TStringBuf json, const TJsonOpts& cfg) {
if (DoParseFromJson(res, json, cfg)) {
return true;
}
res.SetNull();
return false;
}
}
|