-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathflexbuffer_json.cpp
344 lines (317 loc) · 10.3 KB
/
flexbuffer_json.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "flexbuffer_json.h"
#include <cstring>
#include <istream>
#include <optional>
#include "cata_unreachable.h"
#include "filesystem.h"
#include "json.h"
const std::string &Json::flexbuffer_type_to_string( flexbuffers::Type t )
{
static const std::array<std::string, 36> type_map = { {
"null",
"int",
"uint",
"float",
"key",
"string",
"indirect_int",
"indirect_uint",
"indirect_float",
"object",
"array",
"int_array",
"uint_array",
"float_array",
"key_array",
"string_array_deprecated",
"int_tuple_array",
"uint_tuple_array",
"float_tuple_array",
"int_triple_array",
"uint_triple_array",
"float_triple_array",
"int_quad_array",
"uint_quad_array",
"float_quad_array",
"blob",
"bool",
"bool_array",
}
};
return type_map[static_cast<unsigned int>( t )];
}
// Advances the given jsin according to the passed JsonPath
static void advance_jsin( TextJsonIn *jsin, flexbuffers::Reference root, const JsonPath &path )
{
for( JsonPath::index_type idx : path ) {
if( root.IsMap() ) {
std::string member = root.AsMap().Keys()[ idx ].AsString().str();
jsin->start_object();
while( !jsin->end_object() ) {
if( jsin->get_member_name() == member ) {
break;
}
jsin->skip_value();
}
root = root.AsVector()[ idx ];
} else if( root.IsVector() ) {
jsin->start_array();
for( size_t i = 0; i < idx && !jsin->end_array(); ++i ) {
jsin->skip_value();
}
root = root.AsVector()[ idx ];
} else {
cata_unreachable();
}
}
}
void Json::throw_error( const JsonPath &path, int offset, const std::string &message ) const
{
std::unique_ptr<std::istream> original_json = root_->get_source_stream();
std::string source_path = [&] {
if( root_->get_source_path().empty() )
{
return std::string{ "<unknown source file>" };
}
return root_->get_source_path().u8string();
}();
TextJsonIn jsin( *original_json, source_path );
advance_jsin( &jsin, flexbuffer_root_from_storage( root_->get_storage() ), path );
jsin.error( offset, message );
}
void Json::throw_error_after( const JsonPath &path, const std::string &message ) const
{
std::unique_ptr<std::istream> original_json = root_->get_source_stream();
std::string source_path = [&] {
if( root_->get_source_path().empty() )
{
return std::string{ "<unknown source file>" };
}
return root_->get_source_path().u8string();
}();
TextJsonIn jsin( *original_json, source_path );
advance_jsin( &jsin, flexbuffer_root_from_storage( root_->get_storage() ), path );
jsin.skip_value();
jsin.error( message );
}
void Json::string_error( const JsonPath &path, int offset, const std::string &message ) const
{
std::unique_ptr<std::istream> original_json = root_->get_source_stream();
std::string source_path = [&] {
if( root_->get_source_path().empty() )
{
return std::string{ "<unknown source file>" };
}
return root_->get_source_path().u8string();
}();
TextJsonIn jsin( *original_json, source_path );
advance_jsin( &jsin, flexbuffer_root_from_storage( root_->get_storage() ), path );
jsin.string_error( offset, message );
}
std::string Json::str() const
{
std::string ret;
json_.ToString( false, true, ret );
return ret;
}
bool JsonValue::read( bool &b, bool throw_on_error ) const
{
if( !test_bool() ) {
return error_or_false( throw_on_error, "Expected bool" );
}
b = get_bool();
return true;
}
bool JsonValue::read( char &c, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
c = get_int();
return true;
}
bool JsonValue::read( signed char &c, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
// TODO: test for overflow
c = get_int();
return true;
}
bool JsonValue::read( unsigned char &c, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
// TODO: test for overflow
c = get_int();
return true;
}
bool JsonValue::read( short unsigned int &s, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
// TODO: test for overflow
s = get_int();
return true;
}
bool JsonValue::read( short int &s, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
// TODO: test for overflow
s = get_int();
return true;
}
bool JsonValue::read( int &i, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
i = get_int();
return true;
}
bool JsonValue::read( int64_t &i, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
i = get_int64();
return true;
}
bool JsonValue::read( uint64_t &i, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
i = get_uint64();
return true;
}
bool JsonValue::read( unsigned int &u, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
u = get_uint();
return true;
}
bool JsonValue::read( float &f, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
f = get_float();
return true;
}
bool JsonValue::read( double &d, bool throw_on_error ) const
{
if( !test_number() ) {
return error_or_false( throw_on_error, "Expected number" );
}
d = get_float();
return true;
}
bool JsonValue::read( std::string &s, bool throw_on_error ) const
{
if( !test_string() ) {
return error_or_false( throw_on_error, "Expected string" );
}
s = get_string();
return true;
}
void JsonObject::report_unvisited() const
{
if( !visited_fields_bitset_.all() ) {
std::vector<size_t> skipped_members;
skipped_members.reserve( visited_fields_bitset_.size() );
tiny_bitset::block_t *bits = visited_fields_bitset_.bits();
size_t block_idx = 0;
for( size_t last_whole_block = visited_fields_bitset_.size() / tiny_bitset::kBitsPerBlock;
block_idx < last_whole_block; ++block_idx ) {
tiny_bitset::block_t block = bits[block_idx];
tiny_bitset::block_t mask = tiny_bitset::kLowBit << ( tiny_bitset::kBitsPerBlock - 1 );
for( size_t bit_idx = 0; bit_idx < tiny_bitset::kBitsPerBlock; ++bit_idx ) {
if( block & mask ) {
skipped_members.emplace_back( block_idx * tiny_bitset::kBitsPerBlock + bit_idx );
}
mask >>= 1;
}
}
tiny_bitset::block_t block = bits[block_idx];
tiny_bitset::block_t mask = tiny_bitset::kLowBit << ( tiny_bitset::kBitsPerBlock - 1 );
for( size_t bit_idx = 0, end = visited_fields_bitset_.size() % tiny_bitset::kBitsPerBlock;
bit_idx < end; ++bit_idx ) {
if( !( block & mask ) ) {
skipped_members.emplace_back( block_idx * tiny_bitset::kBitsPerBlock + bit_idx );
}
mask >>= 1;
}
error_skipped_members( skipped_members );
}
}
void JsonObject::error_no_member( const std::string &member ) const
{
std::unique_ptr<std::istream> original_json = root_->get_source_stream();
std::string source_path = [&] {
if( root_->get_source_path().empty() )
{
return std::string{ "<unknown source file>" };
}
return root_->get_source_path().u8string();
}();
TextJsonIn jsin( *original_json, source_path );
advance_jsin( &jsin, flexbuffer_root_from_storage( root_->get_storage() ), path_ );
TextJsonObject jo = jsin.get_object();
jo.allow_omitted_members();
jo.get_member( member );
// Just to make sure the compiler understands we will error earlier.
jo.throw_error( "Failed to report missing member " + member );
}
void JsonObject::error_skipped_members( const std::vector<size_t> &skipped_members ) const
{
std::unique_ptr<std::istream> original_json = root_->get_source_stream();
std::string source_path = [&] {
if( root_->get_source_path().empty() )
{
return std::string{ "<unknown source file>" };
}
return root_->get_source_path().u8string();
}();
TextJsonIn jsin( *original_json, source_path );
advance_jsin( &jsin, flexbuffer_root_from_storage( root_->get_storage() ), path_ );
TextJsonObject jo = jsin.get_object();
jo.allow_omitted_members();
for( size_t skipped_member_idx : skipped_members ) {
flexbuffers::String name = keys_[skipped_member_idx].AsString();
if( strncmp( "//", name.c_str(), 2 ) != 0 ) {
try {
jo.throw_error_at( name.c_str(),
string_format( "Invalid or misplaced field name \"%s\" in JSON data",
name.c_str() ) );
} catch( const JsonError &e ) {
debugmsg( "(json-error)\n%s", e.what() );
}
}
mark_visited( skipped_member_idx );
}
}
void JsonObject::throw_error( const std::string &err ) const
{
Json::throw_error( path_, 0, err );
}
void JsonObject::throw_error_at( const std::string &member, const std::string &err ) const
{
throw_error_at( member.c_str(), err );
}
void JsonObject::throw_error_at( const char *member, const std::string &err ) const
{
std::optional<JsonValue> member_opt = get_member_opt( member );
if( member_opt.has_value() ) {
( *member_opt ).throw_error( err );
} else {
throw_error( err );
}
}