-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
JSONSerialization.mm
508 lines (452 loc) · 16.5 KB
/
JSONSerialization.mm
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//Copyright (c) 2018 Michael Eisel. All rights reserved.
// NOTE: ARC is disabled for this file
#import "simdjson.h"
#import "JSONSerialization_Private.h"
#import "JSONSerialization.h"
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <stdio.h>
#import <math.h>
#import <string.h>
#import <atomic>
#import <mutex>
#import <typeinfo>
#import <deque>
#import <dispatch/dispatch.h>
using namespace simdjson;
static inline char JNTStringPop(char **string) {
char c = **string;
(*string)++;
return c;
}
typedef simdjson::ParsedJson::iterator Iterator;
// Unless the lib ever supports custom key decoding, it will never try to lookup an empty string as a key
static const char kEmptyDictionaryString[] = "{\"\": 0}";
static const char kEmptyDictionaryStringLength = sizeof(kEmptyDictionaryString) - 1;
struct JNTContext;
struct JNTDecoder {
Iterator iterator;
JNTContext *context;
JNTDecoder(Iterator iterator, JNTContext *context) : iterator(iterator), context(context) {
}
};
struct JNTDecodingError {
std::string description = "";
JNTDecodingErrorType type = JNTDecodingErrorTypeNone;
DecoderPointer value = NULL;
std::string key = "";
JNTDecodingError() {
}
JNTDecodingError(std::string description, JNTDecodingErrorType type, DecoderPointer value, std::string key) : description(description), type(type), value(value), key(key) {
}
};
Iterator JNTSetupEmptyDictionary(ParsedJson &parser);
struct JNTContext { // static for classes?
public:
ParsedJson parser;
std::deque<JNTDecoder> decoders;
ParsedJson emptyDictionaryParser;
JNTDecoder emptyDictionaryDecoder;
JNTDecodingError error;
std::string snakeCaseBuffer;
std::string posInfString;
std::string negInfString;
std::string nanString;
JNTContext(std::string posInfString, std::string negInfString, std::string nanString) : emptyDictionaryParser(ParsedJson()), emptyDictionaryDecoder(JNTDecoder(JNTSetupEmptyDictionary(emptyDictionaryParser), this)), posInfString(posInfString), negInfString(negInfString), nanString(nanString) {
Iterator iterator = JNTSetupEmptyDictionary(emptyDictionaryParser);
}
};
bool JNTErrorDidOccur(ContextPointer context) {
return context->error.type != JNTDecodingErrorTypeNone;
}
void JNTProcessError(ContextPointer context, void (^block)(const char *description, JNTDecodingErrorType type, DecoderPointer value, const char *key)) {
JNTDecodingError &error = context->error;
block(error.description.c_str(), error.type, error.value, error.key.c_str());
}
Iterator JNTSetupEmptyDictionary(ParsedJson &parser) {
bool success = parser.allocateCapacity(kEmptyDictionaryStringLength);
assert(success);
json_parse(kEmptyDictionaryString, kEmptyDictionaryStringLength, parser);
Iterator emptyDictionaryIterator = Iterator(parser);
emptyDictionaryIterator.down();
return emptyDictionaryIterator;
}
typedef JNTDecoder *DecoderPointer;
DecoderPointer JNTEmptyDictionaryDecoder(DecoderPointer decoder) {
return &(decoder->context->emptyDictionaryDecoder);
}
static const char *JNTStringForType(uint8_t type) {
switch (type) {
case 'n':
return "null";
case 't':
case 'f':
return "Bool";
case '{':
return "Dictionary";
case '[':
return "Array";
case '"':
return "String";
case 'd':
case 'l':
return "Number";
}
return "?";
}
static inline void JNTSetError(std::string description, JNTDecodingErrorType type, JNTContext *context, JNTDecoder *decoder, std::string key) {
if (context->error.type != JNTDecodingErrorTypeNone) {
return;
}
JNTDecoder *value = decoder ? new JNTDecoder(*decoder) : NULL;
context->error = JNTDecodingError(description, type, value, key);
}
static void JNTHandleJSONParsingFailed(int res, JNTContext *context) {
std::ostringstream oss;
oss << "The given data was not valid JSON. Error: " << simdjson::errorMsg(res);
JNTSetError(oss.str(), JNTDecodingErrorTypeJSONParsingFailed, context, NULL, "");
}
static void JNTHandleWrongType(JNTDecoder *decoder, uint8_t type, const char *expectedType) {
JNTDecodingErrorType errorType = type == 'n' ? JNTDecodingErrorTypeValueDoesNotExist : JNTDecodingErrorTypeWrongType;
std::ostringstream oss;
oss << "Expected " << expectedType << " but found " << JNTStringForType(type) << " instead.";
JNTSetError(oss.str(), errorType, decoder->context, decoder, "");
}
static void JNTHandleMemberDoesNotExist(JNTDecoder *decoder, const char *key) {
std::ostringstream oss;
oss << "No value associated with " << key << ".";
JNTSetError(oss.str(), JNTDecodingErrorTypeKeyDoesNotExist, decoder->context, decoder, key);
}
template <typename T>
static void JNTHandleNumberDoesNotFit(JNTDecoder *decoder, T number, const char *type) {
char *description = nullptr;
NS_VALID_UNTIL_END_OF_SCOPE NSString *string = [@(number) description];
asprintf(&description, "Parsed JSON number %s does not fit.", string.UTF8String); //, type);
JNTSetError(description, JNTDecodingErrorTypeNumberDoesNotFit, decoder->context, decoder, "");
}
static inline bool JNTIsLower(char c) {
return 'a' <= c && c <= 'z';
}
static inline bool JNTIsUpper(char c) {
return 'A' <= c && c <= 'Z';
}
static inline char JNTToUpper(char c) {
return JNTIsLower(c) ? c + ('A' - 'a') : c;
}
static inline char JNTToLower(char c) {
return JNTIsUpper(c) ? c + ('a' - 'A') : c;
}
static inline uint32_t JNTReplaceSnakeWithCamel(std::string &buffer, char *string) {
buffer.erase();
char *end = string + strlen(string);
char *currString = string;
while (currString < end) {
if (*currString != '_') {
break;
}
buffer.push_back('_');
currString++;
}
if (currString == end) {
return (uint32_t)(end - string);
}
char *originalEnd = end;
end--;
while (*end == '_') {
end--;
}
end++;
bool didHitUnderscore = false;
size_t leadingUnderscoreCount = currString - string;
while (currString < end) {
char first = JNTStringPop(&currString);
buffer.push_back(JNTToUpper(first));
while (currString < end && *currString != '_') {
char c = JNTStringPop(&currString);
buffer.push_back(JNTToLower(c));
}
while (currString < end && *currString == '_') {
didHitUnderscore = true;
currString++;
}
}
if (!didHitUnderscore) {
return (uint32_t)(originalEnd - string);
}
buffer[leadingUnderscoreCount] = JNTToLower(buffer[leadingUnderscoreCount]); // If the first got capitalized
for (NSInteger i = 0; i < originalEnd - end; i++) {
buffer.push_back('_');
}
memcpy(string, buffer.c_str(), buffer.size() + 1);
uint32_t size = (uint32_t)buffer.size();
return size;
}
void JNTConvertSnakeToCamel(DecoderPointer decoder) {
do {
if (!decoder->iterator.is_string()) {
return;
}
uint32_t newLength = JNTReplaceSnakeWithCamel(decoder->context->snakeCaseBuffer, (char *)decoder->iterator.get_string());
decoder->iterator.set_string_length(newLength);
decoder->iterator.move_to_value();
} while (decoder->iterator.next());
}
static inline char JNTChecker(const char *s) {
char has = '\0';
while (*s != '\0') {
has |= *s;
s++;
}
return has;
}
static inline char JNTCheckHelper(Iterator& i) {
char has = '\0';
if (i.is_object()) {
if (!i.down()) {
return has;
}
do {
has |= JNTChecker(i.get_string());
i.move_to_value();
if (i.is_object_or_array()) {
has |= JNTCheckHelper(i);
}
} while (i.next());
} else if (i.is_array()) {
if (!i.down()) {
return has;
}
do {
if (i.is_string()) {
has |= JNTChecker(i.get_string());
} else if (i.is_object_or_array()) {
has |= JNTCheckHelper(i);
}
} while (i.next());
}
return has;
}
// Check if there are any non-ASCII chars in the dictionary keys
static inline bool JNTCheck(Iterator i) {
return (JNTCheckHelper(i) & 0x80) != '\0';
}
ContextPointer JNTCreateContext(const char *negInfString, const char *posInfString, const char *nanString) {
return new JNTContext(std::string(posInfString), std::string(negInfString), std::string(nanString));
}
DecoderPointer JNTDocumentFromJSON(ContextPointer context, const void *data, NSInteger length, bool convertCase, const char * *retryReason, bool fullPrecisionFloatParsing) {
context->parser.full_precision_float_parsing = fullPrecisionFloatParsing;
NSInteger capacity = length ?: 1;
bool success = context->parser.allocateCapacity(capacity);
assert(success);
const int res = json_parse((const char *)data, length, context->parser);
if (res != 0) {
if (res != NUMBER_ERROR) { // retry number errors
JNTHandleJSONParsingFailed(res, context);
} else {
*retryReason = "A number was too large (couldn't fit in a 64-bit signed integer)";
}
return NULL;
}
Iterator iterator = Iterator(context->parser); // todo: remove conflict with swift Iterator and this one
JNTDecoder decoder = JNTDecoder(iterator, context);
if (JNTCheck(iterator)) {
*retryReason = "One or more keys had non-ASCII characters";
return NULL;
} else {
context->decoders.push_back(decoder);
return &(context->decoders.back());
}
}
void JNTReleaseContext(JNTContext *context) {
delete context;
}
BOOL JNTDocumentContains(DecoderPointer decoder, const char *key) {
decoder->iterator.prev_string();
return decoder->iterator.search_for_key(key, strlen(key));
}
namespace TypeChecker {
bool Double(Iterator *value) {
return value->is_double();
}
bool UInt64(Iterator *value) {
return value->is_integer();
}
bool Int64(Iterator *value) {
return value->is_integer();
}
bool String(Iterator *value) {
return value->is_string();
}
bool Bool(Iterator *value) {
return value->is_true() || value->is_false();
}
}
namespace Converter {
double Double(Iterator *value) {
return value->get_double();
}
uint64_t UInt64(Iterator *value) {
return value->get_integer();
}
int64_t Int64(Iterator *value) {
return value->get_integer();
}
const char *String(Iterator *value) {
return value->get_string();
}
bool Bool(Iterator *value) {
return value->is_true();
}
}
template <typename T, typename U, bool (*TypeCheck)(Iterator *), U (*Convert)(Iterator *)>
static inline T JNTDocumentDecode(DecoderPointer decoder) {
Iterator *iterator = &decoder->iterator;
if (unlikely(!TypeCheck(iterator))) {
JNTHandleWrongType(decoder, decoder->iterator.get_type(), typeid(T).name());
return 0;
}
U number = Convert(iterator);
T result = (T)number;
if (unlikely(number != result)) {
JNTHandleNumberDoesNotFit(decoder, number, typeid(T).name());
return 0;
}
return result;
}
void JNTDocumentNextArrayElement(DecoderPointer decoder, bool *isAtEnd) {
*isAtEnd = !decoder->iterator.next();
}
BOOL JNTDocumentDecodeNil(DecoderPointer decoder) {
return decoder->iterator.is_null();
}
double JNTDocumentDecode__Double(DecoderPointer decoder) {
if (TypeChecker::Double(&decoder->iterator)) {
return Converter::Double(&decoder->iterator);
} else if (decoder->iterator.is_integer()){
return (double)decoder->iterator.get_integer();
} else {
if (decoder->iterator.is_string()) {
const char *string = decoder->iterator.get_string();
if (strcmp(string, decoder->context->posInfString.c_str()) == 0) {
return INFINITY;
} else if (strcmp(string, decoder->context->negInfString.c_str()) == 0) {
return -INFINITY;
} else if (strcmp(string, decoder->context->nanString.c_str()) == 0) {
return NAN;
}
}
JNTHandleWrongType(decoder, decoder->iterator.get_type(), "double/float");
return 0;
}
}
float JNTDocumentDecode__Float(DecoderPointer decoder) {
return (float)JNTDocumentDecode__Double(decoder);
}
static bool JNTIteratorsEqual(Iterator *i1, Iterator *i2) {
return i1->get_tape_location() == i2->get_tape_location();
}
NSMutableArray <id> *JNTDocumentCodingPathHelper(Iterator iterator, Iterator *targetIterator) {
if (iterator.is_array()) {
if (iterator.down()) {
NSInteger i = 0;
do {
if (JNTIteratorsEqual(&iterator, targetIterator)) {
return [@[@(i)] mutableCopy];
} else if (iterator.is_object_or_array()) {
NSMutableArray *codingPath = JNTDocumentCodingPathHelper(iterator, targetIterator);
if (codingPath) {
[codingPath insertObject:@(i) atIndex:0];
return codingPath;
}
}
} while (iterator.next());
}
} else if (iterator.is_object()) {
if (iterator.down()) {
do {
const char *key = iterator.get_string();
if (JNTIteratorsEqual(&iterator, targetIterator)) {
return [@[@(key)] mutableCopy];
}
iterator.move_to_value();
if (JNTIteratorsEqual(&iterator, targetIterator)) {
return [@[@(key)] mutableCopy];
} else if (iterator.is_object_or_array()) {
NSMutableArray *codingPath = JNTDocumentCodingPathHelper(iterator, targetIterator);
if (codingPath) {
[codingPath insertObject:@(key) atIndex:0];
return codingPath;
}
}
} while (iterator.next());
}
}
return [@[] mutableCopy];
}
NSArray <id> *JNTDocumentCodingPath(DecoderPointer targetDecoder) {
Iterator iterator = Iterator(targetDecoder->context->parser);
if (JNTIteratorsEqual(&iterator, &targetDecoder->iterator)) {
return @[];
}
return JNTDocumentCodingPathHelper(iterator, &targetDecoder->iterator) ?: @[];
}
NSArray <NSString *> *JNTDocumentAllKeys(DecoderPointer decoder) {
if (decoder->iterator.is_object()) {
return @[];
}
NSMutableArray <NSString *>*keys = [NSMutableArray array];
decoder->iterator.to_start_scope();
do {
if (!decoder->iterator.is_string()) {
break;
}
[keys addObject:@(decoder->iterator.get_string())];
decoder->iterator.move_to_value();
} while (decoder->iterator.next());
return [keys copy];
}
void JNTDocumentForAllKeyValuePairs(DecoderPointer decoderOriginal, void (^callback)(const char *key, DecoderPointer decoder)) {
// Make a copy of the iterator
Iterator iterator = decoderOriginal->iterator;
if (iterator.get_scope_type() != '{') {
return;
}
if (!iterator.down()) {
return;
}
do {
const char *key = iterator.get_string();
iterator.move_to_value();
JNTDecoder decoder = JNTDecoder(iterator, decoderOriginal->context);
callback(key, &decoder);
} while (iterator.next());
}
DecoderPointer JNTDocumentEnterStructureAndReturnCopy(DecoderPointer decoder) {
decoder->context->decoders.emplace_back(*decoder);
if (decoder->context->decoders.back().iterator.down()) {
return &(decoder->context->decoders.back());
} else {
decoder->context->decoders.pop_back();
return NULL;
}
}
__attribute__((always_inline)) DecoderPointer JNTDocumentFetchValue(DecoderPointer decoder, const char *key) {
decoder->iterator.prev_string();
if (!decoder->iterator.search_for_key(key, strlen(key))) {
JNTHandleMemberDoesNotExist(decoder, key);
}
return decoder;
}
bool JNTDocumentValueIsDictionary(DecoderPointer decoder) {
return decoder->iterator.is_object();
}
bool JNTDocumentValueIsArray(DecoderPointer decoder) {
return decoder->iterator.is_array();
}
// todo: test with auto-converting of snake case, for a large json file like twitter
#define DECODE(A, B, C, D) DECODE_NAMED(A, B, C, D, A)
#define DECODE_NAMED(A, B, C, D, E) \
A JNTDocumentDecode__##E(DecoderPointer decoder) { \
return JNTDocumentDecode<A, B, TypeChecker::C, Converter::D>(decoder); \
}
ENUMERATE(DECODE);